无法从父 for 循环记录值
Can't log value from parent for loop
JS代码
function generate(rectWidth, rectHeight, amount) {
// holds size
let size = {
width: [],
height: []
};
// holds colors
let colors = [];
for (var i = 0; i < amount; i++) {
// generate size
var width = Math.floor((Math.random() * rectWidth) + 1);
var height = Math.floor((Math.random() * rectHeight) + 1);
var r = Math.floor((Math.random() * 255) + 1);
var g = Math.floor((Math.random() * 255) + 1);
var b = Math.floor((Math.random() * 255) + 1);
// add size to object
size.width.push(width);
size.height.push(height);
colors.push(`rgb(${r}, ${g}, ${b})`);
}
return {
size: size,
colors: colors
};
};
let properties = generate(50, 50, 10);
for (let width = 0; width < properties.size.width.length; width++) {
for (let height = 0; height < properties.size.height; height++) {
for (let color = 0; color < properties.colors.length; color++) {
console.log(properties.size.width[width]);
}
}
}
问题
所以我的问题是,在我的代码中,我试图 console.log(properties.size.width[width]);
,但它没有记录,而且我没有收到错误消息。
我想要发生的事情
我希望我的代码 console.log()
properties.size.width
的值
我试过的
- 我试过在循环中使用
var
而不是 let
。
for (let height = 0; height < properties.size.height; height++)
应该是
for (let height = 0; height < properties.size.height.length; height++)
function generate(rectWidth, rectHeight, amount) {
// holds size
let size = {
width: [],
height: []
};
// holds colors
let colors = [];
for (var i = 0; i < amount; i++) {
// generate size
var width = Math.floor((Math.random() * rectWidth) + 1);
var height = Math.floor((Math.random() * rectHeight) + 1);
var r = Math.floor((Math.random() * 255) + 1);
var g = Math.floor((Math.random() * 255) + 1);
var b = Math.floor((Math.random() * 255) + 1);
// add size to object
size.width.push(width);
size.height.push(height);
colors.push(`rgb(${r}, ${g}, ${b})`);
}
return {
size: size,
colors: colors
};
};
let properties = generate(50, 50, 10);
for (let width = 0; width < properties.size.width.length; width++) {
for (let height = 0; height < properties.size.height.length; height++) {
for (let color = 0; color < properties.colors.length; color++) {
console.log(properties.size.width[width]);
}
}
}
JS代码
function generate(rectWidth, rectHeight, amount) {
// holds size
let size = {
width: [],
height: []
};
// holds colors
let colors = [];
for (var i = 0; i < amount; i++) {
// generate size
var width = Math.floor((Math.random() * rectWidth) + 1);
var height = Math.floor((Math.random() * rectHeight) + 1);
var r = Math.floor((Math.random() * 255) + 1);
var g = Math.floor((Math.random() * 255) + 1);
var b = Math.floor((Math.random() * 255) + 1);
// add size to object
size.width.push(width);
size.height.push(height);
colors.push(`rgb(${r}, ${g}, ${b})`);
}
return {
size: size,
colors: colors
};
};
let properties = generate(50, 50, 10);
for (let width = 0; width < properties.size.width.length; width++) {
for (let height = 0; height < properties.size.height; height++) {
for (let color = 0; color < properties.colors.length; color++) {
console.log(properties.size.width[width]);
}
}
}
问题
所以我的问题是,在我的代码中,我试图 console.log(properties.size.width[width]);
,但它没有记录,而且我没有收到错误消息。
我想要发生的事情
我希望我的代码 console.log()
properties.size.width
我试过的
- 我试过在循环中使用
var
而不是let
。
for (let height = 0; height < properties.size.height; height++)
应该是
for (let height = 0; height < properties.size.height.length; height++)
function generate(rectWidth, rectHeight, amount) {
// holds size
let size = {
width: [],
height: []
};
// holds colors
let colors = [];
for (var i = 0; i < amount; i++) {
// generate size
var width = Math.floor((Math.random() * rectWidth) + 1);
var height = Math.floor((Math.random() * rectHeight) + 1);
var r = Math.floor((Math.random() * 255) + 1);
var g = Math.floor((Math.random() * 255) + 1);
var b = Math.floor((Math.random() * 255) + 1);
// add size to object
size.width.push(width);
size.height.push(height);
colors.push(`rgb(${r}, ${g}, ${b})`);
}
return {
size: size,
colors: colors
};
};
let properties = generate(50, 50, 10);
for (let width = 0; width < properties.size.width.length; width++) {
for (let height = 0; height < properties.size.height.length; height++) {
for (let color = 0; color < properties.colors.length; color++) {
console.log(properties.size.width[width]);
}
}
}