Medium Javascript Array

This is my first post. I am writing a program to get input from four input fields, find out the sum of these four and find the average value. When I do this, I get a NaN error, can someone indicate where I am wrong. Thanks

<html> <head> <title> Average marks </title> <script type = "text/javascript"> function average(form) { scores = new Array(4) scores [0] = form.mark1.value scores [0] = new Number(scores[0]) scores [1] = form.mark2.value scores [1] = new Number(scores[1]) scores [2] = form.mark3.value scores [2] = new Number(scores[2]) scores [3] = form.mark4.value scores [3] = new Number(scores[3]) var Sum = 0 var average for(var x = 0; x < scores.length; x ++) { Sum = Sum + scores[x] average = Sum / scores[x] } document.write("The sum of the marks is equal to " + Sum + "<br>") document.write("The average of these marks is equal to " + average + "<br>") } </script> </head> <body> <form> Enter the first mark : <input type = "text" name="mark1"> <br> Enter the second mark : <input type = "text" name="mark2"> <br> Enter the third mark : <input type = "text" name="mark3"> <br> Enter the fourth mark : <input type = "text" name="mark4"> <br> <input type = "submit" value = "submit" onclick="average(this.form)"> </form> </body> </html> 
+4
source share
3 answers

Welcome to Stackoverflow :) We will be happy to help you by learning our tools better. Only one note about the algorithm: move the middle calculation command outside the loop:

 for(var x = 0; x < scores.length; x ++) { Sum = Sum + scores[x]; //or Sum += scores[x]; } average = Sum / scores.length; //length of the array scores is in scores.length 

I would use parseInt() instead of new Number() , because new Number() creates an object, and parseInt() gives you the actual value of the letter as the result. (higher performance).

By the way, do not forget to put var in front of each variable definition if you do not want them to be accessed globally (bad idea). You did a good job with all variables except scores . The definition should be var scores , although this is not the source of this error.

Another point: you can check if the result of parseInt() works with the isNaN() function. If your numbers can have decimal points, you can also use parseFloat() :

The result of both functions is NaN (not a number) if conversion from string to number is not performed.

And finally, I think it is a good idea that you define an array with the specified length. This improves the readability of your code. However, this is not necessary in Javascript, as it automatically increases / decreases the length of the array at runtime, so you do not need to determine in advance how long it should be. It can be good or bad, depending on how you use it. But in general, you can use var myarr=[]; instead of var myarr= new Array(); . However, when you want to hint to other developers what is happening, you can also specify the length of the array: var myarr=new Array(4); .

And the last thing to use Stackoverflow: accept the best answer and try other useful answers. In this way, you will get a rating from other people.

Good luck.

+7
source

You do not average the correct path ... you get the average value from the sum (outside the loop) divided by the number of labels.

also:

  • Do not use new Array(4) . Array length prefixing is not required in JavaScript (and may degrade readability and performance).
  • Do not use new Number() ever. This creates a Number object, which is a terrible thing that will lead to chaos at some point. Use Number(yourString) to cast.
  • I highly recommend you put a semicolon at the end of your statements.
  • scores not been announced. (Turn on strict mode, please!)

In any case, here is what it might look like:

 function average(form) { var scores = [ // Array literal! Number(form.mark1.value), // You could also use a leading + Number(form.mark2.value), Number(form.mark3.value), Number(form.mark4.value) ]; var sum = 0; for(var i = 0; i < scores.length; i++) { sum += scores[i]; } var average = sum / scores.length; // etc. } 
+2
source

The way to build an array of points is unnecessarily complicated. You can simply do this:

 scores [0] = form.mark1.value; scores [1] = form.mark2.value; scores [2] = form.mark3.value; scores [3] = form.mark4.value; 

Then you have a mistake in the average calculation. The correct way to calculate the average is to sum all the values ​​and then divide them by the number of values ​​once.

 for(var x = 0; x < scores.length; x ++) { Sum = Sum + scores[x]; } average = Sum / scores.length; 
0
source

Source: https://habr.com/ru/post/1447253/


All Articles