Javascript String Question 17

Introduction

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.

For example, "lane bat" would result in true because there is exactly three characters between a and b.

function ABCheck(str) {
    //your code here
}
var str = "lane bat";
console.log(ABCheck(str));


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 bat";
console.log(ABCheck(str));



PreviousNext

Related