Fall 2024 - P4
| Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
CSP Period 4 Unit 3.6.4 Conditionals in JavaScript Hacks
In this lesson, students will practice using if-else statements in JavaScript through interactive exercises. They will work on solving problems that require making decisions in their code based on different conditions. The exercises will cover how to use if, else if, and else statements to handle multiple outcomes. Students will also practice combining conditions with logical operators like && (and), || (or), and ! (not). By the end of the lesson, students will have improved their ability to write programs that respond dynamically based on different scenarios.
Hack 1: Check Voting Eligibility
Description: This hack checks if a person is eligible to vote based on their age.
- Define a function named
checkVotingEligibilitythat takes one parameter calledage. - Inside the function, use an
ifstatement to check if the age is 18 or older.- Return the message
"You are eligible to vote!"if true; otherwise, return"You are not eligible to vote yet.".
- Return the message
Call the function with different age values to test your work
Hack 2: Grade Calculator
Description: This hack assigns a letter grade based on a numerical score.
- Define a function named
getGradethat takes one parameter calledscore. - Use
if...else if...elsestatements to determine the letter grade based on the score:- Return
"Grade: A"for scores 90 and above. - Return
"Grade: B"for scores between 80 and 89. - Return
"Grade: C"for scores between 70 and 79. - Return
"Grade: F"for scores below 70.
- Return
Call the function with different scores to test your work.
Hack 3: Temperature Converter
Description: This hack converts temperatures between Celsius and Fahrenheit.
- Define a function named
convertTemperaturethat takes two parameters:value(the temperature) andscale(either “C” or “F”). - Use an
ifstatement to check the scale:- If it’s “C”, convert the temperature to Fahrenheit using the formula
(value * 9/5) + 32and return the result with"°F". - If it’s “F”, convert to Celsius using
(value - 32) * 5/9and return the result with"°C". - If the scale is neither, return an error message.
- If it’s “C”, convert the temperature to Fahrenheit using the formula
Test the function with different values and scales to check your work