'i' in for loop never reaches the length of the string

Tell us what’s happening:
In for loop, I set the condition ‘i’ to be less than or equal to the length of the string, but ‘i’ never reaches the length. Hence, code does not pass the last condition of the test.
Why ‘i’ won’t reach 3 in this example?
Many thanks in advance.

Your code so far


function fearNotLetter(str) {

for(let i=0;i<=str.length;i++){

if(str.charCodeAt(i+1) - str.charCodeAt(i) !==1 ){
return String.fromCharCode(str.charCodeAt(i)+1)
  }
  
  /*i never reaches the length of the string, even though condition is set
  to i <= str.length */
 // console.log('i='+i)
 //console.log(String.fromCharCode(str.charCodeAt(i)))
 // console.log('array length='+str.length)
}  
}
fearNotLetter("abc");

Your browser information:

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

Challenge: Missing letters

Link to the challenge:

You have some loop bound issues going on here.

Lets do some debugging:

// Function
function fearNotLetter(str) {
  // Loop over all string entries
  for (let i = 0; i <= str.length; i++) {
    // Debugging output
    console.log("i = " + i);
    console.log(" - curr char: " + str[i]);
    console.log(" - next char: " + str[i + 1]);
    // Return if gap is found
    if (str.charCodeAt(i + 1) - str.charCodeAt(i) !== 1) {
      return String.fromCharCode(str.charCodeAt(i) + 1)
    }
  }  
}

// Test
console.log("Return: " + fearNotLetter("abcdefghijklmnopqrstuvwxyz"));

This outputs:

Click Here for Long Output Log
i = 0
 - curr char: a
 - next char: b
i = 1
 - curr char: b
 - next char: c
i = 2
 - curr char: c
 - next char: d
i = 3
 - curr char: d
 - next char: e
i = 4
 - curr char: e
 - next char: f
i = 5
 - curr char: f
 - next char: g
i = 6
 - curr char: g
 - next char: h
i = 7
 - curr char: h
 - next char: i
i = 8
 - curr char: i
 - next char: j
i = 9
 - curr char: j
 - next char: k
i = 10
 - curr char: k
 - next char: l
i = 11
 - curr char: l
 - next char: m
i = 12
 - curr char: m
 - next char: n
i = 13
 - curr char: n
 - next char: o
i = 14
 - curr char: o
 - next char: p
i = 15
 - curr char: p
 - next char: q
i = 16
 - curr char: q
 - next char: r
i = 17
 - curr char: r
 - next char: s
i = 18
 - curr char: s
 - next char: t
i = 19
 - curr char: t
 - next char: u
i = 20
 - curr char: u
 - next char: v
i = 21
 - curr char: v
 - next char: w
i = 22
 - curr char: w
 - next char: x
i = 23
 - curr char: x
 - next char: y
i = 24
 - curr char: y
 - next char: z
i = 25
 - curr char: z
 - next char: undefined
Return: {

It looks like you have some funny business at the end of your loop. You are overstepping by two iterations. Do you see how?

(By the way, nice Picard avatar :+1:)

1 Like

Thanks for the quick reply. For the avatar comment as well (big Star Trek fan :grinning:)
Hmmm…not sure I follow your feedback though. In this debugging scenario, string is 26 characters long. My loop reaches 25, whilst it should reach 26. Don’t know why. It should be simple: string is 26 characters long => exit when ‘i’ equals 26. That for some reason does not happen.
Hmmmm… :thinking:

Keep in mind that

a) JavaScript uses 0 based indexing

b) You are looking forward to index i + 1

This output

i = 25
 - curr char: z
 - next char: undefined

says “The current index is i = 25 and the current character is z. When I look ahead to character i = 26, that character is undefined. Therefore, I will return the character corresponding to the character code for the character at index i + 1”.

Unpacking the output like this highlights the issue. The character z is an index 25. There is no other character to compare z with because z is at the end of the string. Everything the loop tries to do after it tries to look at a character that doesn’t exist isn’t going to work.

Was I able to make it any clearer?

1 Like

P.S. ‘i’ actually reaches 26, because counting starts from 0, not from 1! :sweat_smile:

Yeah, but you shouldn’t reach 26

a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 Like

It is just the condition of the test. Need to return undefined.

Right. And you will return undefined if you change your loop bound.

You are comparing the first character to the second, the second to the third, and so on. This means you should compare the second to last character to the last character and stop. Right now you are comparing character 25 to character 26 (which doesn’t exist) and character 26 to character 27 (which also does not exist).

I was able to get your code to pass by only changing your loop bound.

1 Like

Yes, you were. Thanx.

1 Like

I’m glad you got your code working!

No, you got my code working :worried:
:grinning: