Jonas: So I really hope that you gave
the challenge a try, and, of course,
that you managed to do it.
So here goes my solution in case
you're wondering how I did it.
I'm gonna start by creating the tip calculator function.
Okay so function, and I'm gonna call it tip calculator.
Then in here you pass a value, which I call the bill,
and then we have our function body.
Okay now, we want to, depending on the bill value,
to determine the percentage of the tip
that John likes to pay.
So here we have our rules, again,
so 20% if the bill is less than $50,
15% if it's between 50 and 200,
and then 10% if the bill is more than 200.
So I'm gonna define var percentage here,
and then I'm gonna leave it.
This L statement is where I'm actually gonna define
the value for the percentage depending on the bill.
So if the bill is less than 50,
then the percentage should be 20.
Or actually, it should be .2.
So we could write 0.2, but we
don't even need the zero here.
So just .2, because that's actually the value
that later we are going to multiply with the bill.
Okay so remember, to complete %20 of a value,
we multiply it by 0.2 in this case,
because that's 20 divided by 100.
Alright, so else if the bill is greater
or equal to 50, end if the bill is less than 200.
So that's very similar to the
stuff that we already did before, right?
So this should be nothing for you at this point.
So in this case, our percentage is gonna be .15,
and else, the percentage will be .0.
Or actually, .1, sorry.
So .1, which is 10%, okay.
And in this case you don't even
have to specify the if here,
because if the bill is not less than 50
and not between 50 and 200, well,
then that means that it's more than 200.
And so this here covers all of that.
So all we have to do now is to return
this percentage times the bill.
So for example, if our bill was $100,
then we would divide it with .15,
which would then be $15.
So we would return $15 from this tip calculator function.
Alright, now you could, of course,
have done this differently.
For example, you could have done a switch
like we did before for example, where is it?
So something very similar to, here is it.
So you could have done something similar to
this one here for example as well,
where in each case you would then
return the bill times the current percentage.
And if it works fine just like this.
So just to tell that there are always different solutions
for the same problems, okay?
And now I just want to test this here with some value.
So tip calculator, and let's set it to 100.
And we need to console log it.
So again this is just for testing to see
if our function is actually working just fine.
So this here should return 15, right?
And indeed, it does, so that's 15% of 100,
which is between 50 and 200.
Now let's try it with 300,
and we're gonna take 10% of that, which would be 30.
So let's test, and indeed, it works.
And now let's put, let's say, $10,
which is of course, less than 50,
and so two percent of that should be two dollars.
And yeah, indeed it is two.
And so that means that our tip
calculator is actually working.
So all we have to do now is to create
these to erase as John wants the results.
So the first thing I'm gonna do is to
actually start a bills, and to an array,
so we have 124, we have 48, and then 268 I believe.
Let's just confirm that, and yeah.
So these are the bills that we have,
and then let's calculate the tips.
So the first tip is calculated by using tip calculator
with the bills on position number zero, right?
Then a second one, and actually
let's put it in a separate line here.
So the second one is tip calculator
bills on element one, and then finally,
the last one is calculating the tip
with bills at position number two.
Okay, so it has created an array here
which contains these three values,
which all come from the result of
calculating the tip using our tip calculator function.
Alright, so let's log it to the console here,
and so here we have our tips.
Now this here is something weird that is happening
with the 9.6 and 001,
and this is something weird
that javascript does with numbers,
and no one really knows why this is happening.
So it should just be 9.6,
and the same is actually happening here with this
59999998, and this of course, should be 18.6.
But that's nothing we can change,
that's not our fault so javascript just
does these weird calculations here
that do not really make sense, so don't worry about them.
Okay so this is the first array that we want
so the three tips, and then finally,
we want one containing all three final paid amounts.
And so that's also very easy,
let's say final values, so another array.
And here we have to say bills at position zero
plus the tips at position zero.
And that's it, so very simple.
Let's just grab this here, and
here it's one, and here it's two.
And so this is actually all that we
need to do in order to have our final results.
Now you see that actually, here now we have
this .6 and this .6 coming from these two.
So this is really weird, again, don't worry about it.
Anyway what we really wanna see here
is that this 142.6 is really the sum
of the original bill which is 124 plus this 18.6.
And the same of course for the others.
So 57.6 is 9.6 plus 48,
and the same here for this last element.
And with that the challenge is completed.
And you could have done it very differently of course.
For example, you could have declared empty arrays
in the beginning and then use the push method
to add all of these results to the array.
But again, it's not really important that
your code is the same as mine, okay?
I cannot stress this enough.
So what i really want here is to write code
and to think about how to solve these problems.
And if the results that you get are the same as mine,
well then that's really fantastic.
So you've come a long way in this section,
but there's still a couple of more things to learn.
So let's now move on.