(Javascript) how can i access the array (Upper & lower case) via if i put characters on (lowercase) prompt

Viewed 29

in Javascript i want the action is if i put character on the prompt some name(full lowercase/full Uppercase/mixed case ) i can't access the array with desired input.

i only can access with exact typing. i want to access the array with mixing case / usuval typing like lower case

var name = prompt('enter your name');

var nameList = ["Jennifer","Johnson","Natalie","Robinson","John","Ferguson","Samuel","Patterson"];

if (nameList.includes(name)){
    alert('Hi you can enter');
} else {
    alert("You can't enter");
}

thank you

2 Answers

Make all searches lower or upper case

And instead of includes use Array.find

if(nameList.find(itemName)=> itemName.toLowerCase() === name.toLowerCase()) {

...

}

Try to lowercase both, the prompt and the array items before include

const name = prompt('enter your name');
const nameList = ["Jennifer","Johnson","Natalie","Robinson","John","Ferguson","Samuel","Patterson"];
const nameWithLowerCase = nameList.map(n => n.toLowerCase())

if (nameWithLowerCase.includes(name.toLowerCase())) {
  alert('Hi you can enter'); 
} else {
  alert(" You can't enter");
}

Related