【发布时间】:2021-03-02 09:37:12
【问题描述】:
我卡了一段时间,不知道如何修复此代码。任何帮助表示赞赏
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
【问题讨论】:
标签: javascript arrays