16  Coding Challenge 2 Solution

16 Coding Challenge 2 Solution

*** The #1 bestselling JavaScript course on Udemy! ***

“Excellent course. Jonas explains the core concepts in javascript that are usually glossed over in other courses. And he does it in a manner that is clear and concise.” — John Collins

Do you want to learn the number #1 programming language that powers the internet?

Are you tired of wasting your time and money on random youtube videos or JavaScript courses that are either too simple, or too difficult to follow?

Or are you struggling to deeply understand and apply JavaScript to real projects?

If your answer is a big YES… Then this is exactly the course you are looking for!

so here goes my solution to coding challenge number two so let's start by calculating the average scores so far score John is equal to and we have two points so 89 plus 120 plus 103 divide it by 3 then var score Mike is equal to 116 + 94 plus 123 divided by 3 again and now let's simply lock these to the console score John and score Mike and here we go so the average score for John 104 and for Mike 111 so Mike is clearly the winner here and so let's now lock that to the console okay and so what we're gonna do here is of course an if-else statement so give it some space here and now if score John is greater then score Mike well then log to the console that John's now we need to escape this quote here so John's team wins with and now let's also include the score here as I mentioned in the instructions so score John points alright and else if if it's yet a way around basically so score Mike is greater 10 score John well then that's of course log dead Mike's team wins then here we also need to use the score of Mike okay and then else and that's the situation where there is a draw so if John score is not greater than Mike's and Mike's core is also not greater than John well then it means that they have the same score okay and so that is here this else block and so in here we can simply lock that there is a draw so let's test it out and so yeah Mike's team wins with 111 points let's now increase this year let's say to 129 and so not probably John wins and yeah here we go so now John team win now let's try to have an a tie here so we had 89 before let's edit put it to 95 and we need some more points here so let's increase this 215 now that was a bit too much 112 maybe and now we're almost there I guess yeah so now they have the same scores so both have 111 and so now we see that there is a draw and so of course none of these situations here apply because John score is not greater than Mike's score at Mike score is not greater than John's score and so of course we have a draw so that is the easier part now let's also add Mary to the game so far score Mary equals and let's get her points so 97 134 and 105 this should be a comma obviously and now this should be plus signs of course sorry for that so we add all of them together and then divide it by three also log it to the console just to make sure so she has 112 do let's put back what we had initially so it was yeah 89 and so now we have these three different scores alright so let's get rid of this part and I'm not going to delete it oh just comment it out and okay so how can John win the game basically he has to have more points than Mike and also more points than Mary right and so that's what we have to translate into code now so an if-else statement and we have to say that score John should be greater than score Mike and score John should be greater than score Mary okay and that's it so does that make sense that is a great use case of the end operator here and it works like this because we need this year to be true and at the same time we need this to be true so John score needs to be greater than both Mary's and Mike's all right and so only in that case we will want to print that John's team wins the game and then from here it's actually pretty straightforward so once we understand how the logic work all we have to do is to do the same for the other players so how does Mike win the game well of course he has to have a larger score than the other two so score Mike needs to be greater than scored on and at the same time score Mike and what happened here so score Mike greater then score Mary so John and here we have Mike and of course score Mike and and finally the same for Mary so score Mary needs to be larger than score John and score Mary greater than score Mike you know we say that Mary's teams win and here the score Mary Mary all right now what about the other situations well let's just use an else here and say that there is a draw okay so this doesn't really cover all the different situations here but that's not a big deal so for this small example it's okay imagine that Mary and John had both the same score but the largest court in mics so they too would have a tie but would still have more points than Mike and so that's one of the situations in which we will enter this else block and display that there is a draw okay so these three situations here only work for a clear winner all right but again that's not a big deal we don't need to cover every single situation here so these are the more importance of course where we can define which one is the clear winner so let's try it out now and indeed Mary's team wins with 112 points let's add some points here to Mike and if we add three points then he will have the same score as Mary so let's put his tonight in here and they now have both 112 and so we see that there is a draw so perfect and if he now increases even further let's say 29 well then of course Mike's team has the most point and now finally let's add some more points here to John just to test if all three cases work and indeed now yes the most points and he is declared the winner right so that's it I hope you figure it out and so now you are really ready to finally move on to the next part of this intersection which is the hugely important topic of functions so join me right there in the next video

Leave a Reply