Instructor: Welcome to the second coding challenge.
In this one you're gonna test your knowledge
of the if/else statement that we just learned about.
This coding challenge number two
will basically improve the first challenge.
We're still gonna be using DBMI example,
but we are going to print a nice output to the console
and this output is gonna tell us who has the higher BMI.
The output message can be either
Mark's BMI is higher than John's
or John's BMI is higher than Mark's.
That's the first part,
and then as a second part use a template string
to include the actual BMI values in the outputs.
Okay, and as you can guess,
you should probably use an if/else statement
to complete this challenge, right?
So, pause the video now and I'll see you in a second.
All right.
Again, I hope that you managed to do this.
Let me show you how I did it.
Let's start by getting this code from the other challenge,
so coding challenge number one,
because of course we don't have to repeat the same code.
Here we have the mass and the height of both John and Mark,
and we already have their BMIs calculated.
We also have this condition here basically already,
so this operation here already tells us
whether Mark has a higher BMI or not, right?
Let's get rid of this
and let's write our if/else statement.
If BMIMark is greater than BMIJohn,
and here we could have just used this variable,
so we could have written Mark higher BMI right here,
but as I said in the previous lecture
it's actually more common
to write the condition directly in here.
Let's get rid of this variable here,
and then let's come here and get this string
that we want to then log to the console.
So console.log,
and so this makes sense, right?
If BMIMark is greater than BMIJohn,
then we log to the console Mark's BMI is higher than John's,
and if not, then we use the else statement.
Let's copy this one here.
In this case, John's BMI is higher than Mark's,
and actually let's log both of them
to the console here first,
just so we see if our if/else statement
is working as expected.
So BMIMark and BMIJohn.
As you see here, the VS code autocompletion is pretty smart.
Give it a safe, let's try that,
and we see John's BMI is higher than Mark's.
So this one is Mark's, this one is John's,
and indeed John's BMI is higher.
Basically this condition here turned out to be false
and the else block was executed.
Let's try the same now with this first set of data here,
and I'm commenting them out again using command slash
or control slash on Windows.
I need to comment out this one,
okay, give it a safe,
and now we should see the opposite.
Indeed, now Mark's BMI is higher than John's.
That is part one,
and now part two is to actually write a template string,
or this would be called a template literal.
We just fix that.
We should use a template literal
to include the actual BMI values,
so like this here between parenthesis.
So, let's do that.
We're going to transform this to backticks.
That's the first thing,
and then let's see the template again.
Okay, so after the BMI we include two parenthesis
and then in the parenthesis
is where we want the actual value.
This is where we put essentially the placeholder.
Here we will put BMIMark
and here BMIJohn.
Now let's just copy this to make it a bit faster.
Mark, and down here, John.
Okay, let's see.
Yeah, that works just fine.
Cool, so with this we solved the challenge,
and again, if you did it in a slightly different way,
don't worry at all.
That's completely normal and it's actually a good thing.
It means that you're basically finding
your own style of coding, which is a really a good thing
so don't worry about that.
What matters is that you actually used an if/else statement.
That was, of course, mandatory.
Otherwise you couldn't have made this work at all.
All right, so this was a lot of fun
because we actually made our code do something now.
I hope you can really feel that as well.