1. 程式人生 > >Section 2.2 CODDING CHALLENGE 1 ()

Section 2.2 CODDING CHALLENGE 1 ()

CODDING CHALLENGE 1

John and Mike both play backetball in different teams. In the lastest 3 games, John’s team scored 89, 120 and 130 points, while Mike’s team scored 116, 96 and 123 point.

  1. Calculate the average score for each team.
  2. Decide which teams wins in average (hightest average score), and print the winner to the console. Also include the average score in the output.
  3. Then change the scores to show different winners. Don’t forget to take into account there might be a draw (the same average score)
  4. EXTRA: Mary also plays basketball, and her team scored 97, 134 and 105 points. Like before, log the average winner to the console. HINT: you will need the && operator to take the decision.
    if you cannot solve this one, just watch the solution, it’s no problem.
  5. Like before, change the scores to generate different winners, keeping in mind there might be draws.

// 1.

var scoreJohn = (89 + 120 + 103) / 3;
var scoreMike = (116 + 94 + 123) / 3;
console.log("scoreJohn: " + scoreJohn, "scoreMike: " + scoreMike);

// 2.

if (scoreJohn > scoreMike) {
    console.log("John's team wins with " + scoreJohn + " points!")
} else if (scoreMike > scoreJohn) {
    console.log("Mike's team wins with " + scoreMike + " points!")

} else {
    console.log("There is a draw!")
}

// 3.

var scoreJohn = (110 + 120 + 103) / 3; //var scoreJohn = (111 + 120 + 103) 
var scoreMike = (116 + 94 + 123) / 3;
console.log("scoreJohn: " + scoreJohn, "scoreMike: " + scoreMike);

if (scoreJohn > scoreMike) {
    console.log("John's team wins with " + scoreJohn + " points!")
} else if (scoreMike > scoreJohn) {
    console.log("Mike's team wins with " + scoreMike + " points!")

} else {
    console.log("There is a draw!")
}

// 4.

var scoreJohn = (89 + 120 + 103) / 3; 
var scoreMike = (119 + 94 + 123) / 3; //var scoreMike = (116 + 94 + 123) / 3; Draw
var scoreMary = (97 + 134 + 105) / 3;

console.log("scoreJohn: " + scoreJohn, "scoreMike: " + scoreMike, "ScoreMary: "+ scoreMary + ". ");

if (scoreJohn > scoreMike && scoreJohn > scoreMary) {
    console.log("John's team wins with " + scoreJohn + " points!")
} else if (scoreMike > scoreJohn && scoreMike> scoreMary) {
    console.log("Mike's team wins with " + scoreMike + " points!")

} else if (scoreMary > scoreJohn && scoreMary> scoreMike) {
    console.log("Mary's team wins with " + scoreMary + " points!")

} else {
    console.log("There is a draw!")
}