0

I'm trying to find a way to check if the word "miami" appears in this array and print out if it does or doesn’t. I'm not sur wether to use the for loop or forEach loop. How can I print "true" the fact that the word "miami" exists in the array bellow?

let word = "miami";
let arr = ["bcn", "mia", "sao", "mex", "par", "miami", "ams", "ber", "paris", "lis", "mad"];

for (let i = 1; i <= 11; i < arr.length; i++) {
  if (i === 21){
    console.log("miami");
    break;
  } else {
    console.log(i);
  }
}

  • You can do like `if(arr[i] === word){console.log("true")}` if you wanna use for loop. Otherwise there are many other easy ways to check if element is inside array or not. – Maheer Ali Mar 02 '20 at 13:55

1 Answers1

1

const word = "miami";
const arr = ["bcn", "mia", "sao", "mex", "par", "miami", "ams", "ber", "paris", "lis", "mad"];

const result = arr.find(item => item === word)

console.log(result);
cthmsst
  • 374
  • 1
  • 13