In: Computer Science
<HTML JAVASCRIPT>
Please create an array of student names and another array of student grades.
Create a function that can put a name and a grade to the arrays.
Keep Read student name and grade until student name is “???”. And save the reading by using a function
Create another function to show all the grade in that object.
Create the third function that can display the maximum grade and the student’s name.
Create a sorting function that can sort the arrays based on the student’s grade.
Display all the grades and names sorted by the grade.
[Report]
PLEASE POST OUTPUT IN NOTEPAD++ or NOTEPAD! Thanks
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<input id="name" placeholder="name">
<input id="grade" placeholder="grade">
<button onclick="add()">Submit</button>
<p id="students"></p>
<p id="grades"></p>
<p id="all_grades"></p>
<p id="max_grade"></p>
<p id="sorted_result"></p>
</div>
</body>
<script>
const students = []
const grades = []
function add () {
const student = document.getElementById("name").value
if(student === "???"){
showGrades()
maxGrade()
sort()
return;
}
const grade = parseInt(document.getElementById("grade").value)
students.push(student)
grades.push(grade)
showStudentGrades()
}
function saveDetails(name, grade) {
students.push({name, grade});
console.log(students)
}
function showGrades() {
const allGrades = grades.join(", ")
document.getElementById("all_grades").innerHTML ="All Grades : " + allGrades;
}
function maxGrade() {
let maxIndex = grades.indexOf(Math.max(...grades));
console.log(maxIndex)
let maxGradeStudent = students [maxIndex]
document.getElementById("max_grade").innerHTML ="Max Grade : " + maxGradeStudent + " " + grades[maxIndex];
}
function sort() {
const obj = [];
for(let i = 0 ;i< students.length; i++) {
obj.push({"name":students[i], "grade":grades[i]})
}
obj.sort((a,b) => (a.grade > b.grade) ? 1 : ((b.grade > a.grade) ? -1 : 0));
const result = obj.map(x => x.name + " " + x.grade).join("<br />")
document.getElementById("sorted_result").innerHTML = "Sorted Result : <br />"+result;
}
function showStudentGrades () {
document.getElementById("students").innerHTML = "Students : "+students.join(" ");
document.getElementById("grades").innerHTML = "Grades : "+grades.join(" ");
}
</script>
</html>
|
![]() |