At times in your javascript project, you may need to generate a random number. Usually, the tutorials show how to generate a number between 0 to 1. But what if, for example, you want to generate integers between 355 to 485 (both inclusive). Javascript’s Math.random() will generate numbers between 0 (inclusive) to 1(exclusive). One interesting hack could be as follows:-

cosnt getRandomIntInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}

The above arrow function takes the minimum and maximum as the argument and returns a number in between. Now, you can make your very own “Guess the number” game. Cheers!!!

--

--

It happens sometimes that you try to use a string from an API or from user input but the results contain unwanted white space that causes issues with your code. This is an easy way to fix the issue. The Javascript build-in method trim() allows you to do just that :).

const greeting = '   I don't have white space around!   ';console.log(greeting);
// expected output: " I don't have white space around! ";
console.log(greeting.trim());
// expected output: "I don't have white space around!";

Enjoy!!!

--

--