[英]Can't figure out why I'm Getting “Cannot read property '0' of undefined” Error
I'm trying to do the Roman Numeral challenge from freeCodeCamp. 我正在尝试从freeCodeCamp进行罗马数字挑战。 I have no idea why I'm getting the following error:
我不知道为什么会出现以下错误:
if(numKey[i][0] <= remaining) { TypeError: Cannot read property '0' of undefined`
if(numKey [i] [0] <=剩余){TypeError:无法读取未定义的属性“ 0”
Here's my code: 这是我的代码:
function convertToRoman(num) {
const numKey = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50,'L'], [40, 'XL'], [10, 'X'], [9, 'IX'] [5, 'V'], [4, 'IV'] [1, 'I']];
let remaining = num;
let romanStr = '';
while(remaining > 0) {
for(let i = 0; i < numKey.length; i++) {
if(numKey[i][0] <= remaining) {
let j = 0;
while(j < Math.floor(remaining/numKey[i][0])) {
romanStr += numKey[i][1]
remaining -= numKey[i][0] * Math.floor(remaining/numKey[i][0])
j++
}
}
}
break;
}
return romanStr;
}
I have no idea why numKey[i][0]
would be undefined. 我不知道为什么
numKey[i][0]
是不确定的。 As far as I know it is never out of range, and when I do console.log(numKey[i][0])
in the line above, it always prints the correct value. 据我所知,它永远不会超出范围,当我在
console.log(numKey[i][0])
执行console.log(numKey[i][0])
时,它始终会打印正确的值。 Is there something I am missing? 我有什么想念的吗?
Thanks! 谢谢!
You loop is correct .but array is wrong missing some ,
in last two arrays 您环路是正确的。但数组是错误丢失了一些
,
在过去的两个数组
[9, 'IX'] [5, 'V'], [4, 'IV'] [1, 'I']
is are invalid one [9, 'IX'] [5, 'V'], [4, 'IV'] [1, 'I']
是无效的
Check this below snippet. 在下面的代码段中检查。 See its undefined one
查看其未定义的
const numKey = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50,'L'], [40, 'XL'], [10, 'X'], [9, 'IX'] [5, 'V'], [4, 'IV'] [1, 'I']]; console.log(numKey)
function convertToRoman(num) { const numKey = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'],[5, 'V'], [4, 'IV'],[1, 'I'] ]; let remaining = num; let romanStr = ''; while (remaining > 0) { for (let i = 0; i < numKey.length; i++) { if (numKey[i][0] <= remaining) { let j = 0; while (j < Math.floor(remaining / numKey[i][0])) { romanStr += numKey[i][1] remaining -= numKey[i][0] * Math.floor(remaining / numKey[i][0]) j++ } } } break; } return romanStr; } console.log(convertToRoman(5))
I think you just have a typo in your code, the numKey array was just not correctly formatted: 我认为您的代码中只有拼写错误,numKey数组的格式不正确:
Try: 尝试:
function convertToRoman(num) { const numKey = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50,'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']]; let remaining = num; let romanStr = ''; while(remaining > 0) { for(let i = 0; i < numKey.length; i++) { if(numKey[i][0] <= remaining) { let j = 0; while(j < Math.floor(remaining/numKey[i][0])) { romanStr += numKey[i][1] remaining -= numKey[i][0] * Math.floor(remaining/numKey[i][0]) j++ } } } break; } return romanStr; } console.log(convertToRoman(133)); console.log(convertToRoman(42));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.