4

When I type a number with prompt var myNumber = parseInt(prompt("..."))I want it to be converted into an array of numbers. When I try with myNumber = myNumber.split("") it returns:

Object 1 has no method 'split'

help me please

3
  • 2
    What are you using to delimit the numbers in the prompt? You can't split a number, you split strings. Commented Dec 22, 2013 at 14:21
  • Why don't you convert to number after split? Commented Dec 22, 2013 at 14:22
  • I didn't know that i can't split() a number. Commented Dec 22, 2013 at 14:23

4 Answers 4

3

You cannot split the int, you need to have a string data type for this code to work!

So, I would like to suggest to first split it and then convert it to int as

var numbers = "1, 2, 3";
var eachNumber = numbers.split(",");
/* now parse them or whatso ever */

This will work, as you're just splitting the string. And then you will parse it just the way you did it in the first method (of yours).

Sign up to request clarification or add additional context in comments.

2 Comments

i should have thought about making another var where I split the firts var. thank you Afzaal!
yes, you could have done that! :) However, you're most welcome .. :)
2
var intArray = prompt("...").split(" ").map(Number);

1 Comment

You can shorten your map by simply writing .map(Number), try it out
1

You would need to change the number to string, then split it.

You would then want to return it to an integer.

var myNumber = parseInt(prompt("Enter your number"));
var tempString = myNumber + "";
var arr = tempString.split("");
console.log(arr);

Here is a fiddle - http://jsfiddle.net/kellyjandrews/Wa5zD/

Comments

0

myNumber is an integer variable, which does not hold .split() method, it belongs to string variable.

var no = "12 13 14 15";
var noV = no.split(" ")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.