Math.random() Method and trunc() Function

Burak Karaben
2 min readFeb 26, 2022

Today, I would like to mention the last lesson of my JavaScript journey. I have been studying the third chapter until today and I learnt arrays, objects and methods respectively. Since it will be a long journey for me, I tried to endeavour to code everything that I saw in Jonas Schmedtmann’s lessons.

I coded a simple game today and it was too easy to code but funny at the same time. Inside the code, I used the Math.random() method to create a random number between zero and one, however, this method constitutes not only integer values but also the ones with the floating-point ones. Hence by adding the trunc() function at the beginning of the Math.random() method, I got rid of the floating-point values.

After the implementation of these codes, I got the random value between zero and one as an integer. Now all I have to do is multiply this value by six and add one in order to imitate the behaviour of the dice. Lets see our code in gist:

We have to write “dice” inside the prompt window and if our dice is greater than three the program will write “Since your dice is ${dice} and greater than 3 you won.”

--

--

Burak Karaben

Mostly historical contents but sometimes music, web design and diary