罗马数字翻译使用JavaScript [英] Roman Numeral Translator Using JavaScript

查看:90
本文介绍了罗马数字翻译使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不使用下划线的情况下工作,但作为额外的挑战,我正在尝试使用下划线将罗马数字转换为阿拉伯数字。以下是我的尝试。它起作用,除了下一个数字大于当前数字的情况。我真的不确定为什么,但是当我检查if(next> = num)时,if if是否甚至没有被执行?

I got this to work without using underscore, but as an extra challenge I'm trying to convert Roman Numerals to Arabic numbers using underscore. Below is my attempt. It works, except for in the case of the "next" number being greater than the "current" one. I'm really not sure why, but when I check for if(next>= num), that if block is not even being executed?

var DIGIT_VALUES = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000
};

var translateRomanNumeral = function(roman) {
  // if it's not in the digit values object, return null
  for (var i = 0; i < roman.length; i++) {

    if (!(roman[i] in DIGIT_VALUES)) {
      return null;
    }

  }
  //with underscore:
 var romanTranslated =  reduce(roman, function(memo, letter, i) {
    var prev = DIGIT_VALUES[roman[i - 1]];
    var num = DIGIT_VALUES[letter];
    var next = DIGIT_VALUES[roman[i + 1]];
    if (next === undefined || next <= num) {
      return memo + num;
    }



< hr>

由于某种原因,这不会被执行:


This is not being executed for some reason:

    if (next >= num) {
      var diff = num - prev;
      console.log(diff);
      return memo + (diff - prev);
    }
  }, 0);
  // grab the first one

  //1
  // console.log(num);
return romanTranslated;


};

console.log(translateRomanNumeral("LXIV")); //returns 66 ---> should return 64
console.log(translateRomanNumeral("CI")); //working --> returns 101
console.log(translateRomanNumeral("MMMMCCL")); // working ---> returns 4250.
//works!


推荐答案

我错误的主要来源是我的方式定义了reduce,不允许我在迭代时访问索引。我更新了我的reduce函数,然后在稍微调试之后就可以了:

The main source of my error was that the way I had defined reduce did not allow me access to the index while iterating. I updated my reduce function, then it worked after a little debugging:

var DIGIT_VALUES = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000
};
var each = function(collection, iterator) {
  if (Array.isArray(collection)) {
    for (var i = 0; i < collection.length; i++) {
      iterator(collection[i], i, collection);
    }

  } else {
    for (var key in collection) {
      iterator(collection[key], key, collection);
    }
  }

};

var reduce = function(collection, iterator, total) {
  if (total == undefined) {
    total = collection.shift();
  }


  each(collection, function(val, i) {
    total = iterator(total, val, i);
  })
  return total;
};

var translateRomanNumeral = function(roman) {
  if (typeof(roman) !== 'string') {
    return null;
  }
  if (!roman) {
    return 0;
  }
  // if it's not in the digit values object, return null
  for (var i = 0; i < roman.length; i++) {

    if (!(roman[i] in DIGIT_VALUES)) {
      return null;
    }

  }
  //with underscore:

  return reduce(roman, function(memo, letter, i) {
    var num = DIGIT_VALUES[letter];
     //console.log(i);
    //how do you acess the next item in a collection in reduce?
    var next = DIGIT_VALUES[roman[Number(i) + 1]];
    // console.log(Number(i) + 1);
    // console.log(next);

      if ( next === undefined || next <= num) {
        return memo + num;
        //console.log(memo);
      }

      else {
        // var diff = num - prev;
        // console.log(diff);
        return memo - num;
        // memo = memo + (next - num);
      }


    // return memo;
  }, 0);

};

console.log(translateRomanNumeral("LXIV")); //returns 66 ---> should return 64
console.log(translateRomanNumeral("IX")) // should return 9
console.log(translateRomanNumeral("IV")) /// should return 4

console.log(translateRomanNumeral("CI")); 
//working --> returns 101
console.log(translateRomanNumeral("MMMMCCL")); 
// working ---> returns 4250.
//works!

这篇关于罗马数字翻译使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆