Grade calculator in JavaScript with source code

Grade calculator in JavaScript with source code

Introduction

Grade calculator is a small programming exercise made with JavaScript. It is useful for finding the grade of students based on percentage.

Grade calculator is a JavaScript-based programming exercise. This exercise is useful for all js beginners who are new to JavaScript language. In this exercise, we are using JavaScript function, variables, if-else, return, etc.

When the function is called you will get the result based on the number inserted. The formulae for getting the percent is given number divided by total and multiplied by 100. And the grade is assigned based on the percentage. It is a console based program. See the code below.

let gradeCalc = function (score, totalScore) {
    let percent = (score / totalScore) * 100
    let letterGrade = ''

    if (percent >= 90){
        letterGrade = 'A'
    } else if (percent >= 80) {
        letterGrade = 'B'
    } else if (percent >= 70) {
        letterGrade = 'C'
    } else if (percent >= 60) {
        letterGrade = 'D'
    } else {
        letterGrade = 'F'
    }

    return `You got ${letterGrade} with ${percent}`
}

let result = gradeCalc(45, 100)
console.log(result);

How to use this grade calculator project?

  • Copy the code and paste in the editor.
  • Create html file and path the script.
  • Launch the html file and inspect the page.
  • Select the console tab. You will see the output there.