Tell us what’s happening:
I’ve got 4/5 correct but cannot understand why my code is not returning undefined as specified. Would appreciate any kind of help to identify my mistake.
Thanks in advance! Your code so far
function fearNotLetter(str) {
for(var i = 0; i<str.length; i++){
var first = str.charCodeAt(i);
var second = str.charCodeAt(i+1);
if((second - first) == 1){
continue;
}
else return ((second-first) != 1)? String.fromCharCode(first + 1) : undefined;
}
}
fearNotLetter("abce");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
In your ternary you are checking whether (second - first) != 1, but you only get to this line of code if that is true. That means that you will always return String.fromCharCode(first + 1) when this line is reached.
If you iterate to the end of the array with i, then you are reaching past the end of the array with this variable. I’d shorten up the range over which you iterate.