2

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.

2
  • how are the numbers split up? is it 10 different numbers in the input separated by commas or 10 different inputs? Commented Jan 31, 2015 at 14:50
  • How about you take your current markup and code and put it into a JSFiddle (jsfiddle.net). Commented Jan 31, 2015 at 14:51

5 Answers 5

7

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1
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

1 Comment

Add some explanation with answer for how this answer help OP in fixing current issue
0

<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?

Comments

0
  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();

2 Comments

You can avoid the second loop by moving parseInt() bit into the first one.
Please don't post code-only answers but add a little textual explanation about how and why your approach works and what makes it different from the other answers given. You may also have a look at our "How to write a good answer" entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.