So i’m trying to work out a solution for the intermediate challenge - Missing Letters. I have the steps worked out in my head, i want to grab the missing value of code and then once it is stored in an array I can covert it back to a letter using .fromCharCode
I have took a couple of days off from learning to code and feel like I am already rusty, need to learn to maintain consistency!
Here is my code, am i on the right track with my logic? I keep getting the value back of 98 which i know is incorrect but i cant seem to work it out. If anyone could point me in the right direction that would be great, my other solution would be to use a nested for loop rather than .map but I still come to the same roadblock.
Thank you!
Your code so far
function fearNotLetter(str) {
let letters = str.split("");
let code = letters.map(code => code.charCodeAt());
for (let i =0; i < code.length; i++) {
if (code[i + 1] - code[i] !== 1) {
}
return code[i] + 1;
}
}
console.log(fearNotLetter("abce"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.
When we call fearNotLetter, the string gets split into characters in an array.
Then i have mapped each element and converted it into code using .charCodeAt.
After this I was looking to iterate over the code and find any gaps in the consistency and pull this value. (unsure if this logic is correct)
The return statement was supposed to return the missing value, but it was a shot in the dark!
Thanks so much! I’m glad I had the logic correct and it was just a case of understanding scopes. This confuses me because I thought we should not use return in the first curly braces because then it will immediately stop the iteration but here we are doing so.
I have passed all but the undefined part of the problem now and I have tried every possible scope for “return undefined” and also tried using an } else { }
Heres the code after your help:
let letters = str.split("");
let code = letters.map(code => code.charCodeAt());
for (let i =0; i < code.length; i++) {
if (code[i + 1] - code[i] !== 1) {
Let’s look at that last function call: fearNotLetter("abcdefghijklmnopqrstuvwxyz")
This one is not missing any letters. So, your loop reaches the last letter, z, and hits the if block.
if (code[i + 1] - code[i] !== 1)
Because we are at the end of the code array, code[i+1] is undefined. This means your if condition looks for undefined - code[i], which evaluates to NaN. This does not equal 1 so the code in the if block runs.
The return statement executes, and returns the string from the charcode code[i]+1, which happens to be {.
So, how would you adjust your loop bounds to stop at one element before the end of the array?
Solved! Thanks so much for your superb replies they really helped me understand why the problems were occurring. I like the way you troubleshoot problems by breaking down into steps what is going on, and will look to do this also. I’m going to work on getting a better understanding of scopes as I do tend to get confused on this topic.
Thanks again,
function fearNotLetter(str) {
let letters = str.split("");
let code = letters.map(code => code.charCodeAt());
for (let i =0; i < code.length-1; i++) {
if (code[i + 1] - code[i] !== 1) {