Practice and reinforce the concepts from Lesson 3
In this activity, you will learn how to use mathematical operators in JavaScript to perform calculations. You'll create calculators for birth year, BMI, age in months, and time conversions using arithmetic operations.
: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.
:bulb: Tip You can also download the source code template from Stackblitz using the Download Project button if you prefer to use your own IDE.
year
and assign the value of this yearage
and prompt()
a message saying "What is your age?"birthYear
and calculate it with: year - age
alert()
: "Your birth year is " + birthYearExample: If the year is 2021 and your age is 15:
var year = 2021;
var age = +prompt("What is your age?");
var birthYear = year - age;
alert("Your birth year is " + birthYear);
:bulb: Tip Notice the
+
beforeprompt()
? This converts the string input into a number so we can do math with it!
BMI (Body Mass Index) helps estimate if your body is at a healthy weight. Learn more about BMI results here.
BMI = weight / (height × height)
Math.round()
to round the result:bulb: Tip Remember to use
+prompt()
to convert string inputs to numbers! For example:
var weight = +prompt("Enter your weight in kg:");
Expected Results:
How many months have you been alive? Let's calculate that using your age and birth month!
months = (age × 12) + (curMonth - birthMonth)
:bulb: Tip Common Challenge: Getting wrong results?
+
operator: var age = +prompt("What is your age?");
Expected Results:
Let's convert seconds into a readable format showing hours, minutes, and seconds!
hours = totalSeconds / 3600
minutes = (totalSeconds % 3600) / 60
seconds = totalSeconds % 60
Math.round()
to round all results:bulb: Tip Understanding the % (modulo) operator:
%
gives you the remainder after division10 % 3 = 1
(because 10 / 3 = 3 remainder 1)Expected Results:
Before submitting, make sure your projects include:
+prompt()
for number conversion:bulb: Tip Common Problems & Solutions:
Getting NaN (Not a Number)?
+prompt()
not just prompt()
Calculations seem wrong?
Prompts not appearing?
:information_source: Ready to submit your projects? When you have completed ALL THREE calculator projects:
- Test each calculator with the example values
- Make sure all results match the expected outputs
- Submit your work here:
Submit Your Projects Here
Remember: Test all calculations before submitting!