[CWM] Conditionals

Introduction to Conditional Statements in JavaScript

  • Objective: Create a program to calculate the average of three test scores and provide an enthusiastic message if the average score exceeds 95.

Setting Up the Environment

  • Start by creating a new JavaScript file named average.js to enable syntax highlighting for JavaScript code.

Problem Definition

  • The task is introduced by Professor Tillman, who assigns three tests and wants a program that:

    • Calculates the average of the test scores.

    • Congratulates students enthusiastically if their average score is greater than 95.

  • Concept of Enthusiasm in Messages:

    • The message to be displayed should encourage students, inspired by the phrase "y'all watch my gaps. Don't hate. Congratulate."

Program Requirements

Inputs

  • Three fixed test scores will be provided without user input in this scenario:

    • Test 1: 50

    • Test 2: 95

    • Test 3: 80

    • Note: It’s stated that generally, students' test scores will be much higher.

Outputs

  • The program should:

    • Calculate the average test score.

    • Optionally display a congratulatory message if the average is more than 95.

Processing Steps

  1. Calculating the Average:

    • The formula for calculating the average test score is given as:
      extAverage=racextTest1+extTest2+extTest33ext{Average} = rac{ ext{Test 1} + ext{Test 2} + ext{Test 3}}{3}

  2. Display Congratulatory Message:

    • If the average is greater than 95, display a message:

      • Example: "Congrats! You scored higher than 95%! You're the bomb diggity!"

  3. Conditional Logic:

    • Use an if statement to check if the average score exceeds 95 before displaying the congratulatory message.

Coding Steps

  • Step 1: Declare Variables for Test Scores and Calculate Average

    • Create variables to hold the test scores (e.g., test1, test2, test3).

    • Calculate the average using the established formula, storing the result in a variable called testAverage.

    • Code Snippet:
      javascript var test1 = 50; var test2 = 95; var test3 = 80; var testAverage = (test1 + test2 + test3) / 3;

  • Step 2: Set Up Congratulatory Message

    • Declare a variable for the congratulatory message and structure it.

    • Example of how to concatenate a message:
      javascript var congratulationsMessage = "Congrats! You scored higher than 95%! You're the bomb diggity!";

  • Step 3: Implement Conditional Logic to Display Message

    • Write an if statement to check if testAverage is greater than 95:

    • Code Snippet:
      javascript if (testAverage > 95) { console.log(congratulationsMessage); }

  • Step 4: Display the Average Test Score

    • Use console.log to print the average test score.

    • Code Snippet:
      javascript console.log("Your average test score is: " + testAverage);

Testing the Program

  • Run the program using Node.js in the command prompt with the command: bash node average.js

    • Initial results show an average test score of 75, which does not meet the condition to display the congratulatory message.

    • Adjust test scores to achieve a scenario where the average is greater than 95, thus testing the conditional message display.

Final Adjustments and Output Formatting

  • Utilize the toFixed(2) method to truncate the average score to two decimal places when outputted: javascript testAverage = testAverage.toFixed(2);

    • Add a percentage symbol to the output if desired:
      javascript console.log("Your average test score is: " + testAverage + "%");

Conclusion

  • This exercise showcases the use of conditional statements in JavaScript to create a practical application that not only computes an average but also provides interactive feedback based on certain conditions.