简体   繁体   中英

how can i add a value from an array to a value from an another one in javascript?

So, I have two arrays: grade[] and grade2[] that contains elements I give from windows.prompt . I want to add a value from grade with a value from grade2 so i can calculate "media". But the code that i wrote just concats those two values, doesn't add them actually. I am a beginner and I hope you can help me. Thank you!

function display_student() 
{ 
 var n=window.prompt("Number of students: "+"");
 var name= new Array(n);
 var grade=new Array(n); 
 var grade2=new Array(n); 
 var y=0; 
 for (y=0; y<n; y++) 
 { 
name[y]=window.prompt("Student name:","");
grade[y]=window.prompt("Grade 1: ","5");
grade2[y]=window.prompt("Grade 2: ","5")

document.write("</br>"+"Student name: "+name[y]+"</br>"+"Grade 1: "+grade[y]+"    </br>"+"Grade 2: "+grade2[y]+"</br>");
var media=(grade[y]+grade2[y])/2;

document.write("Media: "+media+"</br>");
if(media<5)
document.write("Failed");
else
document.write("Promoted");
  } 
}    

添加之前,请使用parseInt函数。

var media=(parseInt(grade[y]) + parseInt(grade2[y]))/2;

Because they are strings and no numbers.
Take a look at parseInt(string, radix)

The values in the array are of type String ( as returned by the window.prompt function ) you need to convert them to numbers to add them together :

function display_student() {
    var n = window.prompt("Number of students: " + "");
    var name = new Array(n);
    var grade = new Array(n);
    var grade2 = new Array(n);
    var y = 0;
    for (y = 0; y < n; y++) {
        name[y] = window.prompt("Student name:", "");
        grade[y] = window.prompt("Grade 1: ", "5");
        grade2[y] = window.prompt("Grade 2: ", "5"); // added semi-colon here too

        document.write("</br>" + "Student name: " + name[y] + "</br>" + "Grade 1: " + grade[y] + "    </br>" + "Grade 2: " + grade2[y] + "</br>");
        var media = (parseFloat(grade[y]) + parseFloat(grade2[y])) / 2;

        document.write("Media: " + media + "</br>");
        if (media < 5) {
            document.write("Failed");
        } else {
            document.write("Promoted");
        }
    }
}

Working example : http://jsfiddle.net/z5cUw/

Use:

var media=(parseInt(grade[y], 10)+parseInt(grade2[y], 10))/2;

Assuming your grades are integers. Otherwise, use parseFloat (in that case, you won't need the second parameter).

使用parseFloat:

var media=(parseFloat(grade[y])+parseFloat(grade2[y]))/2;

This is an indirect answer because this is tagged homework.

Because window.prompt returns a string by default, you need to convert this string to a number. There are several methods to do this.

  • You can use parseInt() as several people have suggested. Returns an integer value.
  • You can use parseFloat() as suggested.
  • You can force it using a simple number division on the prompt: window.prompt("Grade 1: ","5")/1; or during the media calculation: var media = (grade[y]/1 + grade2[y]/1) / 2;
  • You can use the Number() function: Number(window.prompt("Grade 1: ", "5"));

It is up to you to determine which is the appropriate method for your needs. What if the value entered is NOT a number? Study each of these methods to determine which best suites your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM