我有一个包含两个值的数组,我的目标是获取罗马数字,将其转换为整数,然后将每个数字一一输出。我的问题是在罗马数字函数转换器中,出现错误Uncaught TypeError: str1.charAt is not a function on line 16,它是代码var num = char_to_int(str1.charAt(0));var dynastyReign = [ {"One Dynasty": "MXLI"}, {"Two Dynasty": "MCCCIIII"}, {"Three Dynasty": "MCCCXCVIII"}, {"Four Dynasty": "MCDXLV"}, {"Five Dynasty": "MDCLXIV"}, {"Six Dynasty": "MCMXLIX"}, {"Seven Dynasty": "MMMXICX"}];function roman_to_Int(str1) {if(str1 == null) return -1;var num = char_to_int(str1.charAt(0));var pre, curr;for(var i = 1; i < str1.length; i++){curr = char_to_int(str1.charAt(i));pre = char_to_int(str1.charAt(i-1));if(curr <= pre){num += curr;} else {num = num - pre*2 + curr;}}return num;}function char_to_int(c){switch (c){case 'I': return 1;case 'V': return 5;case 'X': return 10;case 'L': return 50;case 'C': return 100;case 'D': return 500;case 'M': return 1000;default: return -1;}}var year = dynastyReign.map(d => Object.values(d)[0]);dynastyReign.forEach(function(year) {document.write(roman_to_Int(year));});我的代码制作方法是首先创建从罗马数字到int的转换器函数,声明一个变量以仅获取数组的罗马数字部分(var year),然后使用每个变量将整数与转换器一起输出。我不确定应该修复哪一部分,任何帮助将不胜感激。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您可以用var year = dynastyReign.map(d => Object.values(d)[0]);但是随后您继续遍历dynastyReign数组,它是一个对象数组,因此var romanYears = dynastyReign.map(d => Object.values(d)[0]);romanYears.forEach(function(year) { document.write(roman_to_Int(year));});因为roman_to_Int接受字符串而不是对象而引发。而是在罗马年份进行迭代(并在输出之间添加换行符以使内容可读):romanYears.forEach(function(year) { document.write(roman_to_Int(year) + '<br>');});var dynastyReign = [{ "One Dynasty": "MXLI" }, { "Two Dynasty": "MCCCIIII" }, { "Three Dynasty": "MCCCXCVIII" }, { "Four Dynasty": "MCDXLV" }, { "Five Dynasty": "MDCLXIV" }, { "Six Dynasty": "MCMXLIX" }, { "Seven Dynasty": "MMMXICX" }];function roman_to_Int(str1) { if (str1 == null) return -1; var num = char_to_int(str1.charAt(0)); var pre, curr; for (var i = 1; i < str1.length; i++) { curr = char_to_int(str1.charAt(i)); pre = char_to_int(str1.charAt(i - 1)); if (curr <= pre) { num += curr; } else { num = num - pre * 2 + curr; } } return num;}function char_to_int(c) { switch (c) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: return -1; }}var romanYears = dynastyReign.map(d => Object.values(d)[0]);romanYears.forEach(function(year) { document.write(roman_to_Int(year) + '<br>');});还可以清理很多代码,包括避免使用document.write(如今实际上没有任何充分的理由使用)和使用对象代替switch:var dynastyReign=[{"One Dynasty":"MXLI"},{"Two Dynasty":"MCCCIIII"},{"Three Dynasty":"MCCCXCVIII"},{"Four Dynasty":"MCDXLV"},{"Five Dynasty":"MDCLXIV"},{"Six Dynasty":"MCMXLIX"},{"Seven Dynasty":"MMMXICX"}];const romanCharacterMap = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000};function romanToInt(str1) { let num = char_to_int(str1[0]); for (var i = 1; i < str1.length; i++) { const curr = char_to_int(str1[i]); const pre = char_to_int(str1[i - 1]); if (curr <= pre) { num += curr; } else { num = num - pre * 2 + curr; } } return num;}const char_to_int = c => romanCharacterMap[c];var romanYears = dynastyReign.map(d => Object.values(d)[0]);romanYears.forEach(function(year) { document.body.appendChild(document.createElement('div')).textContent = romanToInt(year);});关于javascript - 每个数组的Javascript回显,同时也从字符串转换为int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60585216/ (adsbygoogle = window.adsbygoogle || []).push({});
10-08 21:49