I am attempting to search through three separate arrays of strings using Javascript. The goal is to find a user-inputted name and determine in which array it is located.
Here is an example of what I am trying to achieve: HTML
let users = ['mario', 'gianni', 'pinotto'];
let admins = ['moana', 'cicciolina', 'selen'];
let mods = ['frodo', 'sam', 'bilbo'];
const form = document.querySelector('form');
const btnName = document.querySelector('#name');
let response = document.querySelector('#response');
function search() {
response.innerText = '';
let name = btnName.value.trim();
for (i = 0; i < mods.length; i++) {
if (name == mods[i]) {
response.innerText += `${name} is a moderator`;
break;
} else if (i == users.length - 1) {
for (i = 0; i < admins.length; i++) {
if (name == admins[i]) {
response.innerText += `${name} is an admin`;
break;
} else if (i == users.length - 1) {
for (i = 0; i < users.length; i++) {
if (name == users[i]) {
response.innerText += `${name} is a registered user`;
break;
} else if (i == users.length - 1) {
response.innerText += `${name} is NOT registered`;
break;
}
}
}
}
}
}
form.reset();
};
<form>
<label for="text">Insert name</label>
<input id="name" type="text" name="text" required/>
<input type="button" onClick="search()" value="search">
</form>
Unfortunately, this code is not functioning as intended and is causing the browser to freeze. I suspect I have created an infinite loop somewhere... any suggestions? Thank you.
Big thanks to everyone for their helpful responses.