I have a problem with this code:
var buyerChoice = prompt("Enter a either apple, orange, or banana:", "");
var fruits = new Array ("apple", "orange", "banana");
for(i=0; i < fruits.length; i++) {
if(buyerChoice === fruits[i]) {
document.write(buyerChoice);
} else if (buyerChoice !== fruits[i]){
document.write("Sorry, " +buyerChoice+ " is out of season.");
break;
}
}
I believe the problems lies in the else-if statement because every time I type in an item that exists in my variable, it will return //appleSorry, apple is out of season, thus meeting both conditions.
I’m stumped. I guess the bottom line is how to effectively match a string from a prompt to an array, testing each item and how to parse an array if a prompted string doesn’t exist.
1 Answer