noob需要帮助循环数组

时间:2018-06-08 20:52:14

标签: arrays loops

我是JavaScript的新手,我一直在读一本书以及复数课程。我按照指导者如何循环他的数组,但我不想使用确切的变量和值。当我尝试自己做的时候。我最终得到错误。即使我的代码看起来基本相同。 我想这里的问题是。我错过了什么?

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]);
}

1 个答案:

答案 0 :(得分:0)

你错过了一个加号:

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]);
}