Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Roland O'Donnell
5,667 PointsParseInt(prompt("enter six numbers")); returning multiple integers
how can i return multiple strings as integers? so if i prompt the user to enter 6 numbers how can i convert them to 6 integers without 6 different prompts.
thanks
2 Answers
lloan alas
17,774 PointsRoland O'Donnell
This seems to work on my end:
var x = parseInt(prompt("Enter Six Numbers"),0);
console.log(typeof x); // check the type of x to see if its number
console.log(x+x); // check if you can use the number returned
Jason Anello
Courses Plus Student 94,610 PointsHi Roland,
You can prompt the user to enter each number separated by a space.
The input would look something like this "1 2 3 4 5 6"
Then you could call the split method on that, splitting around the spaces, and get an array of strings. ["1", "2", "3", "4", "5", "6"]
Then you could call the map method on this array to create a new array with all the strings converted to integers.
Here's the code:
var input = prompt("Enter 6 numbers each separated by a space");
var integers = input.split(" ").map(Number);
console.log(integers); // Ex. output [1, 2, 3, 4, 5, 6]
The map method accepts a callback. In this case, the Number() object is used as the callback. It takes each string and passes it to the Number() object which converts the string to an integer and saves that into a new array.
Let me know if you have any questions about this or if I misunderstood what you were trying to accomplish.