I`m having some issue with Javascript. We just started to study it a couple weeks ago and I have to do a work for class:
Need to do a prompt. get 10 numbers input (10 grades) from the user. put the numbers into an array and then do some functions with it.
My question is how do I save the input in the array? We learned about all the loops already. Tried to search online but didn`t found an answer.
I would love if somebody could explain how I do it. Thank you very much.
I`m having some issue with Javascript. We just started to study it a couple weeks ago and I have to do a work for class:
Need to do a prompt. get 10 numbers input (10 grades) from the user. put the numbers into an array and then do some functions with it.
My question is how do I save the input in the array? We learned about all the loops already. Tried to search online but didn`t found an answer.
I would love if somebody could explain how I do it. Thank you very much.
Share Improve this question edited Jul 28, 2022 at 15:10 Dave Newton 160k27 gold badges258 silver badges307 bronze badges asked Jan 31, 2015 at 14:49 Peter TothPeter Toth 291 gold badge1 silver badge7 bronze badges 2- how are the numbers split up? is it 10 different numbers in the input separated by commas or 10 different inputs? – Avi Mosseri Commented Jan 31, 2015 at 14:50
- How about you take your current markup and code and put it into a JSFiddle (jsfiddle.net). – Jeff Watkins Commented Jan 31, 2015 at 14:51
5 Answers
Reset to default 7Just try to ask them to input their numbers or grade, separated by a comma and then you can split on it.
var arr = prompt("Enter your numbers").split(",")
Or, ask prompt
ten times
var arr = [];
for(var i = 0; i < 10; i++)
arr.push(prompt("Enter a number");
If you want them to be numbers, just prefix prompt
with +
, so it becomes a number(provided they're actual numbers) or just do
arr = arr.map(Number);
See the explanations in comments:
var arr = []; // define our array
for (var i = 0; i < 10; i++) { // loop 10 times
arr.push(prompt('Enter grade ' + (i+1))); // push the value into the array
}
alert('Full array: ' + arr.join(', ')); // alert the results
NUMBER_OF_INPUTS = 10;
var i = 0; // Loop iterator
var userInput; // Input from user
sum = 0; //initialise sum
// Collect inputs
for(i=0; i<NUMBER_OF_INPUTS; i++)
{ userInput = parseInt(prompt('Enter input '+(i+1)+' of '+NUMBER_OF_INPUTS));
sum += userInput;
sum /= NUMBER_OF_INPUTS;
}
// Output the average
alert('Average grade: '+ sum.toFixed(2)); //the .toFixed sets it to 2 decimal places
<script>
var grades = [];
var i;
for (i = 0; i < 10; i++) {
grades.push(Number(prompt("Enter your grades:" + (i + 1), "0-100")));
}
document.write("Your grades: " + grades);
</script>
Ok so I made this one. The user can enter 10 different numbers to the array and I can display them. Now - I need to caculate the avarage of the numbers and get the highest number.
I would like to have some help withj it, how can I make it?
function myfunction() {
let arr = [];
for (i = 0; i < 5; i++) {
arr.push(prompt());
}
for (i = 0; i < arr.length; i++) {
arr[i] = parseInt(arr[i]);
}
console.log(arr);
}
myfunction();