I completed the assignment, but I'm still unclear if I have fully grasped my teacher's instructions. Can you provide some insight on this for me?
I attempted to solve it using two arrays, a for-loop, and connecting a button with a function, and it seems to be working. However, the question remains - is it correct? Since my school assignment is in Swedish, some words may be in that language.
Here is the assignment question:
Morgan, a high school teacher, needs a register to keep track of their students' grades. You are given two arrays, one containing names and the other containing grades. For example: name = ["Klara", "Andrea", "Emil", "Sarah", "Alicia", "Victor", "Thomas", "Robert"] with grades: grade = ["A", "B", "C", "A", "D", "C", "E", "E"]. Your task is to create a function (getGrade) that searches for a name and returns the corresponding grade. Then, create a page to test and demonstrate your new feature (either automatically when the page loads or via a button press). The arrays can either be global variables or preferably local variables passed as input to functions.
<body>
<input type="button" value="Click to get Student Name and Grade" onclick="getGrade()">
<div id="student1"></div>
<div id="student2"></div>
<div id="student3"></div>
<div id="student4"></div>
<div id="student5"></div>
<div id="student6"></div>
<div id="student7"></div>
<div id="student8"></div>
<script>
var name = ["Klara", "Andrea", "Emil", "Sarah", "Alicia", "Victor", "Thomas", "Robert"];
var grade = ["A", "B", "C", "A", "D", "C", "E", "E"];
function getGrade() {
for (var i = 0; i < name.length; i++) {
if (name[i] == name[0]) {
document.getElementById("student1").innerHTML += name[i] + "<br/> Grade: " + grade[i];
} else if (name[i] == name[1]) {
document.getElementById("student2").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else if (name[i] == name[2]) {
document.getElementById("student3").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else if (name[i] == name[3]) {
document.getElementById("student4").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else if (name[i] == name[4]) {
document.getElementById("student5").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else if (name[i] == name[5]) {
document.getElementById("student6").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else if (name[i] == name[6]) {
document.getElementById("student7").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
} else {
document.getElementById("student8").innerHTML += name[i] + "<br/> Grade: " + grade[i];;
}
}
}
</script>
Thank you for your feedback!