0

I got stuck for a while, can not figure out how to fix this code. any help is appreciated

let firstArray = ["oranges", "milk", "eggs", "chocolate"]; // First Array
let secondArray = ["milk", "popcorn", "chocolate", "ham"]; // Second Array

let newArray = []; // New Array that will hold the values that are NOT in other array
debugger;
for(let i = 0; i < firstArray.length; i++){ 
    for(let j = 0; j < secondArray.length; j++){
        if(firstArray[i] !== secondArray[j]){ 
            newArray.push(newArray[i]);  // If oranges != milk > add new item > newArray = ['oranges']
        }else if(firstArray[i] !== secondArray[j]){
            newArray.push(newArray[j]);  // If milk != oranges > add new item > newArray = ['oranges']
        }
    }
}

console.log(newArray); // Print the new array
Maryan
  • 19
  • 7

1 Answers1

2

Take a look at the includes() function here.

let firstArray = ["oranges", "milk", "eggs", "chocolate"]; // First Array
let secondArray = ["milk", "popcorn", "chocolate", "ham"]; 
let newArray=[];
for(let i = 0; i < firstArray.length; i++){ 
    if(!secondArray.includes(firstArray[i])) {
    newArray.push(firstArray[i]);
    }   
}
console.log(newArray);
Will Metcher
  • 331
  • 1
  • 11