I'm new to JavaScript and need some help with arrays.
My goal is to verify if the user's input value exists in an array named "fruits" that I have declared. If it does, I want to run a specific piece of code. However, if it doesn't exist in the array, I'd like to display an alert message. I attempted to use the
instanceof
method to check for the value, but none of the conditional statements seem to execute properly. Any insights on what might be going wrong?
$("#submit-btn").bind("click", function() {
var comment = $("#comments");
var commentValue = $.trim(comment.val());
var index;
var fruits = ["Banana", "Orange", "Apple", "Mango"];
for (index = 0; index < fruits.length; index++) {
text += fruits[index];
if (commentValue.length === 0) {
alert('Comments are required to continue!');
}
else if (fruits.includes(commentValue)) {
// Execute code
});
}
else {
alert('Not a valid fruit');
}
return false;
}
});