Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Derrick French
Derrick French
3,370 Points

We never called the function we created so how was it ran? I'm somewhat lost.

I was stuck here debugging and trying to figure out why my browser kept locking up and eventually I figured out what was causing endless loop but we never called out function.

2 Answers

Hi, Derrick

A do while loop works different than a simple while loop. Let's see both of these in action :

Firstly a simple while loop:

var count = 0;

while(count != 0){
  console.log("Hello Derrick");
}

What do you think will be printed on console? we will never go inside the loop because our while condition (count != 0) will return false.

But with Do-while loop we can still go inside the loop once and then check the condition, so no matter what the condition is we will be able to run the body of loop at least once.

var count = 0;

do{
console.log("Hello Derrick");
}while(count != 0);

It will print on console "Hello Derrick" once and then will check the while condition which will turn false so it will only run body of loop for once.

About the video and function,

Look at the first line of code

var randomNumber = getRandomNumber(10);

Dave called the function in the first line of code.

I hope it answers your question , Happy learning :)

  • Zain
Derrick French
Derrick French
3,370 Points

Ok, thanks make sense now, for some reason that wasn't clicking with me.