Just starting out with Javascript and striving to write clean code! To test my app, I aim to have a user's name input via prompt checked against an array for validation purposes.
When I hardcode the value of a variable, the includes function filters as expected:
var name, excluded_Persons;
var name = "jim"
function denyEntry () {
var excluded_Persons = ["bob", "jim"];
if (excluded_Persons.includes(name)) {
alert("You may not enter!!");
enterName ();
}
else {
allowEntry ();
}
}
denyEntry ();
function allowEntry () {
alert("You have been granted access!!");
}
This setup correctly displays "You may not enter!" However, the issue arises when incorporating a function that allows users to input their name:
var name, excluded_Persons;
function enterName () {
var name = prompt("What is your name?").toLowerCase();
}
enterName();
function denyEntry () {
var excluded_Persons = ["bob", "jim"];
if (excluded_Persons.includes(name)) {
alert("You may not enter!!");
enterName();
}
else {
allowEntry();
}
}
denyEntry();
Surprisingly, regardless of the inputted name (even "bob" or "jim"), the message displayed is "You have been granted access!" It seems like the includes function is being disregarded.
Could there be a missing argument in either the enterName or denyEntry functions?