In: Computer Science
[JAVA SCRIPT]
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.
Screenshot of the code:
Code to copy:
<!DOCTYPE html>
<html>
<body>
<script>
// Define the addToArray() function
function addToArray(name, grade, student_name, student_grade)
{
// Add the name to the student_name
student_name.push(name);
// Add the grade to the student_grade
student_grade.push(grade);
}
// Define the displayArr() function
function displayArr(student_name, student_grade, counter)
{
// Declare a variable
var x;
for(x =0; x < counter; x++)
{
// Display the student name and corresponding grade
document.writeln("Student Name: " + student_name[x] + " Grade: " + student_grade[x] + "<br/>");
}
}
// Define the findMaximum() function
function findMaximum(student_name, student_grade, counter)
{
// Assign the first value of the student_grade array
var max_value = student_grade[0];
// Initialize a variable to zero
var max_index = 0;
// Declare a variable
var x;
for(x = 1; x < counter; x++)
{
// Check if the value at index x is greater than
// the value at max_value
if(student_grade[x] > max_value)
{
// Assign the value to max_value
max_value = student_grade[x]
// Assign the value of index to max_index
max_index = x;
}
}
// Display the maximum grade and student's name
document.writeln("Maximum Grade: " + max_value + " Student Name: " + student_name[max_index] + "<br/>");
}
// Define the sortGrade() function
function sortGrade(student_name, student_grade, counter)
{
// Declare the necessary variables
var i, j;
for(i = 0; i < counter; i++)
{
for(j = 0; j < counter - i; j++)
{
// Check if grade at index j is greater than
// the grade at index j+1
if(student_grade[j] > student_grade[j+1])
{
// Swap the elements in the student_grade[] array
var temp_grade = student_grade[j];
student_grade[j] = student_grade[j+1];
student_grade[j+1] = temp_grade;
// Swap the elements in the student_name[] array
var temp_name = student_name[j];
student_name[j] = student_name[j+1];
student_name[j+1] = temp_name;
}
}
}
}
// Declare two arrays
var student_name = [];
var student_grade = [];
// Initialize a counter variable to zero
var counter = 0;
do
{
// Prompt user to enter the name
var name = prompt("Enter the name: ");
// Check if the value in name variable is '???'
if(name == '???')
{
break;
}
// Prompt user to enter the grade
var grade = parseFloat(prompt("Enter the grade: "));
// Call the addToArray() function
addToArray(name, grade, student_name, student_grade);
// Increment the
counter++;
}while(name != '???')
// Call the displayArr() function to display the array
displayArr(student_name, student_grade, counter);
// Call the findMaximum() function to display the
// maximum grade
findMaximum(student_name, student_grade, counter);
// Call the sortGrade() function to sort the array by
// grades
sortGrade(student_name, student_grade, counter);
// Call the displayArr() function to display the array
// after sorting by grades
displayArr(student_name, student_grade, counter);
</script>
</body>
</html>
Sample output: