Ad
  • Custom User Avatar

    I also thought of using a switch, but as you can see by the comments here, using switch(true) and then doing evaluations in each case is kind of an anti-pattern that can be hard to read and debug.
    I would avoid and look for one of the other conditional options in this board.

  • Custom User Avatar

    To further clarify for those still confused on why passing in switch(value) doesn't work, it's because it's comparing a Boolean to a numerical one, which will always be false. Say our average is 95 and we're assigning that to a variable avg. If we try to pass in avg as the argument to the switch statement's parameter, this is essentially what is happening:

    switch(avg) {              //avg == 95
       case (avg >= 90):         //comparing 95 to true, which is false
         return 'A';
    

    (avg >= 90) is returning a Boolean. We'll never reach a switch statement that evaluates to true if we're passing in our avg variable as the argument. I hope this helps!

  • Custom User Avatar

    In your second example, you're dividing by 4 for the average instead of 3. The correct calculation would result in 5.33 which is greater still, than yourPoints.

  • Custom User Avatar

    Adding your points to the list can't change whether you are above average of not.

  • Custom User Avatar

    Because we are looking for one of the case condition results to be true :) Like, we have 87 as avg, so case (avg >= 90) gives you false.. next gives you true, so it will be used.

  • Custom User Avatar

    why we need swith(true) not switch(value)? I just don't understand

  • Custom User Avatar

    should I push my points to the array ? Since there is a note : Your points are not included in the array of your classes points. For calculating the average point you may add your point to the given array!

  • Custom User Avatar

    Your points are not included in the array of your classes points. For calculating the average point you may add your point to the given array!