Hi there,
My code below passes the test, but i don’t think it is totally ok. For instance, if I assign the string “undefined” to my letter variable, then my code does not pass. I figured we are supposed to include the string “undefined” instead of getting undefined by the system itself.
P.S. I thought this would be easy when i read it for the first time, but it was NOT AT ALL, at least not for me. It did get me QUITE some time to get it to pass.
Any guidance is appreciated.
**Your code so far**
function fearNotLetter(str) {
let letter;
for(let i =0; i<str.length; i++){
if(str.charCodeAt(i+1) - str.charCodeAt(i)>1){
letter = String.fromCharCode(str.charCodeAt(i) + 1);
console.log(letter);
}
}
return letter;
}
fearNotLetter("abcdefghijklmnopqrstuvwxyz");
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
undefined is a primitive value, not a string. The string value “undefined” is not an undefined value.
A function will implicitly by default return undefined if nothing else is explicitly returned. The choice to use an implicit or explicit return is up to you, but it makes no real difference to the final outcome.
Explicitly returning it does make it, well explicit which might better communicate that it is an expected return value. But if you depend on a function’s return value for making logical decisions returning a boolean value is often a better choice (so for example an “is” function like isEven).
Changing the subject a bit, but you can return inside the for loop once you’ve found the missing letter instead of iterating over the whole thing. (I’ve removed the logic bits from the code, just showing you the structure).
let letter;
for (...) {
if (hasMissingLetter) {
letter = theMissingLetter;
}
}
return letter;
This is fine but you iterate over the whole loop needlessly. Instead you can do something like this:
for (...) {
if (hasMissingLetter) {
return theMissingLetter;
}
}