In this code snippet, I am attempting to retrieve results from an array and evaluate them one by one.
////////////////////////////////////////
/////////////// Code Issue ////////////////////////
///// The current implementation is not functioning properly, ///////
///// and lacks flexibility /////////////
////////////////////////////////////
var myA = ["said", "imad", "hassan", "bilal", "mohamed"];
var doc = document.getElementById('div1');
var inp = document.getElementById('input1');
function click1() {
'use strict';
var i = inp.value;
if (i <= 0) {
doc.innerHTML = 'Please enter a name';
} else if (i === myA[0] || i === myA[1] ||
i === myA[2] || i === myA[3] || i === myA[4]) {
doc.innerHTML = i;
} else {
doc.innerHTML = 'Please enter a correct name';
}
}
/////////////////////////////////////////////////////
////////////// Attempt to Improve Flexibility //////////////
///////// However, only the last index is being retrieved ////////////
/////////////////////////////////////////////////////
var myA = ["said", "imad", "hassan", "bilal", "mohamed"];
var index = myA[0];
// console.log(index);
var doc = document.getElementById('div1');
var inp = document.getElementById('input1');
function getId(a){
var aL = a.length, index = a[0], x=0;
for(x = 0; x < aL; x++ ){
index = a[x];
// console.log(index);
}
return index;
}
// console.log(getId(myA));
function click1() {
'use strict';
var i = inp.value;
if (i <= 0) {
doc.innerHTML = 'Please type name';
} else if (i === getId(myA)) {
doc.innerHTML = i;
} else {
doc.innerHTML = 'Please type in a correct name';
}
}