Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

In javascript: Let the user input multiple integers/variables. then return them in order and least to greatest. Is there any similar way alike to using infile/outfile from java in javascript?

OpenStudy (hunus):

If you have your numbers in an array you can use the .sort(function(a,b){return a-b}); like var myArray = [10, 7, 4, 9, 0] myArray.sort(function(a,b){return a-b}); will return the array [0, 4, 7, 9, 10]

OpenStudy (hunus):

If you're sorting integers just use the .sort() method and it will sort them alphabetically

OpenStudy (hunus):

Also, if you only use the .sort() method on integers, it will sort them by their first digit var myArray = [1, 2, 11, 112, 23, 43] myArray.sort() would return the array with [1, 11, 112, 2, 23, 43]

OpenStudy (anonymous):

omg !

OpenStudy (anonymous):

wait, how do i get the user to input multiple numbers using an array?

OpenStudy (anonymous):

var hi1 = new Array () will that work?

OpenStudy (hunus):

It should, yes. You could just write var hi1 = [ ]; and then write a loop that takes user input and use the .push() method to push the user input into an array

OpenStudy (anonymous):

this is so not what i have learned from chp.7 and 8. i don't know why my teacher keeps on giving me hw that has nothing to do with what i have learned-.....-

OpenStudy (anonymous):

var multi1 = []; var multi1 = window.prompt( "Enter any variables as you wish." ); do i connect the prompt like this? how do prompt the user to store the integers into multi1?

OpenStudy (anonymous):

got that part. var multi1 = []; multi1 = window.prompt( "Enter any variables as you wish." );

OpenStudy (anonymous):

um, do you think itd b possible for you to briefly tell me about .push() method :( !?

OpenStudy (hunus):

if you had var multi1 = [1,2]; multi1.push(3); It would push the value 3 into the array multi1

OpenStudy (anonymous):

but the user is inputting many integers into the array multi1 how can i store all the integers and return it to the user?

OpenStudy (hunus):

Do you know how many?

OpenStudy (anonymous):

i think it can be any... but preferably alot lol !

OpenStudy (anonymous):

if you set it up var multi1 = [ ]; wouldnt it intake multiple variables/integers????

OpenStudy (hunus):

If you typed them there

OpenStudy (anonymous):

var multi1 = []; multi1 = window.prompt( "Enter any variables as you wish." ); so this would prompt the user to enter variables into multi1 and the array mutli1 would store multiple variables correct?

OpenStudy (hunus):

But it would only run once. One sec I'm writing an example program

OpenStudy (hunus):

It only takes numbers? Or does it take strings too?

OpenStudy (anonymous):

is it easier to take only numbers? but doesn't it take string values first, then dont i have to convert it into integers?

OpenStudy (hunus):

Yes if one inputted a number it would by "3" not the number 3 so it would be easier to take only numbers

OpenStudy (anonymous):

it's string "3" right? i just store string variables. then return to the user the "inputted string values" right?

OpenStudy (hunus):

You could

OpenStudy (anonymous):

and if i document.writeln( multi1 ); would it return all of the inputted string values?

OpenStudy (hunus):

It should, yes

OpenStudy (anonymous):

it does not work :' (

OpenStudy (anonymous):

it stored 1 value!

OpenStudy (anonymous):

is there a way for me to store it one by one by letting the user press enter?

OpenStudy (hunus):

var h1 = []; var bool = true; while(bool === true){ var input = prompt("Enter anything you like"); h1.push(input); var end = prompt("Are you done? y/n"); if(end === "y"){ bool = false; }else{ continue; } } console.log(h1);

OpenStudy (hunus):

I have to head out now, but I'll be on later. That should be what you're looking for. Just tweak it until it's right

OpenStudy (anonymous):

THANK YU!

OpenStudy (hunus):

Yup :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!