Skip to content
Advertisement

Do-While loop output is undefined

the console shows undefined for the following loop. the issue is in array[i] as it says i is undefined but it is the index for the loop elements. I don’t want to change the x to I as I want to run the code only once and test that it will stop as x is not < 5.

JavaScript

Advertisement

Answer

so the problem with your code is that you initialised i without difining it let i; and then you are incrementing it i++, set let i = 0;

JavaScript

now if x is not less than 5 then the loop will run once but there will be a problem that if x is less than 5 then loop will run infinitely so instead of just x < 5 dot x < 5 && i < array.length

JavaScript

now it will work just fine !

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement