My objective is to achieve the following:
- Assign an array value (list) to another array (options).
- If the user input (searchVal) matches a value in the list, remove existing options and add this match. Subsequently continue adding any additional matches without clearing the options array again.
Based on the code snippet below, if searchVal was "whatever"
, the expected output for options should be:
["whatever", "whatevEver1"]
. However, the actual result is: ["whatever", "WhatEver1", "whatttever", "whatever", "whatevEver1"]
Snippet of relevant code:
var list = ["whatever", "WhatEver1", "whatttever"];
var clear = 0;
var options = [];
for (var i=0 ; i < list.length ; i++)
{
options.push([list[i]]);
}
var searchVal = window.prompt(" ");
for (var i=0 ; i < list.length ; i++)
{
if (list[i].toLowerCase().includes(searchVal.toLowerCase())) {
if (clear == 0) {
options.length = 0;
}
options.push([list[i]]);
}
clear++;
}
return options;