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

Ahmed El-Kordy
Ahmed El-Kordy
3,565 Points

why repeating, if statement in each code block.

So basically your first if statement is declared at the top of the code block, but u still repeated it in each else if clause, would u elaborate more on that please, cuz I ran the same code without repeating

if ( +guessMore === number ) {
    correctGuess = true;
  }

and it worked

let correctGuess = false;
const number = 6; 
const guess = prompt('Guess a number between 1 and 10.');

if ( +guess === number ) {
  correctGuess = true;
} else if ( +guess < number ) {
  const guessMore = prompt(`Try again. The number is higher than ${guess}`);
  if ( +guessMore === number ) {
    correctGuess = true;
  }
} else if ( +guess > number ) {
  const guessLess = prompt(`Try again. The number is lower than ${guess}`);
  if ( +guessLess === number ) {
    correctGuess = true;
  }
}

if ( correctGuess ) {
  console.log("You guessed the number!");    
} else {
  console.log(`Sorry. The number was ${number}.`);  
}
Alexandra Hadjidaki
Alexandra Hadjidaki
26,496 Points

Is this the original code with the repetition you are referring to, or your code without the repetition? It's unclear and would help me answer your question :)

1 Answer

Joshua Graber
Joshua Graber
11,785 Points

Hi, Ahmed,

From what I can see in the code posted, there's nothing repeating unnecessarily. Each condition branches the code to test one possibility based on the guest a user entered. The reason that each condition repeats the code you've identified is so that, if the user's guess is in fact correct, the code sets the correctGuess flag to true, and the final if/else statement delivers the "correct" message to the user. If it is not correct, the correctGuess flag remains false, and the user gets the "incorrect" message.

Because it's always a possibility that the user has guessed the correct answer, each code block needs to evaluate the user's guess against the number they're trying to guess. That's why the repetition. (Although, if it's really bothersome to you, you could try to remove that code and place it into a separate function and call it from within the current code when each statement is tested... it would make for a more DRY script, and might be a fun challenge!)

Hope this helps!!

Sincerely, Joshua

Ahmed El-Kordy
Ahmed El-Kordy
3,565 Points

well explain thank you for your time/ support greatly appreciated