菜鸟需要帮助循环数组



我对 JavaScript 很陌生,我一直在读一本书以及 pluralsight 课程。 我按照讲师循环数组的方式进行操作,但我不想使用确切的变量和值。当我尝试自己做的时候。我最终收到错误。即使我的代码看起来基本相同。 我想这里的问题是。我错过了什么?

let shirts = ["red shirt", "green shirt" , "blue shirt", "flanel", "black shirt"];
let jeans = [ "blue jeans", "shorts", "sweatpants", "khakis"];
let outfit = [];

for( let shirtsIdx = 0; shirtsIdx < shirts.length; shirtsIdx++) {
for ( let jeansIdx = 0; jeansIdx < jeans.length; jeansIdx++) {
outfit.push(jeans[jeansIdx] + " and " shirts[shirtsIdx]);
}
}
for ( let i = 0; i < outfit.length; i++) {
console.log(outfit[i]);
}

您缺少一个加号:

outfit.push(jeans[jeansIdx] + " and " + shirts[shirtsIdx]);

var shirts = ["red shirt", "green shirt" , "blue shirt", "flanel", "black shirt"];
var jeans = [ "blue jeans", "shorts", "sweatpants", "khakis"];
var outfit = [];
for( var shirtsIdx = 0; shirtsIdx < shirts.length; shirtsIdx++) {
for ( var jeansIdx = 0; jeansIdx < jeans.length; jeansIdx++) {
outfit.push(jeans[jeansIdx] + " and " + shirts[shirtsIdx]);
}
}
for ( var i = 0; i < outfit.length; i++) {
console.log(outfit[i]);
}

最新更新