I'm experiencing some challenges with retrieving a pre-declared array based on user input. This task is designed to help me grasp how to capture user feedback using an HTML drop-down and then return a value using a script.
What I aim for in the second part of the script is to retrieve the array that corresponds to the string selected by the user.
/* The current method successfully fetches the required array, but it involves placing them inside an object and returning them based on the key,
something I'd prefer to avoid. */
const obj = { att: [1, 1, 1, 1], btt: [2, 2, 2, 2], ctt: [3, 3, 3, 3], dtt: [4, 4, 4, 4] };
function getOption() {
let choice = document.getElementById("mySelect").value;
function pick(name) {
return obj[String(name)];
}
document.getElementById("demo").innerHTML = pick(choice);
// For drop-down choice "att", output will be "[1,1,1,1]".
}
/* In this section, even though the arrays are named One and Two respectively, and I am returning a string that represents either
"One" or "Two", JavaScript doesn't seem to recognize these as array names and simply returns the strings themselves. */
const One = ['a','a'];
const Two = ['b','b'];
function getSecondOption() {
let choice = document.getElementById("mySelectZwei").value;
function pick(name) {
return String(name);
}
document.getElementById("demoZwei").innerHTML = pick(choice);
// For drop-down choice "One", output will be "One".
}
I have attempted to remove the use of String() in the second segment, but without any impact. The Array.isArray() method also failed to produce results, indicating that the editor does not establish a linkage between the array names and the strings.
I would greatly appreciate any guidance on what areas to explore further or suggestions for potential solutions.
Thank you very much!