-1
let scoreDolphins = [96,108,89];
let dolphinsAv = 0;
let scoreKoalas = [88,91,110];
let koalasAv = 0;

let findScoreAvD = (scoreDolphins) =>{
    for(let i = 0; i < scoreDolphins.length; i++){
        dolphinsAv = dolphinsAv + scoreDolphins[i];
    }
    dolphinsAv = dolphinsAv /3;
    //console.log(dolphinsAv);
    return dolphinsAv
}


let findScoreAvK = (scoreKoalas) =>{
    for(let i = 0; i < scoreKoalas.length; i++){
        koalasAv = koalasAv + scoreKoalas[i];
        
    }
    koalasAv = koalasAv / 3;
    //console.log(koalasAv)
    return koalasAv;
}

function announceWin (AvK, AvD){
    //console.log(koalasAv);
    //console.log(dolphinsAv);
    const averageK = AvK;
    const averageD = AvD;
    console.log(averageK);
    console.log(averageD);
    
    if(averageK > averageD){
       console.log("Koalas win the trophy"); 
    }if(averageK < averageD){
        console.log("Dolphins win the trophy");
    }if(averageK === averageD){
        console.log("Both win the trophy");
    }else{
        console.log('didnt work')
    }
    

// switch(averageK, averageD){
//     case(averageK > averageD): 
//         console.log("Koalas win the trophy");
//     break;

//     case(averageK < averageD): 
//         console.log("Dolphins win the trophy");
//     break;
    
//     case(averageK === averageD): 
//         console.log("Both win the trophy");
//     break;
//     default: console.log('did not work');
    
// }
}

announceWin(findScoreAvK(scoreKoalas), findScoreAvD(scoreDolphins));

I expected the switch to work similarly to the if statement, why is it not the same logic here? its been bugging me for like an hour now and i cant figure it out... its for a coding challenge

CHALLENGE #3 There are two gymnastics teams: Dolphins and Koalas. They compete against each other 3 times. The winner with the highest average score wins a trophy!

Your tasks:

  1. Calculate the average score for each team, using the test data included below. The average score for Dolphins should be assigned to the scoreDolphins variable, and the average score of Koalas should be assigned to the scoreKoalas variable.

  2. Compare the team's average scores to determine the winner of the competition, and print to the console:

"Dolphins win the trophy" if Dolphins win, or

"Koalas win the trophy" if Koalas win, or

"Both win the trophy" if their average scores are equal.

TEST DATA: Dolphins scored 96, 108, and 89. Koalas scored 88, 91, and 110.

  • 2
    That's not how a switch statement works at all. Your cases are either `true` or `false` and you're comparing `averageD` to them... – DallogFheir Aug 11 '23 at 21:08
  • thast not true, see here: https://stackoverflow.com/questions/9235152/can-i-use-a-case-switch-statement-with-two-variables – Joniras Aug 11 '23 at 21:10
  • Your switch cases ALL work together, whereas your 'else" statement is only tied to your last if-statement. Regardless of the first two if-statements, if your last-if statement is false, the "else" will fire. In your switch, the default only fires if none of the previous cases are true. – mykaf Aug 11 '23 at 21:10
  • I tried to write an answer to explain this, but the question got closed! I encorage you to look at [the documentation for switch statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch). You can't put two values into a switch statement like you tried: `switch(averageK, averageD)` It's only meant to take a single variable and then you have different "cases" for what that variable value is – Chris Barr Aug 11 '23 at 21:11
  • @ChrisBarr his two values in the switch statement get evaluated with the comma operator (var1, var2)-> var2, therefore only one (in his case averageD) gets used to compare to a value in the case statements. – Joniras Aug 11 '23 at 21:16
  • have a look to https://stackoverflow.com/questions/65649146/switch-case-in-js/65649168#65649168 – Mister Jojo Aug 11 '23 at 21:41

0 Answers0