-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem_263.js
More file actions
40 lines (30 loc) Β· 1.07 KB
/
problem_263.js
File metadata and controls
40 lines (30 loc) Β· 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const SEPARATORS = [',', ';', ':'];
const TERM_MARKS = ['.', '?', '!'];
const isUpperCase = string => /^[A-Z]*$/.test(string);
/**
*
* @param {string} stream
*/
function sentenceChecker(stream) {
const isValid = (context, char, nextChar) => {
const currValid = true;
if (!context && !isUpperCase(char)) return false;
if (context.length === 1 && (char !== ' ' || isUpperCase(char))) {
return false;
}
if (TERM_MARKS.includes(char))
return (
SEPARATORS.includes(context[context.length - 1]) ||
TERM_MARKS.includes(context[context.length - 1])
);
if (!nextChar) return TERM_MARKS.includes(char) && currValid;
context += nextChar;
return currValid
? isValid(context, nextChar[0], nextChar.substring(1))
: false;
};
return isValid('', stream[0], stream.substring(1)) ? stream : '';
}
console.log(sentenceChecker('This is a valid sentence.')); // This is a valid sentence.
console.log(sentenceChecker('This is a invalid sentence')); // ''
console.log(sentenceChecker('This is a INvalid sentence')); // ''