Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  90] [ 3]  / answers: 1 / hits: 70012  / 9 Years ago, sat, may 16, 2015, 12:00:00

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.


More From » javascript

 Answers
4

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


[#66585] Thursday, May 14, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ishmaelw

Total Points: 528
Total Questions: 96
Total Answers: 103

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
;