I'm new to web design, just learning CC now. now trying to program my own text game js, in terms of learning js. I am stuck with arrays. I have a predefined array with strings that needs to be compared with the user's response, but the comparison was not performed.
var myArray = ['selection 1', 'selection 2', 'selection 3', 'selection 4', 'selection 5', 'selection 6', 'selection 7', 'selection 8'];
alert("text description of scene and dimensions");
var dmg_start = Math.floor((Math.random() * 50) + 1);
var user = prompt("make a selection").toLowerCase();
first idea how to do it:
if (user.indexOf(myArray) > 0) {
console.log(user); // or mb document.write?
} else {
var user = prompt("make a selection").toLowerCase();
}
alert(myArray + "you can choose following");
Second idea:
var find = function (myArray, user) {
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] == user) {return i;
}
}
return null;
};
Third idea:
do
{
var user = prompt("make a selection").toLowerCase();
}
while (myArray.indexOf(user); // in idea, here must be checking for existence user given value in array
alert(myArray + "you can choose following");
also, I think that you can take a break after the first incorrect data entry with the alert alert (myArray + "you can choose the following"); to help the user make a decision, then use a continuation loop.
In some cases, I have two iterations of the loop, then an interrupt loop, even if the user gives the wrong (not contained in the array) value in the loop.
. ?
.