- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
describe
Given A string representing A student’s attendance, ‘A’ represents attendance, ‘D’ represents absence, and ‘L’ represents tardiness. The student will be punished for two or more absences or three or more consecutive tardiness. Please determine whether the student should be punished and return to the Boolean type.
**
The string contains only three uppercase letters: A, D, and L. The value contains a maximum of 10000 characters.
The sample
The student broke the contract twice and was late for three times in succession, so he should be punished.
The student has only broken the contract once, but he has been late for three times in a row, so he should be punished.
Explanation 3: The student only broke the contract once, and he was late at most twice in a row, so he should not be punished.
Answer key
So let’s count absenteeism, D stands for determine, go through the string, find the number of D’s, the number of absenteeism.
The string that corresponds to being late for three times is’ LLL ‘. The JavaScript string has an includes method. Includes () checks whether the string contains the specified substring.
Var STR = "Hello world, welcome to the Runoob." ; var n = str.includes("world"); *n* Output: trueCopy the code
By using this method, you can quickly determine whether there is an LLL.
export class Solution {
/**
* judge
*
* @param record: Attendance record.
* @return: If the student should be punished return true, else return false.
*/
judge(record) {
// Write your code here.
let countD=0
for(let item of record){
if(item==='D'){
countD++
}
}
if(countD>=2||record.includes('LLL')) {return true
}else return false}}Copy the code