w3resource

JavaScript: Display a message when a number is between a range

JavaScript Basic: Exercise-8 with Solution

Write a JavaScript program where the program takes a random integer between 1 to 10, the user is then prompted to input a guess number. If the user input matches with guess number, the program will display a message "Good Work" otherwise display a message "Not matched"

Sample Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Guess a number</title>
</head>
<body>
</body>
</html>

JavaScript Code:

// Get a random integer from 1 to 10 inclusive
 const num = Math.ceil(Math.random() * 10);
console.log(num);
 const gnum = prompt('Guess the number between 1 and 10 inclusive');
 if (gnum == num)
   console.log('Matched');
  else
   console.log('Not matched, the number was '+gnum);
   

Sample Output:

Matched

Explanation:

The Math.ceil() function is used to get the smallest integer greater than or equal to a given number.

The Math.random() function is used to get a floating-point, pseudo-random number in the range [0, 1] that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Flowchart:

Flowchart: JavaScript - Display a message when a number is between a range

ES6 Version:

// Get a random integer from 1 to 10 inclusive
var num = Math.ceil(Math.random() * 10);
console.log(num);
var gnum = prompt('Guess the number between 1 and 10 inclusive');
if (gnum == num) console.log('Matched');else console.log('Not matched, the number was ' + gnum);

Live Demo:

See the Pen JavaScript basic animation - basic-ex-8 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to find which 1st January is being a Sunday between 2014 and 2050.
Next: Write a JavaScript program to calculate number of days left until next Christmas.

What is the difficulty level of this exercise?



JavaScript: Tips of the Day

Promise object

const myPromise = () => Promise.resolve('I have resolved!');

function firstFunction() {
  myPromise().then(res => console.log(res));
  console.log('second');
}

async function secondFunction() {
  console.log(await myPromise());
  console.log('second');
}

firstFunction();
secondFunction();

With a promise, we basically say I want to execute this function, but I'll put it aside for now while it's running since this might take a while. Only when a certain value is resolved (or rejected), and when the call stack is empty, I want to use this value.
We can get this value with both .then and the await keyword in an async function. Although we can get a promise's value with both .then and await, they work a bit differently.
In the firstFunction, we (sort of) put the myPromise function aside while it was running, but continued running the other code, which is console.log('second') in this case. Then, the function resolved with the string I have resolved, which then got logged after it saw that the callstack was empty.
With the await keyword in secondFunction, we literally pause the execution of an async function until the value has been resolved before moving to the next line.
This means that it waited for the myPromise to resolve with the value I have resolved, and only once that happened, we moved to the next line: second got logged.

Ref: https://bit.ly/3jFRBje