简体   繁体   English

无法弄清楚为什么我得到“无法读取未定义的属性'0'”错误

[英]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.

相关问题 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 我不断收到错误:TypeError Cannot read property 'getContext' of null 并且不知道为什么 - I keep getting the error: TypeError Cannot read property 'getContext' of null and can't figure out why 我不断收到“未捕获的TypeError:无法读取null的属性&#39;value&#39;”响应,并且无法弄清原因 - I am continually getting a 'Uncaught TypeError: Cannot read property 'value' of null' response and can't figure out why 无法弄清楚为什么我一直不确定 - Can't figure out why I keep getting undefined 我在JavaScript中变得未定义,无法弄清为什么 - I am getting undefined in JavaScript and can't figure it out why ReactJS:在数组中出现错误。 俗话说“ TypeError:无法读取未定义的属性&#39;handleDismiss&#39;”。 不能弄清楚为什么 - ReactJS: Getting an error in array. Its saying “TypeError: Cannot read property 'handleDismiss' of undefined”. Cant figure out why 变得“未定义”,无法弄清原因 - Getting 'undefined', can't figure out why 为什么我在HTMLUListElement收到错误“无法读取未定义的属性&#39;classList&#39;”,这是为什么。 <anonymous> ” - Why is it that I'm getting the error “Cannot read property 'classList' of undefined at HTMLUListElement.<anonymous>” 我收到未处理的承诺拒绝错误,但不知道为什么 - I'm getting an Unhandled Promise Rejection error but can't figure out why 我收到错误Uncaught TypeError:无法读取未定义的属性&#39;drawBox&#39;,有人可以发现问题吗? - I'm getting the error Uncaught TypeError: Cannot read property 'drawBox' of undefined, Can anyone spot the issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM