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 Basics Making Decisions in Your Code with Conditional Statements Program Multiple Outcomes

Angelica Islas
Angelica Islas
4,082 Points

I have questions about the curly brackets from the example. It doesn't look like they are all closed to me.

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}.);
}

Mark Casavantes
Mark Casavantes
2,866 Points

Hello Angelica,

It looks like you put more than one line of code together. Every ";" should end a line. I see some double }} in your code. Eliminate one of the }. back ticks are needed on statements that have ${guess} or ${number}. Why is there a + sign in front of some variables? Change const to let for variable guess. A const variable cannot change once assigned. Instead of having variables guessMore and guessLess, just have a variable guess. Use guess < number for guessLess and guess > number for guessMore. You will need a loop to let people guess more than once. I do not know if I caught all of what you need to fix, but I hope I have helped you.

Steven Parker
Steven Parker
210,155 Points

Mark, a leading "+" is one way of converting a string (like what comes in from "prompt") into a number. And I think this particular example game is intended to allow only 2 guesses by design.

And technically, you can have more than one statement on a line, but it's not usually considered good practice.

1 Answer

Steven Parker
Steven Parker
210,155 Points

One technique that helps visualize proper bracketing is to indent lines based on nesting level. Your code formatted in this way (and shown here using Markdown formatting) looks like this:

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}.`);
}

The code itself seems fine and works correctly.   :+1:

Mark Casavantes
Mark Casavantes
2,866 Points

Thank you Steven, I learn a lot from you every time you post and I appreciate learning from you.