0

I have a line of code that is supposed to act as a check for if the user inputs a value that isn't within my array.

I made an array with all valid inputs and then made and if statement that checks this.

var productCode = ["LT","ST","DC","LC","PR","SP"];

var productChosen = prompt("Choose a product code, LT, ST, DC, LC, PR, or SP");

if ( productChosen === productCode) {

etc..
}

else {

alert("Please input a valid product code");
}

It always goes to the else statement.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130

1 Answers1

1

The productChosen is a string. The productCode is an array. These types can never be equal. What you are looking for is includes() to check if the string is included in the array.

if(productCode.includes(productChosen)) { ... }
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56