Skip to Content
Lab 5

Functions and Reusability – Grade Calculator

Overview and Objectives

In this lab, learners will focus on writing reusable code by creating a JavaScript function that calculates letter grades from numeric scores. This exercise reinforces the use of functions, conditional logic, parameter validation, and proper code organization. By the end of this lab, learners will be able to:

  • Define a function in JavaScript.
  • Use conditional statements (if/else if/else) to determine outcomes.
  • Validate function input and return appropriate values.
  • Call functions multiple times and display outputs using console.log().

Tasks and Instructions

Task 1: Create the Grade Calculator Function

  1. File Creation:

    • Create a new JavaScript file named gradeCalculator.js.
  2. Define the Function:

    • Write a function named calculateGrade(score) that:
      • Accepts a numeric score as its parameter.
      • Uses conditional logic to return a letter grade based on the following criteria:
        • A for scores 90–100
        • B for scores 80–89
        • C for scores 70–79
        • D for scores 60–69
        • F for scores less than 60
      • Checks if the score is within the valid range (0–100). If not, returns an "Invalid score" message.
    • Include a header comment describing what the function does and inline comments where needed.

Task 2: Test the Function

  1. Function Calls:
    • Call calculateGrade() with various test scores (for example: 95, 82, 73, 65, 50, and an out-of-range value like -5 or 105).
  2. Display Output:
    • Use console.log() to print the returned letter grade for each test case.
  3. Optional Enhancement:
    • Wrap the function calls in a loop or allow user input via prompt() to make the program interactive.

Submission Criteria

The following should be included within a GitHub repository, and submitted via Canvas:

  • Files/Links:
    • Submit the gradeCalculator.js file containing your code.
  • Output Evidence:
    • Provide a screenshot or text log showing your code execution and output in the console.
  • Error-Free Code:
    • Ensure that your script executes without syntax or runtime errors.

Grading Rubric

CriteriaPointsDescription
Function Definition10 ptsCorrectly defines a function calculateGrade(score) that accepts one parameter.
Conditional Logic Implementation10 ptsUses appropriate if/else if/else statements (or a switch statement) to determine and return the correct letter grade for valid scores.
Return Values8 ptsReturns the correct letter grade for scores within the valid range; returns "Invalid score" for scores out of range.
Function Calls and Testing8 ptsCalls the function with multiple test values and prints the output using console.log(), demonstrating that the function works as intended.
Parameter Validation5 ptsIncorporates a check to ensure the input score is between 0 and 100, handling out-of-range values appropriately.
Code Quality and Documentation5 ptsCode is well-organized, properly indented, uses descriptive variable and function names, and includes relevant comments.
Code Reusability4 ptsDemonstrates modularity by encapsulating the grade calculation logic within a function to avoid code repetition.

Total Points: 50 points

Note

Partial credit will be awarded for submissions that demonstrate a clear understanding of function creation and conditional logic even if some elements are incomplete or contain minor errors. For example, if the function correctly calculates grades for most ranges but lacks proper validation for out-of-range inputs, or if the function is well-written but test cases are minimal, points will be deducted proportionally. Credit will be given for correct overall approach, modular design, and clear code organization, even if some best practices are not fully met.