this seems like it should be really basic but I'm having issues with it. I'm just starting out so please forgive me if this is answered in other posts - i wasn't able to find anything I know how to apply to my specific issue...I'm wanting to check for numbers 1-4 in an array then build a new array with true or false elements being results of the search...in JS
function linearSearch(vector, x) {
for (i = 0; i < 4; i++) {
if (vector[i] == x) {
return true;
}
}
return false;
}
function checkall() {
var test = [2, 4, 1, 3];
var collect = [];
for (i = 0; i < 4; i++) {
var ck = linearSearch(test, i + 1);
collect[i] = ck;
}
return collect;
}
console.log(checkall());
var ito make the variable local to each function. Otherwise, when you calllinearSearchit changes the value ofiin thecheckall()loop.