Generating numbers between 1- 6

Hi I am new to javascript, can you guys help me with the code . Here is the question
Write Javascript code that does the following :

A) Generates a random number in the range of 1 to 6 then store it within a variable.
B) The program should prompt the user to enter a number from in the range of 1 to 6.q
C) If the number entered by the user does not match the number generated in point A
display a message inside the browser to encourage the user try one more time.
Otherwise congratulate the user.

PS: Below is an example on how to prompt the user for input

Var name = prompt(“Please enter your name”);

Here is my code below:

    <title>Brief 2 _Question 3 </title>
     var generate1 = Math.floor(Math.random() * 7);  

     var ask_user = prompt("Enter the number between 1-6");

      if (generate1 != ask_user){
      var message = alert("Try one more time");
        message = alert("Congratulations");


I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


First of all, need to check your math. You are using

Math.floor(Math.random() * 7)

Because Math.random() will return a number between 0 and .99999999999, your formula will return a number between 0 and 6.9999999999. When that gets rounded down, it will give a number from 0-6.

I don’t want to give it away, but hopefully that will point you in the right direction.

As to your loop, I think you need to have the prompt inside the loop, so you can keep checking. Maybe use a do/while loop so you can check after if there was a match.

1 Like