Skip to main content

Command Palette

Search for a command to run...

[JavaScript] JavaScript Fundamentals – Part 1 Coding Challenge #2

Updated
•View as Markdown
[JavaScript] JavaScript Fundamentals – Part 1 Coding Challenge #2
/* 
Use the BMI example from Challenge #1, and the code you already wrote, and
improve it.
Your tasks:
1. Print a nice output to the console, saying who has the higher BMI. The message
is either "Mark's BMI is higher than John's!" or "John's BMI is higher than Mark's!"
2. Use a template literal to include the BMI values in the outputs. Example: "Mark's
BMI (28.3) is higher than John's (23.9)!"
*/

const markMass1 = 78;
const markHeight1 = 1.69;
const johnMass1 = 92;
const johnHeight1 = 1.95;
const markMass2 = 95;
const markHeight2 = 1.88;
const johnMass2 = 85;
const johnHeight2 = 1.76;

const markBMI1 = markMass1 / markHeight1 ** 2;
const johnBMI1 = johnMass1 / johnHeight1 ** 2;
const markBMI2 = markMass2 / (markHeight2 * markHeight2);
const johnBMI2 = johnMass2 / johnHeight2 ** 2;

console.log(`Mark's BMI 1 = ${markBMI1}`, `John's BMI 1 = ${johnBMI1}`);
console.log(`Mark's BMI 2 = ${markBMI2}`, `John's BMI 2 = ${johnBMI2}`);

if (markBMI1 > johnBMI1) {
    console.log(`Mark's BMI 1 is higher than John's!`);
} else {
    console.log(`John's BMI 1 is higher than Mark's!`);
}

if (markBMI2 > johnBMI2) {
    console.log(`Mark's BMI 2 (${markBMI2}) is higher than John's (${johnBMI2})!`);
} else {
    console.log(`John's BMI 2 (${johnBMI2}) is higher than Mark's (${markBMI2})!`)
}

Code Challenge 2.PNG

More from this blog

Jayden's Blog

166 posts