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?
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]
If you're sorting integers just use the .sort() method and it will sort them alphabetically
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]
omg !
wait, how do i get the user to input multiple numbers using an array?
var hi1 = new Array () will that work?
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
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-.....-
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?
got that part. var multi1 = []; multi1 = window.prompt( "Enter any variables as you wish." );
um, do you think itd b possible for you to briefly tell me about .push() method :( !?
if you had var multi1 = [1,2]; multi1.push(3); It would push the value 3 into the array multi1
but the user is inputting many integers into the array multi1 how can i store all the integers and return it to the user?
Do you know how many?
i think it can be any... but preferably alot lol !
if you set it up var multi1 = [ ]; wouldnt it intake multiple variables/integers????
If you typed them there
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?
But it would only run once. One sec I'm writing an example program
It only takes numbers? Or does it take strings too?
is it easier to take only numbers? but doesn't it take string values first, then dont i have to convert it into integers?
Yes if one inputted a number it would by "3" not the number 3 so it would be easier to take only numbers
it's string "3" right? i just store string variables. then return to the user the "inputted string values" right?
You could
and if i document.writeln( multi1 ); would it return all of the inputted string values?
It should, yes
it does not work :' (
it stored 1 value!
is there a way for me to store it one by one by letting the user press enter?
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);
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
THANK YU!
Yup :)
Join our real-time social learning platform and learn together with your friends!