Assessment Wanted for Loops Skills Test

Hello all! I would appreciate any feedback on the last 2 Loops skills test. I can understand the logic behind the For loop, and the do while loop, but for the life of me am having a really difficult time with ‘While Loops’ If anybody could give me some feedback on how to implement those into these scenarios, or even just give me some examples of times where a while loop would be necessary, it would be greatly appreciated. Thanks all!

Loops 2 - For Loop,
Loops 2 - Do-While,
Loops 3 - Do-While,

again, any feedback is greatly appreciated.

p.s. I have also been looking for more channels to connect with the coding community, whether that be Slack or Discord or what have you, I would be happy to hear all your suggestions.

Take care all!

Hello @meestajones

you doing great well done

just tiny things which i am sure it was typo

for loop2-do-while

'\s Phone Number is : '

after \ you missed to type ’

could you tell me what the issue you have with while loop

you can use for and while for the same case for example you can write for like this

for(;condition;)

which would be equal to the while
but for me i use for when i have fixed amount of number that the loop will run and while when it will execute based on condition that change depend on other factor

the only thing that do while is difference that it will run at least one time

so you can use both as you like but it better to use the one with the best use case

like if and switch

hope that help and have a nice day :slight_smile:

I think I just needed to play around with while some more so what I did was
i=10;
while (i>0) {
alert('Hello World: ’ + i);
i–;
}
and that helped me to visualize exactly how it works.
I guess the only thing that I’m having trouble figuring out is a good use case for a while loop? Do you have any specific examples (without going too in depth) of when that would be the best way to go?

let me ask @chrisdavidmills

could you give him an example that show that it better to use for than while and another example for using while is better than for

even if both can be used for the same thing

and have a nice day both of you :slight_smile:

Hi @justsomeone and @meestajones!

This is an interesting question, and something we don’t cover much in our learning loops tutorial.

I had a look around, and found this article, which seems useful: https://www.digitalocean.com/community/tutorials/using-while-loops-and-do-while-loops-in-javascript

the key bits I pulled out of here are:

  • while and do…while loops differ in that they keep running forever while a condition is true, whereas a for loop runs a set number of times. It is not necessary to set the exit condition up front with while/do.while loops.
  • do…while will always run at least once.

But apart from that, it doesn’t really matter which one you use, as long as you understand them, and they do what you want them to do.

thanks a lot @chrisdavidmills :slight_smile:

Yes thanks so much @chrisdavidmills. As with most things to do with programming, I’m sure the more practice I get with it all, the more comfortable I will become with these concepts.

1 Like

You are welcome folks! I found the question interesting, because it is not something I’ve really considered before. I usually tend to just use for loops for everything, unless there’s a more convenient utility function availabe for what I want to do (such as forEach() or map()), in which case I’ll use something like that instead.

1 Like