Practice and reinforce the concepts from Lesson 4
In this activity, you will learn how to use if-else statements and conditional logic to control the flow of your JavaScript programs. You'll create password checkers, BMI categorizers, and input validation systems.
:warning: Warning DO NOT DELETE the existing files in the template:
- Package files
- Any other files you didn't create
ONLY EDIT the necessary files.
In each exercise below, click the Stackblitz link and familiarize yourself with the interface.
Time: 5 minutes
:bulb: Tip Alternative: You can download the source code template from Stackblitz using the Download Project button if you prefer to use your own IDE.
For the prompt, you can ask whatever message you like.
If you have done it correctly, it should look like this:
Demo code answer:
var mySecretPass = "iLoveTelebort"
var checkPassword = prompt("What is the password?");
if (mySecretPass === checkPassword) {
console.log("Successful.");
} else {
console.log("Invalid password.");
}
:bulb: Tip Notice how we use
===
(strict equality) instead of==
. This ensures both the value and type match!
Time: 20 minutes
:bulb: Tip Alternative: You can download the source code template from Stackblitz using the Download Project button if you prefer to use your own IDE.
Do you remember the BMI calculator we did in the previous concept? This time let's make it so we can check what weight category as the result.
BMI (Body Mass Index) is used to estimate if your body is at a healthy weight or not.
BMI = weight / (height * height)
.toFixed(1)
to show 1 decimal place
alert("Your BMI is " + BMI + ". You are underweight.");
alert("Your BMI is " + BMI + ". You have a normal weight.");
alert("Your BMI is " + BMI + ". You are overweight.");
:bulb: Tip Hint: Use if-else if-else statements to check BMI ranges:
if (BMI < 18.5)
else if (BMI >= 18.5 && BMI < 25)
else
Assuming your weight is 60kg and your height is 1.7 meters:
Time: 15 minutes
:warning: Warning Important: Make sure you complete Exercise 1 before starting this exercise. You'll be building on your previous code!
Let's create a validation system for our BMI calculator to handle invalid inputs.
+prompt()
instead of just prompt()
var weight = +prompt("Enter your weight in kg:");
var height = +prompt("Enter your height in meters:");
alert("Invalid input.");
if (weight === 0 || height === 0) {
alert("Invalid input.");
} else {
// Your BMI calculation and category logic here
}
:bulb: Tip Testing Tip: Test all weight categories by using these example inputs:
Make sure your Control Flow projects include:
.toFixed(1)
for precise BMI display+prompt()
for number conversion||
) for validation:information_source: Before submitting, make sure to:
- Test all BMI categories (underweight, normal, overweight)
- Test validation with 0 values for both weight and height
- Verify your alerts show the correct messages
- Check that your code uses
+prompt()
for number conversion
When you have completed your "Control Flow" projects, submit them using the link below:
Problem: "My BMI calculation shows NaN"
+prompt()
to convert string inputs to numbersProblem: "My validation isn't working"
===
for comparisonProblem: "Wrong weight category is showing"
Problem: "My decimal places are wrong"
.toFixed(1)
on your BMI variable after calculation but before using it in alerts:bulb: Tip Need Help? If you're stuck, try adding
console.log()
statements to check your variable values at each step!