Missing letters help converting from ascII code

Tell us what’s happening:
I have opted to use a for loop on this to iterate through the values, in the description it says no sorting is needed. I use the for loop to check if the next value in the string is just one ascii code away from the current value. if it’s not, I want to push the ascii value +1 to chars. I just need to know how to get the char code from an ascii value. I saw on stack overflow that if it’s a string, I could use the .fromCharCode method but on string.prototype on MDN, I don’t see that being an option. Is there a way to get the character from the ascii code? what data type should I use?

Your code so far


function fearNotLetter(str) {
  let test = 'e';
  console.log(test.charCodeAt(0));
  let chars =[];
  for (i=0; i<str.length; i++){ 
    console.log(str.charCodeAt(i)+1);
    if (str.charCodeAt(i) + 1 !== str.charCodeAt(i+1)){
      console.log('inside the if loop', str.charCodeAt(i), str.charCodeAt(i) + 1);
      chars.push(str.charCodeAt(i) + 1);
      break;
    } console.log('this is chars' + chars);
    console.log(chars.fromCharcode(chars));

    // so far I have identified that the charCodeAt the missing one is 100.  now all I have to do is use fromCharCode at it and it should work.

  //  chars.push(str.charCodeAt(i));
  // }
//console.log(chars.fromCharCode(chars));
  // let first = str.charCodeAt(0);
  // console.log(first);
  // let last = str.charCodeAt(str.length-1);
  // console.log(last);
  // console.log(chars);
  // let answer = str.split("");
  // return answer;
} return chars;
}

fearNotLetter("abce");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters/

it should be used with String so rewrite to:

chars.push(String.charCodeAt(i));

does spelling it there with a capital S use a constructor to make the 100 it returns into a string like ‘100’?

sorry I made a mistake earlier , to convert to a character you need
String.fromCharCode(i);

The other one changes a character to a number

1 Like

so this is a replit I made using String.fromCharCode() method but I try i in there and it gives a square, and when I try putting chars in there, it give me ‘d’ which I expect to be the solution.

String.fromCharCode(chars);

you are not giving it a number, you are giving it an array.
Try passing a number in.

1 Like

I figured it out, thank you very much for your help :slight_smile:

1 Like

did you end up using fromCharCode or did you use a totally different algorithm? (I actually used regular expressions to solve this one)

I used fromCharCode but i made it a little less efficient by making the for loop loop through 25 letters so it never gets past z. i had a problem that it never returned undefined and that’s how I got it to work.

My suggestion: If you know that the chars are going to be consecutive until that one missing sheepie, why not compare the current char with the last char and make sure that it’s off by one. If not, then you know you have a missing sheepie. And you won’t have to loop through 25 letters.

2 Likes

Taking a closer look at the original post, you’re already doing that. Now I’m not sure what the issue is since you solved it. My bad.

I couldn’t figure out why it didn’t work with the last test case but when I changed to loop 25 times it fixed it… weird.

Well if you have a working solution that passes the test, I guess all’s fine in the end. If you want improvements and post code though, make sure to add the spoiler tags.