0

Hi I created a code that would print false if there is one max number in an array and true if there is two or more of the same max number. However, it is not working even though the following works:

let diceScores = [2,6,5,6];
let maxScore = Math.max(...diceScores);
diceScores.filter(x => x === maxScore).length > 1 ? console.log('tie') :console.log('no tie');

That is the code that I learnt that works. Now I modified it into my website as I needed to store the true or false into a var isTie; and it doesn't work...can someone please tell me why so i can learn too, thanks in advance.

My code (doesn't work):

var randomNumber1 =2;
var randomNumber2 =5;
var randomNumber3 =5;
var isTie; 
var userChoices = [];
var noOfChoices = 3

 if (noOfChoices === "3") {
    userChoices.push(randomNumber1, randomNumber2, randomNumber3);
    let maxScore = Math.max(userChoices);
    userChoices.filter(x => x === maxScore).length > 1 ? isTie = "True" : isTie = "False";
    console.log(isTie);
  }
  • `Math.max(userChoices);` is wrong, you need to spread the array into the `.max` call – CertainPerformance Sep 01 '20 at 07:36
  • @CertainPerformance You mean `Math.max(...userChoices);`? I tried that as well.. –  Sep 01 '20 at 07:37
  • 1
    You also define `noOfChoices = 3` but then test `noOfChoices === "3"` - a number won't be `===` to a string – CertainPerformance Sep 01 '20 at 07:38
  • @CertainPerformance Oh... my bad ok for some reason I thought numbers behave as numbers and not like letters in string I'm so sorry will work on it –  Sep 01 '20 at 07:39

0 Answers0