Signup/Sign In
PUBLISHED ON: JANUARY 19, 2023

JavaScript Program to Guess a Random Number

In this tutorial, we'll make a JavaScript program that generates a random number and asks the user to guess it. Until the user correctly guesses the number, the program will provide feedback on their guess, letting them know if it's too high or too low. We will generate the random number using JavaScript's built-in Math.random() function, and we will check the user's guesses using while loops and conditional statements.

This program is a great way to learn about basic programming concepts in JavaScript, such as variables, functions, and control flow. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

Math.random()

Math.random() is a JavaScript built-in function that generates a random number between 0 (inclusive) and 1 (exclusive) (exclusive). The returned value is a floating-point number in the range [0, 1], which means that it is greater than or equal to 0 but less than 1.

let randomNumber = Math.random();
console.log(randomNumber);

This will generate a number between 0 and 1 at random and log it to the console. You can use Math.random() in conjunction with other mathematical operations to generate a random number within a specific range.

Approach

The user is asked to guess a number from 1 to 10.

  • The parseInt() will convert the numeric string value to an integer value.
  • The while loop is used to take inputs from the user until he/she is able to guess a right answer.
  • The if...else statement is used to check the correctness of the guess.

Program to Guess a Random Number

// JavaScript program to guess a number generated by the user.

function guessNum() {

    // generating a random integer from 1 to 10
    let rand = Math.floor(Math.random() * 10);
    rand+=1;
    
    const num = parseInt(prompt("Guess a number from 1 to 10: "));

    while(num !== rand) {
        num = parseInt(prompt(" Guess a number from 1 to 10: "));
    }

    if(num == rand) 
        console.log("You guessed the correct number.");
    
 }

guessNum();


Guess a number from 1 to 10: 1
Guess a number from 1 to 10: 8
Guess a number from 1 to 10: 2
Guess a number from 1 to 10: 3
Guess a number from 1 to 10: 4
You guessed the correct number.

Conclusion

Finally, we developed a simple but effective JavaScript program that generates a random number and prompts the user to guess it. We were able to check the user's guesses and provide feedback on whether their guess was too high or too low by utilizing JavaScript's built-in Math.random() function, while loops, and conditional statements.

This program is an excellent introduction to some fundamental programming concepts in JavaScript, such as variables, functions, and control flow. With this program's knowledge, you can now apply these concepts to create more complex programs and projects.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.