I'm attempting to solve the following problem from coderbyte.com:
Using the JavaScript language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
The following is my attempt:
function SimpleSymbols(str) {
// code goes here
var abc = 'abcdefghijklmnopqrstuvwxyz';
for (var i = 0; i < str.length; i++) {
if (abc.indexOf(str[i]) !== -1) {
if (str[i-1] + str[i+1] === "++") {
return true;
}
else {
return false;
}
}
}
}
This works for the following cases:
SimpleSymbols("+a+d+"); // true
SimpleSymbols("+ab+d+"); // false
SimpleSymbols("b+d+"); // false
The only case I have found where this doesn't provide the right answer is when there is a trailing letter, for example:
SimpleSymbols("+a+b"); // true
This returns true, when in fact it should return false.
NB: I'm assuming string will be lowercase... I haven't dealt with case sensitivity, but I'd like to get the lowercase version working and then I will make it case independent.
Any ideas on what is wrong with my code?