Hi my fellow FCC warriors. I need help in this challenge because I have absolutely no clue what I’m doing in this challenge. I looked at the hints and I was told to use 2 methods: charCodeAt and fromCharCode which both I can’t imagine to use them effectively. In fact, I feel my code is trash and is lost and is all over the place. I look forward to your helps!
link to challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters/
function fearNotLetter(str) {
let arr = [];
let testStr = "d"
let code = testStr.charCodeAt(0)
console.log(code)
for ( let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 0 ) {
arr.push(str.charCodeAt(i))
}
}
console.log(arr)
for ( let i = 0; i < arr.length; i++) {
if ( code < arr[i]) {
arr.push(code)
}
}
console.log(arr)
let convert = arr.map(elem => String.fromCharCode(elem))
console.log(convert)
let joined = convert.join("")
let filtered = joined.slice(joined.length-1)
console.log(filtered)
return filtered;
}
console.log(fearNotLetter("abce"));
Well I’m not the best to explain this, but I feel comfortable giving you two hint questions cause I just did this one myself.
What are you comparing this to, charCodeAt returns a number but are you looking for an equality that is greater than 0 or a charCode number?
Your using two for loops, is it needed? Your first for loop starts at 0, but you found the first charCode already couldn’t you compare off of that one?
Again I apologize if this isn’t helpful, I’m here learning too, but I like to give help if I can.
for 1. i’m basically just converting all the alphabets into unicodes and then followed by “>0” which is basically just there for the sake of it since all the unicodes are basically more than 0. and if its more than 0, i just execute the following statements.
idk man…not too sure cuz in the first for loop i pushed the unicodes into “arr”. so then the second for loop, i iterate the “arr”
but anyways im not accomplishing anything despite all my huge chunks of code.
function fearNotLetter(str) {
let testindex = str.charCodeAt(0);
for ( let i = 1; i < str.length; i++) {
if (str.charCodeAt(i)-1 === testindex) {
// Would this be undefined because it passed the test?
}
else { return /* This where you can use the char code methods you mention */ };
}
}
This is the format I used…I don’t want to give anymore and I’m not saying this is the best way to go about it. I left out variables and other pieces if you have more questions please feel free to ask.
okay so first you change the letter of the first index to a unicode. then iterate them starting with index 1 which is “b”. and only b will pass the condition and execute its statements because “b” is 98 and 98-1 = 97 ( unicode of a ). so the first “if condition passes” it means the numbers are in order and the “else” means if its NOT in order? im still wondering what statements i should execute…
“update index” as in increment ++ ? i did that and it looks like im comparing each letter to the letter BEFORE it. so for eg. i start iterating “b” then im comparing “b” to the letter before which is “a”. then i continue to iterate c, and then compare it to to the letter before it which is b and so on.
now if im understanding this correctly, if the “if condition passes every loop and until the loop is done”. i should “return undefined”? because it means every letter is in order and there’s no “Missing letter”.
but im wondering what should i put in the if STATEMENT everytime i loop ONCE?
As for the “else”, it means the letters are not in order which means there’s a missing letter in between. any idea how to place the appropriate letter between them?
Still not working for some reason… where am i going wrong
function fearNotLetter(str) {
let testindex = str.charCodeAt(0);
for ( let i = 1; i < str.length; i++) {
if (str.charCodeAt(i)-1 === testindex) {
return undefined;
}
else { return String.fromCharCode(str.charCodeAt(i)-1)
};
}
testindex++
}
console.log(fearNotLetter("abce"));
i feel like the “return undefined” shouldn’t be in the if condition, it should be returned like in the final line of the function. but there’s 1 more issue. for the “else statements”. shouldn’t the final iteration go through “e” and it is “101” then i do 101 - 1 = 100 which is “d” the answer. but it doesn’t seem to work out.
NVM I SOLVED IT lmao. i put the "testindex too far below. i fixed it, its just now in the for loop. so now the comparison works, and eveyrthing works!
function fearNotLetter(str) {
let testindex = str.charCodeAt(0);
for ( let i = 1; i < str.length; i++) {
if (str.charCodeAt(i)-1 === testindex) {
}
else { return String.fromCharCode(str.charCodeAt(i)-1)
};
testindex++
}
return undefined
}
console.log(fearNotLetter("abce"));
function fearNotLetter(str) {
let testindex = str.charCodeAt(0);
for ( let i = 1; i < str.length; i++) {
if (str.charCodeAt(i)-1 !== testindex) {
return String.fromCharCode(str.charCodeAt(i)-1)
}
testindex++
}
}
console.log(fearNotLetter("abce"));