-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabCheck.js
More file actions
25 lines (24 loc) · 705 Bytes
/
abCheck.js
File metadata and controls
25 lines (24 loc) · 705 Bytes
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
/* determine if two characters are separated a specific way in the string.
* 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) */
function ABCheck(str) {
str = str.split("");
for (var i = 0; i < str.length; i++) {
if (/a/.test(str[i])) {
var aIndex = i;
}
if (/b/.test(str[i])) {
var bIndex = i;
}
}
return (bIndex - aIndex == 4);
}
var str = "lane borrowed";
console.log(ABCheck(str));
/* str = "after it"
"lane borrowed"
"aaaaddddd"
"123advb"
*/