Hi there , cant understand why i am not returning undefined for the last test
heres the challange :
heres my code
function fearNotLetter(str) {
let arr= str.split('');
let allpresent=true;
console.log(allpresent);
for(let i=0 ;i<arr.length;i++){
if(str.charCodeAt(i)+1!==(str.charCodeAt(i+1))){
let char = String.fromCharCode(str.charCodeAt(i)+1);
console.log(char);
allpresent=false;
return char;
}
else{console.log(allpresent+i);}
}
if(allpresent){
return ;
}
}
fearNotLetter("abcdefghijklmnopqrstuvwxyz");
passing all tests except 'âabcdefghijklmnopqrstuvwxyzâ should return undefined
Formatting applied for human readability:
function fearNotLetter(str) {
let arr = str.split('');
let allpresent = true;
console.log(allpresent);
for (let i = 0; i < arr.length; i++) {
if (str.charCodeAt(i)+1 !== (str.charCodeAt(i+1))) {
let char = String.fromCharCode(str.charCodeAt(i)+1);
console.log(char);
allpresent = false;
return char;
} else {
console.log(allpresent + i);
}
}
if (allpresent) {
return;
}
}
fearNotLetter("abcdefghijklmnopqrstuvwxyz");
Can you explain what you have done so far to troubleshoot whatâs happening? I see an issue when I run this exact code and test case in the console.
Please provide a link to the Challenge when asking a question: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters
i have double checked that my if statement is correct , i have tried to log char , and allpresent to make sure that 1: char is not logging ergo the condition is evaluating false
2 allpresent is evaluating to true ergo we should return undefined ;
Doesnât you console show this output?
true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
What looks odd here?
i replaced ( allpresent+i) with (allpresent, i)
true
true 0
true 1
true 2
true 3
true 4
true 5
true 6
true 7
true 8
true 9
true 10
true 11
true 12
true 13
true 14
true 15
true 16
true 17
true 18
true 19
true 20
true 21
true 22
true 23
true 24
{
n i am getting this which seems more like the output i expect owvere that curly brace is what i imagine the problem is
Ok, so that seems to be coming after you see true 24
. That means i
is 25 when you are getting the }
? This is a problem. Why is this a problem? is i + 1 === 26
a valid index for str
?
does { mean anything ??
i thought there might be a problem when i 25 is compared because there is no i 26. but i imagined it would evaluate to false and go to the else statement, which it does but prints the }
i see, its the unicode after âzâ it evaluates true.
thank you
1 Like
hi just for clarity could i ask what something is compared to if it is compared to an item outside of the bounds of the array?
If you reference an element that is not there, it is undefined
.
const arr = ['a', 'b', 'c'];
console.log(arr[3]); displays undefined, because there is not element at index 3.