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

Can anyone help me with this code?

hey im having a problem with this code

function Submit() {
  var name = prompt("Hello, type your name");
  alert("Hello " + name + " lets start the game");

  alert("We gonna roll a dice, and you gonna guess what it lands on!");


  var randomNumber = Math.floor(Math.random() * 6) + 1;

  var guess = prompt("Guess what it lands on! from 1-6");


  if (guess = randomNumber) {
    alert("YOU GUESS IT RIGHT!!!! CONGRATULATIONS!");
  } else {
    alert("you didnt guess it right, the dice landed on " + randomnumber);
  }

}

the code after the variable "guess" does not work properly,

1 Answer

you should change:

if(guess = randomNumber) - cause that just gives guess the number of randomNumber

to:

if(guess == randomNumber) - this will will check if guess equals randomNumber

and also in the else statement else you wrote alert ("...", randomnumber) - your var is randomNumber not randomnumber (Capital 'N')

hope this is works for you :)

Hello, thanks for replying,

this fixed the issue! thanks :)

i appreciate it!

// Erdrag