Hi every one .
I have a problem in my random quote machine and Math.random()
.
I have used Math.random()
function . But the problem is, I have taken 15 objects which containing my quotes. but the Math.random
function sometimes generates same number one after one ,that means same quote generating twice . here is my codepen pen . how can I solve this ?
Hi Shoeb,
I think you can save the index of the last quote in a cookie (https://www.w3schools.com/js/js_cookies.asp)
Then after running Math.random() you check if it is repeating the last one and run Math.random() again to select a new one.
Btw, I’m not sure why you use: Math.floor((Math.random() *8))+Math.floor((Math.random() *7))
instead of Math.floor(Math.random() * 15)
Also, I think you should use Math.ceil() instead of Math.floor() cause your array has indexes from 1 to 15, with Math.floor() you will be generating numbers from 0 to 14 since you round the numbers downwards (https://www.w3schools.com/jsref/jsref_floor.asp)
hello @piggito . thanks for your complement .
Math.floor((Math.random() *8))+Math.floor((Math.random() *7))
I used that for generating random numbers below 15 . and hope that the sum of two random number will random to fix my problem . but still problem remains . one quote generates twice sometimes.