In: Computer Science
JAVASCRIPT:
- Please create an object (grade) with 10 names and 10
grades.
- Create a method (inputGrade) that can put a name and a grade to
the grade object.
- Create another method (showAlltheGrades) to show all the grade in
that object.
- Create the third method (MaxGrade) that can display the maximum
grade and the student name.
- Using “prompt” and inputGrade method input 10 student names and
their grades.
- Display all the grades and names by using showAlltheGrades
method.
NOTE: Make sure to use the push() method when adding elements to the arrays.
Please post the code and a screenshot of the output. Thanks!
I've declared the names initially as well as I have given them using the inputGrade method. Choose the one you are required.
Code:
var grade = {
name:
["Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10"],
score:
["44","83","74","54","65","66","44","76","56","71"],
inputGrade: function(data1,data2){
grade.name.push(data1);
grade.score.push(data2);
},
showAlltheGrades: function(){
for ( var i=0;
i<grade.name.length; i++ ) {
console.log(grade.name[i]+":"+grade.score[i]);
}
},
MaxGrade: function(){
var max = grade.score[0];
var index = 0;
for (var i = 1; i <
grade.score.length; i++) {
if
(grade.score[i] > max) {
index = i;
max = grade.score[i];
}
}
console.log("Maximum Grade:
"+grade.name[index]+" , "+grade.score[index]);
}
};
var name,score;
for (i = 0; i < 10; i++) {
name=prompt("Please enter Student "+i+" Name :
");
score=prompt("Please enter Student "+i+" Grade :
");
grade.inputGrade(name,parseInt(score));
}
Output: