I cannot figure out how to store multiple integers in an array from one prompt
like
const prompt = require('prompt-sync')();
num_array = []
num_array.push(prompt("Enter x amount of integers: "));
console.log(num_array)
in the console, it asks:
Enter x amount of integers:
so i enter: 7 6 5 9 21 4 6 99 820
when the console logs the array, instead of it showing: ‘7 6 5 9 21 4 6 99 820’
I wanna know if it’s possible for it to show: ‘7’, ‘6’, ‘5’, ‘9’, ’21’, ‘4’, ‘6’, ’99’, ‘820’
4
Answers
Someone solved it and their solution wasn't too confusing for me.
My code:
My code after their comment:
I tried it and It works
the person who solved it is @HenryWoody
You could use the
split()
function to split the input string based on a space, or comma, your choice. And then you could useparseInt()
to convert the substrings into integers.Try this:
prompt-sync
does not automatically split the input string into an array of numbers. We have to do it manually. Something like:Try to split the input into individual integers: