0

I have the following function that counts and outputs the order number for every item within a div:

$('.number').each(function(i) {
        $(this).text(++i);
        $(this).text(function (i, n) {
     var result = Number(n) + 0;
     return result;

    });
 });

I'm trying to turn the numbers into Roman letters and I've tried the following, without success:

function convertToRoman(num) {
  var roman = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
  };
  var str = '';

  for (var i of Object.keys(roman)) {
    var q = Math.floor(num / roman[i]);
    num -= q * roman[i];
    str += i.repeat(q);
  }

  return str;
}

Would it be a way to do that?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Anta
  • 141
  • 1
  • 9
  • Does this answer your question? [Convert a number into a Roman Numeral in javaScript](https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript) – freedomn-m Mar 09 '21 at 11:32
  • I've tried to implement that but couldn't manage to make it work – Anta Mar 09 '21 at 11:33
  • [your code](https://jsfiddle.net/yL9kghrm/) seems to work fine. Perhaps you could create a *complete* snippet that shows what you're trying to do - eg that includes where you actually call your method. See [mcve] and [ask]. – freedomn-m Mar 09 '21 at 11:39
  • Did you want `$(this).text(convertToRoman(++i))` – freedomn-m Mar 09 '21 at 11:40
  • Thanks for your answer. I think so! I put a Fiddle together to try to debug it. I'm getting 0 as a value for all the counters. Link: https://jsfiddle.net/gmzbri/c1tpnkLw/ – Anta Mar 09 '21 at 11:58
  • 1
    Updated fiddle: https://jsfiddle.net/vsqfr1ea/ you had removed the `return` from the function and then you were overwriting it. – freedomn-m Mar 09 '21 at 12:37
  • Thanks a lot @freedomn-m! I appreciate it a lot. Works perfect. – Anta Mar 09 '21 at 13:52

0 Answers0