0

I can not figure it out why this function doesn't return true...


function isTheTeamPlaying(teamID, meetings) {

  meetings.forEach( element => {

    if (element.includes(teamID.toString())) {

      console.log(element); //Return '0-1' and '1-2'
      return true;
    }
  })
}


console.log(isTheTeamPlaying('1', ['0-1', '0-2', '0-3', '1-2'])); //Return undefinded

I was expecting the second cosole.log() to return true

  • `return meetings.some(meeting => element.includes(teamID.toString()));` should do the trick –  Nov 07 '22 at 12:58
  • Note that with the logic you're trying to implement now `isTheTeamPlaying("1", ["11-2"])` will return `true` which is incorrect, since it's team 11 playing, not 1, but `"11-2".includes("1")` is `true. – VLAZ Nov 07 '22 at 13:00
  • `forEach()` is a consumption behavior, it does not return any value – 山河以无恙 Nov 07 '22 at 13:03
  • @VLAZ I didn't think about that thank you, should I use regex instead ? – Johan Testas Nov 07 '22 at 15:53
  • @山河以无恙 Alright so here is the answer thank you – Johan Testas Nov 07 '22 at 15:55
  • @JohanTestas you could. But you don't *have to*. Non-regex way would be something like `element.split("-").includes(teamID)`. But you could also just not pass strings but some other data structure like arrays instead. – VLAZ Nov 07 '22 at 15:56
  • [Any time](https://www.youtube.com/watch?v=qYH2qYyCii4) –  Nov 07 '22 at 16:20
  • @VLAZ thank you !! meeting.split('-').includes(teamID.toString()) resolved the issue – Johan Testas Nov 08 '22 at 08:27

0 Answers0