Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

My code worked but isn't exactly the same. Is that okay?

Hi,

Before Dave showed his solution I tried to write this program. I managed to get a similar outcome although my code is very different. (See below)

Is this an issue. How can I learn how to get to the answer is the most efficient way. Does it just take lots of practice?

var randomNumber = getRandomNumber(10000);
var counter = 1;
var guess = true;

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

while (guess) {

  var guessNumber = getRandomNumber(10000);

  if (guessNumber == randomNumber) {
    guess = false;
  } else {
    counter += 1;
    guessNumber = getRandomNumber(10000);
  }
}

alert(`The number was ${randomNumber}. It took ${counter} guesses.`);

Thank you!

Kit :)

Broderick Lemke
Broderick Lemke
13,480 Points

Hi Kit! This snippet of code looks good to me. There are a few key differences with your code and Dave's but from what I'm seeing there isn't a major issue with efficiency. Over time you will learn things that can really take a hit on efficiency (traveling through an array multiple times, looping more than needed, connecting to an outside datasource repeatedly) and will come up with ways to work around them.

I would say that Dave's code might be more readable and maintainable where as you have a lot more going on, but this isn't a wrong answer at all. I do see a bit of your code that could be improved though, which would help the readability! Let's imagine our randomNumber picked at the start of the program is 1337. We then enter the while loop because guess is true. We set guessNumber to a random number, let's say 1853. We check if they're equal and they're not, so we go into the else statement. We add 1 to the counter and pick a new random value for guessNumber: 1963. Now we get to the end of the loop and check if guess is still true which it is. Everything seems to work so far but then we generate a new random number. We never used the 1963 we generated in the else statement. I would update the code like this:

// Generate a random number that we will guess
var randomNumber = getRandomNumber(10000);
var counter = 1;
var guess = true;

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

while (guess) {
  //Update the counter BEFORE we make a guess
  counter += 1;

  //Get a new random number to compare to our random number
  var guessNumber = getRandomNumber(10000);

  //If our two numbers match, set guess to false to end the while loop.
  if (guessNumber == randomNumber) {
    guess = false;
  } 
}

alert(`The number was ${randomNumber}. It took ${counter} guesses.`);

As a final note: If you're really interested in learning more about efficiency you could always look into Big O notation which is a system that is used to describe how well a piece of code will scale and if it is suitable for the amount of data you're dealing with and performance level you are looking for.

P.S. Nice work with the template literals in your alert at the end :)

1 Answer

Firas Al-Mahrouqi
Firas Al-Mahrouqi
2,750 Points

Broderick Lemke,

Wouldn't updating the counter before making a guess result in a wrong count should the loop terminate on the first attempt?