Reputation: 411
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");
}
}
Upvotes: 0
Views: 114
Reputation: 34168
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.
parseInt()
as several people have suggested. Returns an integer value.parseFloat()
as suggested.window.prompt("Grade 1: ","5")/1;
or during the media calculation: var media = (grade[y]/1 + grade2[y]/1) / 2;
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.
Upvotes: 0
Reputation: 38147
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/
Upvotes: 0
Reputation: 973
Use parseFloat:
var media=(parseFloat(grade[y])+parseFloat(grade2[y]))/2;
Upvotes: 0
Reputation: 5654
Use the parseInt
function before adding.
var media=(parseInt(grade[y]) + parseInt(grade2[y]))/2;
Upvotes: 2
Reputation: 71918
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).
Upvotes: 0
Reputation: 7536
Because they are strings and no numbers.
Take a look at parseInt(string, radix)
Upvotes: 1