-3
var num=1234

My answer should be 4 how do i do that? I tried using "split" and "toString" but I don't know how to use it.

Mike
  • 67
  • 2
  • 8
  • 3
    Possible duplicate of [How do I separate an integer into separate digits in an array in JavaScript?](http://stackoverflow.com/questions/9914216/how-do-i-separate-an-integer-into-separate-digits-in-an-array-in-javascript) – Jack jdeoel May 12 '16 at 03:21

3 Answers3

1

Do you need to use toString and split? Because the most efficient way is num % 10

voll
  • 195
  • 8
1

Working Example

var lastNum = num.toString().split('').pop();
omarjmh
  • 13,632
  • 6
  • 34
  • 42
1

Try this

var num = 1234;
var stringNum = num.toString();
var arrayNum = stringNum.split("");
var lastNumber = arrayNum[arrayNum.length-1];
  • – Mike May 12 '16 at 03:26
  • "My answer should be 4 how do i do that? I tried using "split" and "toString" but I don't know how to use it." that is what he want – Lautaro Bertuzzi May 12 '16 at 03:29