用于二维数组的循环范围
For Loop Scoping with 2D Array
我正在参加 Advent Code Challenge,并且 运行 进入了大厅。我之前解决了这个错误,但在这种情况下,我被卡住了。下面的代码给了我一个 Uncaught TypeError: Cannot read properties of undefined.
相对于 switch (diagArray[i][j])
.
我的想法是 diagArray
超出范围。是真的吗?
拜托,任何帮助都很棒!
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray.length; i++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
// console.log(rates)
}
}
这是因为在第二个 for 循环中它说 diagArray.length
,它应该是 diagArray[i].length
这样你就得到了第二个数组的长度。
您的第二次迭代增加了 i
变量而不是 j
,同时迭代到 diagArray
的长度而不是 diagArray[i]
。
您可以使用控制台 API 调试您的值,无需猜测即可找出问题的根源。
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray[i].length; j++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
console.log(`${i}:${j} = ${diagArray[i][j]}`)
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
// console.log(rates)
}
}
您应该像这样更新第二个 for 循环表达式。
for (let j = 0; j < diagArray[i].length; j++) {
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray.length; i++) {
这里有多个问题。
第一个for循环没问题,但第二个for循环需要受限于内部数组的长度:
diagArray[i].length
而且它目前还在增加第一个循环计数i++
。那需要 j++
工作示例:
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for (let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray[i].length; j++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
console.log(rates)
}
}
另外,通常不建议使用这样的循环来迭代数组,因为正如您所发现的,很容易搞砸索引。
我建议改为对数组的值进行交互:
for (const row of diagArray) {
for (const value of row) {
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for (const row of diagArray) {
for (const value of row) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (value) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
console.log(rates)
}
}
我正在参加 Advent Code Challenge,并且 运行 进入了大厅。我之前解决了这个错误,但在这种情况下,我被卡住了。下面的代码给了我一个 Uncaught TypeError: Cannot read properties of undefined.
相对于 switch (diagArray[i][j])
.
我的想法是 diagArray
超出范围。是真的吗?
拜托,任何帮助都很棒!
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray.length; i++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
// console.log(rates)
}
}
这是因为在第二个 for 循环中它说 diagArray.length
,它应该是 diagArray[i].length
这样你就得到了第二个数组的长度。
您的第二次迭代增加了 i
变量而不是 j
,同时迭代到 diagArray
的长度而不是 diagArray[i]
。
您可以使用控制台 API 调试您的值,无需猜测即可找出问题的根源。
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray[i].length; j++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
console.log(`${i}:${j} = ${diagArray[i][j]}`)
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
// console.log(rates)
}
}
您应该像这样更新第二个 for 循环表达式。
for (let j = 0; j < diagArray[i].length; j++) {
for(let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray.length; i++) {
这里有多个问题。
第一个for循环没问题,但第二个for循环需要受限于内部数组的长度:
diagArray[i].length
而且它目前还在增加第一个循环计数i++
。那需要 j++
工作示例:
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for (let i = 0; i < diagArray.length; i++) {
for (let j = 0; j < diagArray[i].length; j++) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (diagArray[i][j]) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
console.log(rates)
}
}
另外,通常不建议使用这样的循环来迭代数组,因为正如您所发现的,很容易搞砸索引。
我建议改为对数组的值进行交互:
for (const row of diagArray) {
for (const value of row) {
const diagArray = [
[0,1,0,0,0,1,1,1,0,0,0,1],
[1,1,0,1,0,0,0,0,0,0,0,1],
[1,1,1,0,0,1,0,0,1,0,1,1]
]; // example data
for (const row of diagArray) {
for (const value of row) {
let gammaRate = ''
let epsilonRate = ''
let ones = 0
let zeros = 0
let rates = []
switch (value) {
case 1:
ones++
break
case 0:
zeros++
break
default:
break
}
if (ones > zeros) {
gammaRate+=1
epsilonRate+=0
} else {
gammaRate+=0
epsilonRate+=1
}
rates.push([gammaRate, epsilonRate])
console.log(rates)
}
}