I have some fruits names stored in an array. If the user enters a food name which is already stored in the array then true should be returned, otherwise false.
But it only returns true when I input the value Mango, otherwise always returning false, why?
Here's my code:
HTML:
<input type="text" id="value" />
<button onclick="check()">test</button>
<p id="pValue">
</p>
JavaScript:
var myVar;
check = function() {
myVar = document.getElementById("value").value;
var fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
for(i = 0; i < fruits.length; i++) {
if(myVar == fruits[i]) {
document.getElementById("pValue").innerHTML = "true";
}
else {
document.getElementById("pValue").innerHTML = "false";
}
}
}