Reputation: 1707
I'm getting the following error on browser console
Uncaught TypeError: rand.slice is not a function
JavaScript
var rand, date;
date = Date.now();
rand = Math.random() * Math.random();
rand = Math.floor(date * rand);
rand = rand.slice(-5);
document.getElementById("test").innerHTML = rand;
I'm not able to figure it out what's wrong with this code.
Upvotes: 20
Views: 76406
Reputation: 1785
try:
rand = (rand+'').slice(-5);
This will generate random number that will be converted to string by combining it with +'' and then we can call slice function
Upvotes: 4
Reputation: 752
Try using rand = rand.toString().slice(-5);
instead of rand = rand.slice(-5);
slice
can be used either on array
or string
and not on numbers.
Upvotes: 12
Reputation: 4525
you can't directly slice a number.. you can convert to string first then slice
after
like this:
rand = rand.toString().slice(-5)
JavaScript Array slice() Method
JavaScript String slice() method
Upvotes: 48