In: Computer Science
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.
Please modify the JavaScript code (HTML) below to meet the
requirements, and submit it. Also submit a screenshot of the
output.
<html>
<body>
<script>
//store student name in student array and the grade in grade
array
function spush(slist, name, sgrade, grade){
slist.push(name);//student list
sgrade.push(grade);//grade list
}
//show all the student names and their grades
function showlist(slist, sgrade){
var i;
for (i = 0; i < slist.length; i++){
document.writeln(slist[i] +" "+sgrade[i]+ "<br>");
}
}
function findmax(sgrade){
var i;
var max;
var maxid;
max = sgrade[0];
maxid = 0;
for(i=1;i<sgrade.length; i++){
if(max < sgrade[i]){
max = sgrade[i];
maxid = i;
}}
return maxid;//using index number
}
var stdlist = [];
var sgrade =[];
var mid;
//instead of following, please use prompt for input
spush(stdlist, "John", sgrade, 90);
spush(stdlist, "Tom", sgrade, 95);
spush(stdlist, "Mary", sgrade, 97);
showlist(stdlist, sgrade);
mid = findmax(sgrade);//max grade index number
document.writeln("The maximum grade: "+stdlist[mid]+"
"+sgrade[mid]);
</script>
</body>
</html>
<html>
<body>
<script>
//store student name in student array and the grade in grade array
function spush(slist, name, sgrade, grade) {
slist.push(name);//student list
sgrade.push(grade);//grade list
}
//show all the student names and their grades
function showlist(slist, sgrade) {
var i;
for (i = 0; i < slist.length; i++) {
document.writeln(slist[i] +" "+sgrade[i]+ "<br>");
}
}
function findMaxIndex(sgrade){
var i;
var max;
var maxid;
max = sgrade[0];
maxid = 0;
for(i=1;i<sgrade.length; i++){
if(max < sgrade[i]){
max = sgrade[i];
maxid = i;
}
}
return maxid;//using index number
}
function swap(arr, i, j){
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function sortData(stdlist, sgrade) {
var len = stdlist.length
for (i=0; i < len; i++){
for (j=0, stop=len-i; j < stop; j++){
if (sgrade[j] > sgrade[j+1]){
swap(sgrade, j, j+1);
swap(stdlist, j, j+1);
}
}
}
}
var stdlist = Array();
var sgrade = Array();
var name
while(name != '???') {
name = prompt('Enter name of student');
if(name == '???') {
break;
}
var grade = parseFloat(prompt('Enter grades: '));
spush(stdlist, name, sgrade, grade);
}
showlist(stdlist, sgrade)
var index = findMaxIndex(sgrade)
document.writeln("The maximum grade: "+stdlist[index]+" "+sgrade[index]);
sortData(stdlist, sgrade)
showlist(stdlist, sgrade)
</script>
</body>
</html>
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.