it doesn't work. it always goes with the else. i need to use array, and see if a string contains any of the words of the array. please help, this is really annoying
function buttonClick() {
var name = document.getElementById('myText').value;
var yourstring = name;
var substrings = ['fruit', 'apple'];
var length = substrings.length;
while (length--) {
if (yourstring.indexOf(substrings[length]) != -1) {
var outcome = 1;
} else {
var outcome = 2;
}
}
switch (outcome) {
case 1:
document.getElementById('fruitvalue').innerHTML = name + 'is ...';
break;
case 2:
document.getElementById('fruitvalue').innerHTML = name + 'is not ...';
break;
}
}
<body>
<center>
Last Name:<input type="text" id="myText" value="">
<button
onClick="buttonClick()"
style="font-family:font; color:blue;"
>Submit</button>
<h2 id="fruitvalue"></h2>
</center>
</body>
</head>