Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: rand.slice is not a function

Tags:

javascript

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.

like image 648
Red Virus Avatar asked May 16 '15 07:05

Red Virus


1 Answers

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

like image 123
Dyrandz Famador Avatar answered Oct 12 '22 17:10

Dyrandz Famador