I'm trying to search through an array using the .includes() method. I don't want an element that is indeed in the array to go undetected using the .includes() method due to case sensitivity. Therefore, I am attempting to use .toUpperCase() in conjunction with .includes()
I don't understand why my code is not working.
var euroTour = ["France", "the Netherlands", "the UK", "Spain",
"Portugal"];
var findCountry = euroTour.toUpperCase().includes('FRANCE');
I expect that using the code above, that true would be written to the document. However, nothing is written. When I remove the .toUpperCase() method altogether, as expected, false is written to the document since the subject of the search 'FRANCE' is not the same thing as 'France', an actual element of the array.
.toUpperCase()
can only be used on strings and not on whole array which you we were doing. Use map and capitalise
the strings in array and then check with
.includes
var euroTour = ["France", "the Netherlands", "the UK", "Spain",
"Portugal"];
var findCountry = euroTour.map((e)=>e.toUpperCase()).includes('FRANCE');
console.log(findCountry)