JS: Grading System of a school Any Fix (Error== undefined)

Viewed 37

A school has the following rules for the grading system:

// a. 45 to 50 - D
// b. 50 to 60 - C
// c. 60 to 80 - B
// d.Above 80 - A

Ask the user to enter marks, name as well as Roll No and print the corresponding grade

const school_grading_system = () => {
  let student_details = {
    name_of_student: prompt("Enter your name:"),
    roll_number_of_student: Number(prompt("Enter your Roll No: ")),
    marks_of_student: Number(prompt("Enter your marks out of 100: ")),
    percentage_obtained_student: function() {
      return ((this.marks_of_student / 100) * 100)
    }
  }
  if (this.percentage_obtained_student >= 80 && this.percentage_obtained_student < 80) {
    console.log(`${this.percentage_obtained_student}:Good Job! You have scored A Grade`)
  } else if (this.percentage_obtained_student >= 60 && this.percentage_obtained_student < 60) {
    console.log(`${this.percentage_obtained_student}: Good! You have obtained B Grade`)
  } else if (this.percentage_obtained_student >= 50 && this.percentage_obtained_student < 60) {
    console.log(`${this.percentage_obtained_student}: Good! You have obtained C Grade`)
  } else if (this.percentage_obtained_student >= 45 && this.percentage_obtained_student < 50) {
    console.log(`${this.percentage_obtained_student}: Good! You have obtained D Grade`)
  }
}

console.log(school_grading_system())

3 Answers

Here is a shorter way of doing the grading work:

// 45 to 50 - D
// 50 to 60 - C
// 60 to 80 - B
// Above 80 - A
const grades=[[101,"*** impossible - you must have cheated!"],[80,"A"],[60,"B"],[50,"C"],[45,"D"],[0,"E or below"]];

[95,23,57,-5,45,83,110,74,62].forEach(s=>
  console.log(`your score of ${s} gets you the grade ${(grades.find(([g])=>s>=g)??[0,"below ZERO"])[1]}`))

There are some problems within your code that lead to the result being wrong, let's break them down:

  • The use of this keyword in your school_grading_system (outside the student_details object) is the window object and NOT student_details object so when you try this.percentage_obtained_student that is translated to window.percentage_obtained_student and as window dosen't have an attribute called percentage_obtained_student the result of that call would be undefined that lead to deliver the wrong results. So instead of this.percentage_obtained_student you should use student_details.percentage_obtained_student to get the needed piece of data.

  • Another issue is the your conditions found on the if .. else statements. Let's imagine a student having 80 as his score, when you say student_details.percentage_obtained_student >= 80 && student_details.percentage_obtained_student < 80 this condition will never be satisfied because it negates itself as that conditon translates to IF the SCORE is greater than or equal to 80 AND the score is less than 80 but we cannot have a number bigger or equal and lower than itself at the same time (I couldn't describe more accurately).

With that being said, here's a corrected version of your code:

const school_grading_system = () => {
  let student_details = {
    name_of_student: prompt("Enter your name:"),
    roll_number_of_student: Number(prompt("Enter your Roll No: ")),
    marks_of_student: Number(prompt("Enter your marks out of 100: ")),
  }

  /** the conditons are now refined */
  if (student_details.marks_of_student >= 80) {
    console.log(`${student_details.marks_of_student}:Good Job! You have scored A Grade`)
  } else if (student_details.marks_of_student >= 60) {
    console.log(`${student_details.marks_of_student}: Good! You have obtained B Grade`)
  } else if (student_details.marks_of_student >= 50) {
    console.log(`${student_details.marks_of_student}: Good! You have obtained C Grade`)
  } else if (student_details.marks_of_student >= 45) {
    console.log(`${student_details.marks_of_student}: Good! You have obtained D Grade`)
  }
}

school_grading_system();

By the way, you function has some unhandled cases, to mention some, it doesn't take into consideration the case of a score LESS THAN 45. Also, it doesn't handle wrong user inputs like when the user types a number higher than 100.

// Rule 1: Kinda keep it simple 
// As total marks are going to be 100 so no need for a percentage method

const school_grading_system = () => {
  let student_details = {
    name_of_student: prompt("Enter your name:"),
    roll_number_of_student: Number(prompt("Enter your Roll No: ")),
    marks_of_student: Number(prompt("Enter your marks out of 100: "))

  }
  if (student_details.marks_of_student >= 45 && student_details.marks_of_student < 50) {
    return "Grade D"
  } else if (student_details.marks_of_student >= 50 && student_details.marks_of_student < 60) {
    return "Grade C"
  } else if (student_details.marks_of_student >= 60 && student_details.marks_of_student < 80) {
    return "Grade B"
  } else if (student_details.marks_of_student >= 80 && student_details.marks_of_student <= 100) {
    return "Grade A"
  }
}
console.log(school_grading_system())


// Note: I have not displayed the name and marks stuff because I hope you are capable enough to do that so I am leaving that upon you

Related