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

Maxim Melnic
Maxim Melnic
4,178 Points

Hi, i can write like this? (shorter method)

function randomNumb() { return Math.floor(Math.random() * 10) + 1; }

var guess; var random = randomNumb(); var count = 0;

do { guess = prompt("Number from 1 - 10"); count += 1; } while (parseInt(guess) !== random)

document.write("Number was " + random + " it took you" + count);

1 Answer

Steven Parker
Steven Parker
231,269 Points

The particular optimization you have performed here removes unnecessary variables and test steps, so good job. :+1:

You'll find that most programs can be condensed and optimized. The examples in the courses have a primary objective of conveying concepts clearly, so they will often use a few extra steps and variables to make sure what is happening is easily understood.

In real world programming, optimization will often be more important; but caution should be exercised to avoid creating code that obscures its functionality. This can make future maintenance difficult, particularly if it will be performed by other parties.