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!
[Reference JavaScript code]
<html>
<body>
<script>
// Declare a class
class Student {
// initialize an object
constructor(grade, name) {
this.grade=grade;
this.name=name; }
//Declare a method
detail() {
document.writeln(this.grade + " " +this.name)
}//detail
}//class
var student1=new Student(1234, "John Brown");
var student2=new Student(2222, "Mary Smith");
student1.detail();//call a method
student2.detail();
</script>
</body>
</html>
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new web page with name "grade.html" is created, which contains following code.
grade.html :
<html>
<body>
<script>
// Declare a class
class Student {
// initialize an object
constructor(grade, name) {
this.grade=grade;
this.name=name; }
//Declare a method
detail() {
document.writeln(this.grade + " " +this.name+"<br/>")
}//detail
}//class
var grade=[];//object grade to hold the Student
//function to get grades and names from the user
function inputGrade()
{ //using for loop to get all names
for(i=0;i<10;i++)
{
var name=prompt("Enter Name","name");//asking and reading name
var grades=parseInt(prompt("Enter Grade","Grade"));//asking and reading grade
//creating Student object and passing name and grade
var student=new Student(grades,name);
grade.push(student);//pushing student in the grade
}
}
//function to show all the grade
function showAlltheGrades()
{
for(i=0;i<10;i++) //using for loop to display details
{
grade[i].detail();//call method on the object
}
}
//function to display MaxGrade
function MaxGrade()
{
//considering first element grade as max grade
var mgrade=grade[0].grade;
var names="";//variable to get name
for(i=0;i<10;i++)
{
if(mgrade<grade[i].grade)//checking grade
{
mgrade=grade[i].grade;//get grade
names=grade[i].name;//get name
}
}
//print max grade and name
document.write("Max Grade:"+mgrade+" Name:"+names);
}
inputGrade();//call function to get input
showAlltheGrades();//call function to display names
MaxGrade();//call function to find max grade
</script>
</body>
</html>
======================================================
Output : Open web page grade.html in the browser and will get the screen as shown below
Screen 1 :grade.html, Screen asking name
Screen 2:Screen asking grade
Screen 3:Screen showing all grades and max grades
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.