Prompting the user for 10 inputs, sotring it in an array, finding the average, and printing numbers less than average

StackOverflow https://stackoverflow.com/questions/23366099

The title is the question in mind.

Here is what I have so far:

html>
 <head>
  <title> New Document </title>
 </head>
 <script type = "text/javascript">
 var i;
var elmt = nums[10];
var ask = prompt("Please enter 10 numbers");
var sum = 0
for(var i =0, i< elmt.length; i++;):
    sum += parseInt(elmt[i], 10);

var avg = sum/elmt.length;

 document.write("The sum of all the elements is: " + sum + " The average is: " +avg);

 <body>

 </body>
</html>

I'm not really sure what I'm doing to be honest. I tried researching arrays and this questions and similar ones so I can try to learn the syntax but to no avail. I'm practicing this on my own to try and learn. Please don't bash, I'm not asking anyone to do this FOR me, hints would be ok. I'm not some teenager endlessly dumping in code and hoping the internet does my homework.

EDIT1:

<html>
 <head>
  <title> nums </title>
 </head>
 <script type = "text/javascript">
var sum =0;
var belowAvg = 0;
var num = new Array(10);
var sum = 0
var i;

for(i = 0; i<10; i++;):
{
    num[i] = eval(prompt("Enter a number"));
    sum = sum + num[i];
}

avg = sum/10;
for (i=0; i<10; i++)
{
    if ( num[i] < avg)
    {
        belowAvg++;
    }
}

alert ("average is" + avg);
alert (belowAvg + "numbers are less than the average");

</script>

 <body>

 </body>
</html>

EDIT2:

Ok, now I'm extremely lost. Everyone had good answers and all but my understanding isn't enough to apply any of them unfortunately. I've been playing around with all suggestions and I'm still lost. Can anyone show me an example maybe? I didn't want my hand to be held, but....

有帮助吗?

解决方案

You're almost there, but you're only prompting once and placing the input in to a variable. You need to prompt 10 times if you want 10 numbers (otherwise you need to parse the input and extract 10 numbers from it).

Assuming you wanted a few methods of populating elmt through ask (here i use input instead of ask and nums instead of elmt--but we're talking minor semantics):

Method one, prompt 10 times:

// initialize array with 10 elements
var nums = new Array(10);
// iterate over each element and prompt for value
for (var i = 0; i < nums.length; i++){
  var input = prompt('Enter number ' + (i + 1) + ' of ' + (nums.length + 1));
  // place value (as a number) into the array
  nums[i] = new Number(input);
}
/* sum/average code */

Method two, prompt once and String.prototype.split:

// initialize array with 10 elements
var nums = new Array(10);
// prompt for 10 numbers
var input = prompt('Enter ' + (nums.length + 1) + ' numbers separated by commas:');
var values = input.split(',');
// check we have the right amount
if (values.length == nums.length){
  // iterate over the values
  for (var i = 0; i < nums.length; i++){
    // place the input (as a number) into the array
    nums[i] = new Number(values[i]);
  }
}
/* sum/average code */

Method three, input boxes:

<script>
  function average(){
    // initialize array with 10 elements
    var nums = new Array(10);
    // iterate over the elements
    for (var i = 0; i < nums.length; i++){
      // grab <input> based on id attribute
      var input = document.getElementById('num' + i);
      // insert the value (cast as a number) in to the array
      nums[i] = new Number(input.value); // case to number
    }
    /* sum/average code */
  }
</script>

<p>Enter 10 numbers:</p>
<input type="text" id="num0" />
<input type="text" id="num1" />
<input type="text" id="num2" />
<input type="text" id="num3" />
<input type="text" id="num4" />
<input type="text" id="num5" />
<input type="text" id="num6" />
<input type="text" id="num7" />
<input type="text" id="num8" />
<input type="text" id="num9" />

<button onclick="average()">Click to average</button>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top