I'm trying to code a gradebook as a project. So far I have developed the code to calculate exam grades and display the outputs however, I'm having trouble displaying my arrays once the user inputs the information (Student first and last name, ID and Exams) It wont display it. Also my code still wants to add user inputs once I click the cancel button on the browser. I would like the user inputs to stop once I hit the cancel button but how would I fix that?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//Initilization of Grades
let grade = 0;
//Start of the function
studentArray();
//Student Function
function studentArray(){
const student = [{
firstName :"",
lastName :"",
ID :0,
Exams :0
}]
while(true){
student.firstName = prompt("Enter First Name : ");
student.lastName = prompt("Enter Last Name : ");
student.ID = prompt("Enter ID : ");
student.Exams = calculateExamgrade(grade);
if(student === "e" || student=== null){
break;
}
student.push(student.firstName);
student.push(student.lastName);
student.push(student.ID);
student.push(student.Exams);
}
//Displays array
for(i = 0; i < studentArray; i ){
console.log(i 1 studentArray[i]);
}
}
//Calculate the grade of the exams
function calculateExamgrade(grade){
let TestGrades = [];
while(true){
let input = prompt("Add Grades")
if(input ==="e" || input === null){
break;
}
TestGrades.push(Number(input));
}
//Calclate the arrays
var sum = 0;
for(var i in TestGrades){
sum = TestGrades[i];
}
//Calculate average grade
grade = Math.round((sum/TestGrades.length));
//Return grade letter
switch(true){
case grade >= 90:
console.log("You recived an A")
return grade;
break;
case grade <= 89 && grade >= 80:
console.log("You recived a B")
return grade;
break;
case grade <= 79 && grade >= 70:
console.log("You recived a C")
return grade;
break;
case grade <= 69 && grade >= 60:
console.log("You recived a D")
return grade;
break;
case grade <= 59:
return grade;
break;
}
}
</script>
</body>
</html>
CodePudding user response:
Hey I solve the problem in your code and write comments explain the part that has issue in your code, have a look
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//Initilization of Grades
let grade = 0;
//Start of the function
studentArray();
//Student Function
function studentArray() {
// Student value need to change so don't use const, however u can use push function with const variables
// Here u declare array of object, do u aware of this?
student = {
firstName: "",
lastName: "",
ID: 0,
Exams: 0
}
// In this situation u already assign the value to student.firstName and so on, so there is no need for u to use push function
student.firstName = prompt("Enter First Name : ");
student.lastName = prompt("Enter Last Name : ");
student.ID = prompt("Enter ID : ");
student.Exams = calculateExamgrade(grade);
//display the object
console.log(student)
}
//Calculate the grade of the exams
function calculateExamgrade(grade) {
let TestGrades = [];
while (true) {
let input = prompt("Add Grades")
// change the === null to == null (try google why, because I'm busy I have class to attend :)
if (input === "e" || input == null) {
break;
}
// Check if the input is convertible to integer or not if yes push to array, if no prompt input again without pushing the input
if (Number(input) === NaN) {
continue
} else {
TestGrades.push(Number(input));
}
}
//Display Array
console.log(TestGrades)
//Calclate the arrays
var sum = 0;
for (var i in TestGrades) {
sum = TestGrades[i];
}
//Calculate average grade
grade = Math.round((sum / TestGrades.length));
//Return grade letter
switch (true) {
case grade >= 90:
console.log("You recived an A")
return grade;
break;
case grade <= 89 && grade >= 80:
console.log("You recived a B")
return grade;
break;
case grade <= 79 && grade >= 70:
console.log("You recived a C")
return grade;
break;
case grade <= 69 && grade >= 60:
console.log("You recived a D")
return grade;
break;
case grade <= 59:
return grade;
break;
}
}
</script>
</body>
</html>
CodePudding user response:
Working with prompt()
generally does not create a good user experience. But this seems to be an exercise, so here is my suggestion on how to simplify and fix your code:
const data=[];
const props=[["fn","First name?"],["ln","Last name?"],["id","Id?"],["scores","Grades?"]];
function getStudent(){
const a={};
for ([k,q] of props){
// const q=props[k];
if (k=="scores") { let t;
a[k]=[];
while(t=prompt(q)) a[k].push( t);
if (a[k].length){
a.avg=a.scores.reduce((a,c)=>a c)/a.scores.length;
// grade calculation as a one-liner:
a.grade="ABCD"[9-Math.floor(a.avg/10)]??"";
}
} else
a[k]=prompt(q);
if (!a.fn) return false
}
return a;
}
for (let s;s=getStudent();) data.push(s);
console.log(data);
You can input several student data. The loop will be terminated by clicking "cancel" when asked for the first name.