Iterate with JavaScript While Loops, Clarity on where i go wrong

Please kindly help i have been stuck on this question more than i my mind can take. clearly the example does not provide for me.

Link to the challenge:

Can you give us a little more detail about what you are stuck on? Did you attempt to solve this challenge at all? If so, can you show us your code?

Yep i did, i always post my solution after attempt but one guy on this platform advised me to stop sharing my solution because it encourages laziness for others who attempt to not solve the code, but i will go back to it and share with you, basically it is the loops i honestly don’t understand how they work and the examples are not helping because i cant seem to run the code although it looks like it coming alright but just towards the end i encounter errors that prohibit my solution to run.

Do you not understand the concept of a loop? Or is it the syntax? If I give you the following code:

let i = 0;
while (i<10) {
  i++;
}

How many times will the code inside the while loop run? What will the value of i be after this code executes?

I’m just guessing what questions you might have. If you can be more specific that would help a lot.

1 Like

Its alright to share wrong code, one that does not work, when asking for advises and solutions. When you need to post complete code, which works, you can use spoiler tags, to hide it. [spoiler] this will remain hidden[/spoiler] like this.
Its hard to understand what exactly bothers you in loops. Is it the general concept, is it the syntax. Is it one of the loop types which is harder to comprehend? While loops, while considered the most basics, can also be harder to comprehend, as they are not as explicit, as for example the for loop, where you can give some more clear parameters on how the loop to behave.
Regarding while loops, the code in the loop body will keep repeating, until the condition in the the loop parenthesis evaluate to false. I.e. unless you plan on run the loop infinite, you must find a way to make the condition become false. Obviously, it needs to be truthy initially, for the loop to have any effect. For example:

let count=0
while (count<5) {
  count++
  console.log('count='+count, '| Is count < 5 ?', count<5)
}

This loop has a simple counter, i called “count”. It will run while count is lower than 5. Inside the loop, i add 1 to count, every iteration. This means, since it starts as 0, it will repeat 5 times and when count becomes 5, it will quit, as the condition will evaluate to false. I placed a console log and if you attempt it, you will notice how the message is printed 5 times in the console, for every loop iteration.

honestly it is applying the concept which obviously make it difficult to follow the syntax.

Just like learning any other language, unfamiliar syntax is going to take a bit to get comfortable with. Fortunately, a while loop is a fairly simple structure:

while (this statement is true) {
    do whatever is in here then
    check statement again
}

Is there anything you don’t understand about the above? If not, then you understand how a while loop works!

my mind is tired i will give it another try the next day, you putting it in a sounding better version but it which i believe i will be able to follow the moment a code is break down in a sense to reason based on what you have shared with regard the concept of the loops.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.