-1

I'm trying to get the function loop() to return true or return false after it's done checking if values in one array are present in another array. I'm stuck with where the return statements have to go since I'm new to javascript.

aa = ["one", "two", "three", 'four'];
ab = ["one", "two", "three"];
loop = function() {
  aa.forEach(function(i) {
    if(!ab.includes(i)) { 
      console.log (i + ' does not exists in ab') 
      // return false
    } else {
      console.log (i + ' exists in ab') 
    }
  });
}

console.log(loop()) // should return false since 'four' is not included in ab

Right now loop() returns undefined. How does this have to be done to make loop() return true or false?

Norman
  • 6,159
  • 23
  • 88
  • 141

1 Answers1

1

forEach does not return any value. Also function loop is not returning any value.

You can just iterate the array and use some to check that value is present in the other array

const aa = ["one", "two", "three", 'four'];
const ab = ["one", "two", "three"];
aa.forEach((item) => {
  const isPresent = ab.some(elem => elem === item)
  console.log(isPresent)
})
brk
  • 48,835
  • 10
  • 56
  • 78