|
| 1 | +/* |
| 2 | +
|
| 3 | +Level: Beginner |
| 4 | +
|
| 5 | +CODING CHALLENGE 2 |
| 6 | +
|
| 7 | +
|
| 8 | +/* |
| 9 | +
|
| 10 | +John and Mike both play basketball in different teams. |
| 11 | +In the latest 3 games, John's team scored 89, 120 and 103 points, |
| 12 | +while Mike's team scored 116, 94 and 123 points. |
| 13 | +
|
| 14 | +1. Calculate the average score for each team |
| 15 | +
|
| 16 | +2. Decide which team wins in average (highest average score), |
| 17 | +and print the winner to the console. |
| 18 | +Also include the average score in the output. |
| 19 | +
|
| 20 | +3. Then change the scores to show different winners. |
| 21 | +Don't forget to take into account there might be a draw |
| 22 | +(the same average score). |
| 23 | +
|
| 24 | +4. EXTRA: Mary also plays basketball, and her team scored 97, 134 and 105 points. |
| 25 | +Like before, log the average winner to the console. |
| 26 | +HINT: you will need the && operator to take the decision. |
| 27 | +
|
| 28 | +5. Like before, change the scores to generate different winners, keeping |
| 29 | +in mind there |
| 30 | +might be draws. |
| 31 | +
|
| 32 | +
|
| 33 | +
|
| 34 | +SOLUTION 👇🏼 |
| 35 | +
|
| 36 | +*/ |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +// With comments: |
| 48 | + |
| 49 | + |
| 50 | +// 1. Calculate the average score for each team |
| 51 | + |
| 52 | +var johnAverageScore = (89 + 120 + 103) / 3; // 104 |
| 53 | +var mikeAverageScore = (116 + 94 + 123) / 3; // 111 |
| 54 | +var marryAverageScore = (97 + 134 + 105) / 3; // 112 |
| 55 | + |
| 56 | +// 2. Decide which team wins in average (highest average score), |
| 57 | +// and print the winner to the console. |
| 58 | + |
| 59 | +// 3. Then change the scores to show different winners. Don't forget to take into |
| 60 | +// account there might be a draw (the same average score) |
| 61 | + |
| 62 | + |
| 63 | +// 4. EXTRA: Mary also plays basketball, and her team scored 97, 134 and 105 points. |
| 64 | +// Like before, log the average winner to the console. |
| 65 | +// HINT: you will need the && operator to take the decision. |
| 66 | + |
| 67 | + |
| 68 | +// 5. Like before, change the scores to generate different winners, keeping in mind there |
| 69 | +// might be draws. |
| 70 | + |
| 71 | + |
| 72 | +if (johnAverageScore > mikeAverageScore && johnAverageScore > marryAverageScore) { |
| 73 | + console.log('John\'s team is winning with ' + johnAverageScore + ' points'); |
| 74 | +} else if (mikeAverageScore > johnAverageScore && mikeAverageScore > marryAverageScore) { |
| 75 | + console.log('Mike\'s team is winning with ' + mikeAverageScore + ' points!'); |
| 76 | +} else if (maryyAverageScore > johnAverageScore && marryAverageScore > mikeAverageScore) { |
| 77 | + console.log('Marry\'s team is winning with ' + marryAverageScore + ' points!'); |
| 78 | +} else { |
| 79 | + console.log('It\'s a draw!'); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
0 commit comments