Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中罗马数字Kata的重构_Javascript_Refactoring_Jasmine_Underscore.js_Roman Numerals - Fatal编程技术网

Javascript中罗马数字Kata的重构

Javascript中罗马数字Kata的重构,javascript,refactoring,jasmine,underscore.js,roman-numerals,Javascript,Refactoring,Jasmine,Underscore.js,Roman Numerals,我是Javascript中罗马数字Kata的新手。 所有规格都合格。我已经重构了spec文件,现在正试图重构主文件 过去的样子是这样的: function roman(number) { var conversion = '' if (number == 100){ conversion += 'C' number -= 100 } // omitted code while (number >= 1){ conversion += 'I'

我是Javascript中罗马数字Kata的新手。 所有规格都合格。我已经重构了spec文件,现在正试图重构主文件

过去的样子是这样的:

function roman(number) {
  var conversion = ''

  if (number == 100){
    conversion += 'C'
    number -= 100
  }
// omitted code

  while (number >= 1){
   conversion += 'I'
   number -= 1
 }

return conversion
}
function roman(number) {

  var denominations = {

    100: 'C',
    // omitted code
    5: 'V',
    4: 'IV',
    1: 'I'

  }

  var conversions = ''

  _.each(denominations, function(roman_num, natural_num) {
    while (number >= natural_num){ 
      conversions = conversions + roman_num
      number -= natural_num
    }
  })
  return conversions
}
Expected 'IIII' to equal 'IV'
Expected 'IIIII' to equal 'V'
现在看起来是这样的:

function roman(number) {
  var conversion = ''

  if (number == 100){
    conversion += 'C'
    number -= 100
  }
// omitted code

  while (number >= 1){
   conversion += 'I'
   number -= 1
 }

return conversion
}
function roman(number) {

  var denominations = {

    100: 'C',
    // omitted code
    5: 'V',
    4: 'IV',
    1: 'I'

  }

  var conversions = ''

  _.each(denominations, function(roman_num, natural_num) {
    while (number >= natural_num){ 
      conversions = conversions + roman_num
      number -= natural_num
    }
  })
  return conversions
}
Expected 'IIII' to equal 'IV'
Expected 'IIIII' to equal 'V'
我花了一些时间通过Chrome使用JS控制台进行调试,它看起来没有遍历每个面额,而是停留在1

我还使用了Jasmine,因此我的错误如下所示:

function roman(number) {
  var conversion = ''

  if (number == 100){
    conversion += 'C'
    number -= 100
  }
// omitted code

  while (number >= 1){
   conversion += 'I'
   number -= 1
 }

return conversion
}
function roman(number) {

  var denominations = {

    100: 'C',
    // omitted code
    5: 'V',
    4: 'IV',
    1: 'I'

  }

  var conversions = ''

  _.each(denominations, function(roman_num, natural_num) {
    while (number >= natural_num){ 
      conversions = conversions + roman_num
      number -= natural_num
    }
  })
  return conversions
}
Expected 'IIII' to equal 'IV'
Expected 'IIIII' to equal 'V'
等等

因此,我的问题是: 1.为什么它只能返回1“I”的值 和 2.我怎么修理它


提前谢谢

JavaScript对象是以字符串作为索引的HashMap。因此,不能保证“u.each”中元素的顺序与声明的顺序相同。您应该使用保证有序数组的结构

function roman(number) {
  var denominations_natural = [100, 5, 4, 1];
  var denominations_roman = ["C", "V", "IV", "I"];

  var conversions = '';

  for (var i=0; i<denominations_natural.length; i++) {
    while (number >= denominations_natural[i]) {
      conversions += denominations_roman[i];
      number -= denominations_natural[i];
    }
  }

  return conversions;
}
函数罗马(数字){
var面额_natural=[100,5,4,1];
var面额罗马=[“C”、“V”、“IV”、“I”];
var转换=“”;
对于(var i=0;i=面额_自然[i]){
兑换率+=罗马面额[i];
数字-=面额自然[i];
}
}
返回转换;
}