0

not sure why this isn't working for me , i have a script embedded into about 100 different third party sites , each of those sites prints a var in its page source to determine its uniqueID

The var printed to each page is

var league_id = '43570';

the 5 digit number changes depending on the league_id for that site.

I want to execute a function for certain sites that have a league_id The following works fine , but my list may be 100 or more league_id

if ( league_id === '43570' || league_id === '10065' ) {
    console.log(league_id + " League Validated");
} else {
    console.log(league_id + " League Not Validated");
}

So i wanted to place in an array , but my below 2 efforts have failed

if ([43570,10065,61,3902].indexOf(league_id) > -1) {
    console.log(league_id + " League Validated");
} else {
    console.log(league_id + " League Not Validated");
}

if([43570,10065,61,3902].includes(league_id)) {
    console.log(league_id + " League Validated");
} else {
    console.log(league_id + " League Not Validated");
}
MShack
  • 642
  • 1
  • 14
  • 33

2 Answers2

2

From the MDN docs:

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).

You're comparing a variable of type number to one of type string.

Brendan Bond
  • 1,737
  • 1
  • 10
  • 8
1

ok dummy me , i realized i need to parse the number

const parsed = parseInt(league_id);
if ([43570,10065,61].indexOf(parsed) > -1) {
    console.log(league_id + " League Validated");
} else {
    console.log(league_id + " League Not Validated");
}
MShack
  • 642
  • 1
  • 14
  • 33