Challenge

Im doing really well, I just need a little help now. I have it so the computer makes a number, and it prompts the user the question, and it says if its right, or if you need to guess higher or lower. I need it to prompt again. Guidance please.
button to start game ----> Round

function round() {
var CN = Math.random();
  CN = CN * 10;
  var CN = Math.round(CN);
  console.log(CN);
  document.write('Computer has made a number.');

 var PG = prompt("Pick a number 1 - 10");
  if (PG == CN) {
   
    document.write('You got it right!');
  }
  else if (PG < CN) {
    document.write('Guess higher.');

  }
  else {
    document.write('Guess lower.');
  
  }
};
1 Like

How about a for loop ? Have you done the unit 3 of the javascript course on codecademy? it teaches you how to use for loops

1 Like

About organizing your code and changing those multiple if/elseif/else statements to a switch statement?

1 Like

made it to this. It will say if you need to go higher or lower and tell you if its correct, but it won’t stop if you get it right. help?

function round() {
var CN = Math.random();
  CN = CN * 10;
  var CN = Math.round(CN);
  console.log(CN);
  document.write('Computer has made a number.');
for (guess = false;guess = true;guess = false) {
 var PG = prompt("Pick a number 1 - 10");
  if (PG == CN) {
   
    document.write('You got it right!');
    guess = true;
  }
  else if (PG < CN) {
    document.write('Guess higher.');

  }
  else {
    document.write('Guess lower.');
  }
  }
};
1 Like

its because the for loop will change it back to false, and its set to stop at true. How do i fix this?

1 Like

Remember what var does?

That’s not how we do for loops in JavaScript.

Usually we use for loops when you know how many loops we want to make - we would do something like:

for (var i = 0; i <= 5; i++) {

Since you have no idea how many guesses it will take your player, that makes this a good place for a while loop – you want to keep looping while guess is false.

I bet you can find the syntax for a while loop easily enough.

2 Likes

made it to this, need some help now.

function round() {
var CN = Math.random();
  CN = CN * 10;
  var CN = Math.round(CN);
  console.log(CN);
  document.write('Computer has made a number.');
do {
 var PG = prompt("Pick a number 1 - 10");
  if (PG == CN) {
   
    document.write('You got it right!');
    guess = true;
  }
  else if (PG < CN) {
    document.write('Guess higher.');
    guess = false;
  }
  else {
    document.write('Guess lower.');
    guess = false;
  }
}
  while (guess = true);
 };
1 Like

If you don’t already know, you should learn about the difference between =, ==, and ===. I’m saying that because you have a misused = there.

After that, you should investigate the use of parseInt.

2 Likes

can you please specify which ones I have misused or provide a link to somewhere I can find out?

1 Like
while (guess = true);

MDN: Expressions and Operators

= is an assignment operator (sets a value)
==, === are comparison operators (compares values)

2 Likes

Is this the only mistake like this?

1 Like

Yes, but if you aren’t sure about any then you should specifically ask so we can talk you through the difference between setting a value and comparing values.

1 Like