OK, welcome back.
Now we're going to do the solution for Section four code challenge, so let's take a look at a few possibilities
now.
This could be anything, of course.
So let's just start with our statement.
Let's get a page here and let's just put thirty three.
So a regular if statement would be something like if age is greater than thirty three, then console
log.
I am now middle aged and maybe middle age is a little bit older.
Let's just go with it and then else.
We could just say
that long.
I'm still young yet.
All right, so let's just try that out.
And I would get I'm still young yet because the age is set to thirty three and that's not more than
thirty three, let's change changes to forty.
OK, so let's go forty five.
And we test that now.
Middle aged Bactine.
Thirty three.
I'm still young yet.
OK, great.
So we can do something else with that.
Thirty three.
We could say if we could start with this same if and do it agisting else if in the middle elusive age
equals thirty three.
Hey, that's how old I am.
And then else we can keep this email statement, OK?
All right, so since our age value is thirty three, that's what we get if we change to thirty two,
it would be the same as the previous one.
Now, since it's thirty three, it's it's falling into this elusive condition right now.
Let's convert these into ternary statements.
So how do we convert this first one into a ternary statement.
Well we would have to take this first conditional expression right here.
And then you do questionmark and then we do the one case and then we do the other case.
I would go.
All right, so that works right there.
How would we do the same thing with the ternary in here?
See, this gets a little trickier now that we have else, if so, we're going to have to do some nesting
of some of this ternary.
So first we check the first condition and we see if that's true.
Then here's what we get to say.
If it's false, though, we're going to check for another expression, another ternary.
So we're going to nest another ternary in here, which we could do inferences just so that we can read
this a little bit more easily and we can say
age questionmark.
Cooling.
Console log, oh, we can do to do this one.
Right there and do the calling there.
So this might be a little bit hard to read, but basically what you can do is read it like this.
Is the age over 40, if so, console, I'm now middle aged, if not, is the age thirty three, if so,
Consolo like that's how old I am and if not, I'm still young yet.
Let's give that a shot.
All right, so this worked.
So this is a repeat of that one, and that's in a ternary expression, so you could do it all in a single
line, that would be totally fine.
OK, now for number four, we've got to take our, if else, if else, statement and make it into a
switch statement.
So we're going to take this right here and reimagine this as a switch statement.
Now, if we were to do what we did last time, we would take a switch and then age and then check it
against specific values that wouldn't help us with the range that we need to that we need to deal with.
So this greater than 40, we're going to have to see if this is true instead of just checking for a
specific value.
So let's change this to switch.
True.
And then we can do a case where age is greater than 40
and then log that break.
And then case where age is
thirty three
and then comes a log that in break.
And then default, and that's the same as the statement.
We go right there.
So now if we save that, that'll give us the same behavior there.
Notice these are thirty three, so that accounts for that one, that one and that one, if we make this
thirty two, I'm still young yet if we make this forty five, I'm now middle aged.
Fantastic.
OK, so there's our example of the Section four code challenge and welldone.