Guess the number | Sololearn: Learn to code for FREE!

0

Guess the number

Hi devs, i'm trying to make a simple game in JS that It generates a number from 1 to 10 and you have to hit it as fast as possible. Example: generated the number 7, if you typed 2, then it will appear written that the number is higher. Thanks

7/24/2017 2:03:05 PM

Leonardo Junio

8 Answers

New Answer

+3

This is where flowcharts come in handy. If you look up how to create a flowchart, you can map out the flow of your program and basically create a blueprint that you can use to guide you while you're programming the game. Once you understand the flow of what you're wanting, it's as simple as filling in the blanks. In this case, you'll just utilize something like a text input box, a button to start the game only after you inputted your guess, and then a variable that gets displayed based upon a random number from 1-10. It's actually really simple to do, so don't give up! :) If you run into problems, such as maybe not knowing how to do random number in JS, then google it and read the language references for it (not considering the ample amount of resources online for it). This habit will make it easier for to you find out what you need to know quicker and learn from it all. Best of luck!

+3

You're welcome. Hope the advice helps out now and later for you. Do you have your code? Post it up for us and I'm sure Faith or myself can help you out with it easily.

+2

You can use Math.random() for the random number. This will generate a random decimal from (inclusive)0-1(exclusive). So, with some arithmetic we can make it from 1-10: Math.random() * 10 + 1; // 1-10 *Note you will need to round this down* Now you need some way to take the user input. This can be done many ways, I assume you want it to be on the html page. For that, one option would be the input tag. https://www.w3schools.com/tags/tag_input.asp You can display the output of whether or not its higher or lower however you'd like. 👍

+1

That code only runs once, so you need to loop it somehow. Perhaps add a loop to keep asking the user for more input, that way he can keep guessing the number. The answer would need to be initialized outside the loop, that way it won't change as you keep guessing. Also, your if statements are backwards. if(number < answer) Keep in mind the way you made the variable names. answer is what the user guessed. number is the actual random number. So, if statement would actually mean your number is to high. Here's a code with those fixes. I added 'numTries' so the loop doesn't go infinitely. var numTries = 0; var number = Math.round(Math.random()*100); // show the answer confirm(number); do{ var answer = Number(prompt("Guess a number between 1 and 100")); if(number == answer){ confirm("Correct!"); break; } else if(number < answer){ confirm("Your number was too high!"); } else if (number > answer) { confirm("Your number was too low!"); } }while(numTries++ < 10);

0

Thank you all of you. Yesterday I tried, but could not store the random number until I hit it.

0

Here is my code, friend. var answer = prompt("Guess a number between 1 and 100"); var number = confirm(Math.round(Math.random()*100)); answer=parseInt(answer); if (number === answer) { confirm("You win!"); } else if(number < answer){ confirm("Your number was too low!"); } else if (number > answer) { confirm("Your number was too high!"); } Have some problems: 1- every time that i open The Page, ask to input the number. And if is wrong, The number change. 2- i want to print the numbers I tried so they would not be repeated. 3-I do not want the random number to change until I hit it Your help is being warmly welcomed. Thank you so.

0

Thanks for the help, now I can store the random number. But instead of appearing as alert, I would like you to have an input type number to enter the number and if it is right or correct, write on the page.

0

This is my code. But instead of always being on the alert, I'd like it to be written on the page and take the number from what I write in my input. <!DOCTYPE html> <html> <title>Guess the number</title> <script> var numTries = 0; var number = Math.round(Math.random()*10); // show the answer confirm(number); do{ var answer = Number(prompt("Guess a number between 1 and 10")); if(number == answer){ confirm("Correct!"); break; } else if(number < answer){ confirm("Your number was too high!"); } else if (number > answer) { confirm("Your number was too low!"); } }while(numTries++ < 10); //button function myFunction() { var x = document.getElementById("myNumber").value; document.getElementById("demo").innerHTML = x; } </script> <body> <h1>Descubra o número!</h1> <input type="number" id="myNumber" value=""> <button onclick="myFunction()">Try it</button> <p id="demo"></p> </body> </html>