1

I am doing a JavaScript, trying to retrieve the user's numerical input, and store the input's digits as an array.

I managed to retrieve the user's input using DOM, but, how can I store the digits into an array?

2
  • Why not post the code you have, and an example of what output you want ? Commented Jul 13, 2017 at 9:42
  • Either input.split() or input.split(",") depending on what your input is Commented Jul 13, 2017 at 9:44

4 Answers 4

2

If you want to obtain an array of numbers, one solution is to use Array.from method and map array items to numbers.

let number=1234;
let array=Array.from(number.toString()).map(Number);
console.log(array);

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

Comments

1

If you want to store singular digits, you could use .split('')

userInput = '42';

console.log(userInput.split(''));

1 Comment

userInput is not defined
1

what you can do here is simply:

userInput1 = '42';
userInput2 = '43';
const inputArray = [userInput1, userInput2]

1 Comment

userInput is not defined
0

For an array of number:

var input = "123456789";
var array = input.split('')
for(var i = 0; i <array.length; i++) { array[i] = +array[i]; } 
console.log(array)

The operation +array[i] is just a quick way to do the number conversion.

If you want the array of chars remove the for loop from the code

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.