0

//I want the behaviour from the first example to work with prompt(), as shown in the second example. indexOf() //does not seem to work with the prompt variable 'userInput'.

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let replaceGuestInput = 7;
let findIndex = arr.indexOf(replaceGuestInput);

//output 6. Intended behaviour.
console.log(findIndex);



let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const indexTest = () => {
    //I type the element I want to find the index of in the prompt.
    let userInput = prompt(`Type the arr element to find its index:\n${arr}`);
    let findIndex = arr.indexOf(userInput);
    alert(`${findIndex}`)
}


//output on index.html: -1. Unintended behaviour.
indexTest();

poeticider
  • 11
  • 1
  • The indexOf method does not find it because a string does not match a number. Prompt returns a string. `let userInput = +prompt(\`Type the arr element to find its index:\n${arr}\`);` – epascarello Nov 15 '22 at 15:29
  • Convert the input from `prompt` to a number. Currently, it's a string. – kelsny Nov 15 '22 at 15:29

0 Answers0