-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathab_check.js
38 lines (33 loc) · 1.06 KB
/
ab_check.js
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
/**
* Have the function abCheck(str) take the str parameter being passed and return
* the string true if the characters a and b are separated by exactly 3 places
* anywhere in the string at least once (ie. "lane borrowed" would result in
* true because there is exactly three characters between a and b). Otherwise
* return the string false.
*
* https://www.coderbyte.com/results/bhanson:AB%20Check:JavaScript
*
* @param {string} str
* @return {string} 'true' or 'false'
*/
function abCheck(str) {
let searchLetters = ['a', 'b'];
let letterSpace = 3;
if (str.length < letterSpace + 2) {
return 'false';
}
for (let i = 0; i < str.length - letterSpace - 1; i++) {
if (str[i] === searchLetters[0]) {
if (str[i + letterSpace + 1] === searchLetters[1]) {
return 'true';
}
}
if (str[i] === searchLetters[1]) {
if (str[i + letterSpace + 1] === searchLetters[0]) {
return 'true';
}
}
}
return 'false';
}
module.exports = abCheck;