Making a guessing game _ help() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Making a guessing game _ help()

I'm trying to make a guessing game. - The first red background Is your guess (there' an input to enter number 0 - 10. - The green background is what you got to match to win - The 2nd (last) red background is the computer's guess. So, on the first red, enter a number (0 - 10), and click roll on the green screen. If your number matches the number In the green, you win. Else If the compGuess matches the number on the green, Computer win. The problem: You / computer win even when the Winning # > ur guess. https://code.sololearn.com/WScSKZg5SJB7/?ref=app

5th Oct 2019, 3:30 AM
Ginfio
Ginfio - avatar
5 Answers
+ 2
1. You need to add a clear result script. If both user guess and computer guess doesn't match the randomed winning number, update the result to "No one wins this round". 2. You should use Math.ceil() instead of Math.floor, because now you can have 0 as winning number, but you are using == instead of === without type comparison. So when user doesn't input anything, JS will interpret both "" (empty string) and 0 (number) as false and returns "You win"
5th Oct 2019, 4:34 AM
Gordon
Gordon - avatar
+ 2
Diego Got that. line 34. It is generating # 0 - 9. I'm not sure what u r seeing on your screen. Do do you know how to limit the numbers greater than 0 less than 10 (1-9)?
5th Oct 2019, 4:44 AM
Ginfio
Ginfio - avatar
+ 2
Ginfio It is generating a number between 1 (inclusive) and 8 (inclusive), try it out a couple of times and see for yourself. To get a number between 1 (inclusive) and 9 (inclusive) use: Math.floor(Math.random()*9)+1;
5th Oct 2019, 4:58 AM
Diego
Diego - avatar
+ 1
[continued]: The problem: for example: if your guess is 5, and when you click roll, if the winning number (the nimber on the green screen) is 6, you still win. I don't understand how. Same thing for the computer guess. Also, it's not 0-10, it's 0-9.
5th Oct 2019, 3:35 AM
Ginfio
Ginfio - avatar
+ 1
1. You're changing the Computer's guess on each roll (line 34), so the number displayed isn't actually the guessed number. Just comment/remove line 34: // cNG = Math.floor(Math.random()*9); 2. Your code actually returns a number between 0 (inclusive) and 8 (inclusive). Change all Math.floor(Math.random()*9); to Math.floor(Math.random()*11);
5th Oct 2019, 4:34 AM
Diego
Diego - avatar