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
-
File Creation:
- Create a new JavaScript file named
gradeCalculator.js
.
- Create a new JavaScript file named
-
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–100B
for scores 80–89C
for scores 70–79D
for scores 60–69F
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.
- Write a function named
Task 2: Test the Function
- Function Calls:
- Call
calculateGrade()
with various test scores (for example:95
,82
,73
,65
,50
, and an out-of-range value like-5
or105
).
- Call
- Display Output:
- Use
console.log()
to print the returned letter grade for each test case.
- Use
- Optional Enhancement:
- Wrap the function calls in a loop or allow user input via
prompt()
to make the program interactive.
- Wrap the function calls in a loop or allow user input via
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.
- Submit the
- 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
Criteria | Points | Description |
---|---|---|
Function Definition | 10 pts | Correctly defines a function calculateGrade(score) that accepts one parameter. |
Conditional Logic Implementation | 10 pts | Uses appropriate if/else if/else statements (or a switch statement) to determine and return the correct letter grade for valid scores. |
Return Values | 8 pts | Returns the correct letter grade for scores within the valid range; returns "Invalid score" for scores out of range. |
Function Calls and Testing | 8 pts | Calls the function with multiple test values and prints the output using console.log() , demonstrating that the function works as intended. |
Parameter Validation | 5 pts | Incorporates a check to ensure the input score is between 0 and 100, handling out-of-range values appropriately. |
Code Quality and Documentation | 5 pts | Code is well-organized, properly indented, uses descriptive variable and function names, and includes relevant comments. |
Code Reusability | 4 pts | Demonstrates modularity by encapsulating the grade calculation logic within a function to avoid code repetition. |
Total Points: 50 points
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.