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 gr
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
Just 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);
<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?
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