javascript - js. check if array have a user-specified value thru prompt -


i newbie in web-design, learning on cc. trying program own js text game, in part of studying js. stuck arrays. have predefined array strings, need compared user answer, comparing did not doing.

    var myarray = ['selection 1', 'selection 2', 'selection 3', 'selection 4', 'selection 5', 'selection 6', 'selection 7', 'selection 8'];     alert("text description of scene , dimensions");      var dmg_start = math.floor((math.random() * 50) + 1); // pre-start damage dimension, used further     var user = prompt("make selection").tolowercase(); 

first idea, how it:

if (user.indexof(myarray) > 0) { console.log(user); // or mb document.write? } else { var user = prompt("make selection").tolowercase(); } alert(myarray + "you can choose following"); 

second idea was:

    var find = function (myarray, user) {     (var = 0; < myarray.length; i++) {              if (myarray[i] == user) {return i;                  }       }        return null;    }; 

third idea is:

do {     var user = prompt("make selection").tolowercase(); } while (myarray.indexof(user); // in idea, here must checking existence user given value in array alert(myarray + "you can choose following"); 

also, think possible break after 1st wrong data input alert alert(myarray + "you can choose following"); user make decision, use continue loop.

in cases got 2 iterations of loop, break loop, if in loop user gives wrong (non contained in array) value.

but both useless. way can suggest solve problem?

big attention , helping me know more.

try this...

var somearray = ['selection 1', 'selection 2', 'selection 3']; var someindex = -1;  {     var someuser = prompt("make selection").tolowercase();     (var = 0; < somearray.length; i++) {         if (somearray[i] === someuser) {             someindex = i;             break;         }     }      if (someindex > -1) {         alert('found @ index: ' + someindex);     } else {         alert('not found: please try this...');         // or more put here     } } while (someindex === -1); 

Comments