I am searching for an value from an array in javascript and this is my code.
HTML
<input type="text" placeholder="enter here" id="textvalue">
<button id="checkbutton">check</button>
Javascript
let arr = ["boy", "cat", "dog"];
let txtval = document.querySelector("#textvalue");
let chkbutt = document.querySelector("#checkbutton");
chkbutt.addEventListener("click", arraycheck);
function arraycheck () {
for(i=0; i<arr.length; i++) {
if(txtval.value==arr[i]) {
alert("match found for " + txtval.value)
}
else {
alert("its not there")
}
}
}
At the moment, I keep getting alert saying "its not there" when I enter "dog". It prints "its not there" twice and then prints "match found for dog". I would like the alert to show only if the word does not exist in the array. I know it is doing this because I have the if statement in the for loop and each time if the index of the array does not match, it shows the alert. But how do I do it?