javascript

Write a function to check if milk exists in your array

var items = ["milk", "bread", "sugar"];

function checkForProduct(item) {
  if (items.indexOf(item) === -1) {
    console.log("item does not exist");
  } else {
    console.log("item is in your list");
  }
}

checkForProduct("socks"); //item does not exist
checkForProduct("milk"); //item is in your list
Output
checkForProduct("socks"); //item does not exist
checkForProduct("milk"); //item is in your list
Was this helpful?