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 Simplify Repetitive Tasks with Loops The do...while Loop

Why is it when I run the do while statement in the console sometimes it generates < ten random numbers?

function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }

let counter = 0; do { console.log(The random number generated is ${getRandomNumber(10)}); counter += 1; } while ( counter < 10);

2 Answers

Cameron Childres
Cameron Childres
11,820 Points

Hi Samuel,

Identical results in the console get grouped together. In Chrome this displays as a number in a circle to the left. Check for these numbers, if you see any then it means the same random number occurred more than once.

You can see this behavior by running this in your console. You should see "10" circled to the left of the the logged statement:

let counter = 0;
do {
    console.log(`<-- shows that this was logged 10 times`);
    counter += 1;
} while ( counter < 10);

Also Samuel you forgot your back ticks around this --> The random number is ${getRandomNumber(10)}); Hope that helps =)