DEV Community

Discussion on: Understanding js reduce with Roman Numerals

Collapse
 
insign profile image
Hélio oliveira

I give a overall look, and by the title, the content made confuse. Then I started reading and probably found something long and confuse to me. But as I said, I really appreciate your effort and hope this article help others.

Thread Thread
 
oinak profile image
Oinak

Yep, probably the title is misleading, because the content covers more concepts than just reduce and roman numerals, but I am guilty of trying to have a catchy headline, and also afraid that a thoroughly descriptive title could sound boring.

I am sorry it did not serve you well and grateful for your kind comments despite your disappointment.

Thread Thread
 
dannyengelman profile image
Danny Engelman

It can be made easier to read:

const romanToArabic = (input) => [...input].reduceRight((
  acc,
  letter,
  idx,
  arr,
  value = {m:1000, d:500, c:100, l:50, x:10, v:5, i:1}[letter.toLowerCase()],
  doubleSubtraction = letter == arr[idx + 1] // ignore IIX notation
 ) => {
  if (value < acc.high && !doubleSubtraction) { 
    acc.Arabic -= value;
  } else {
    acc.high = value;
    acc.Arabic += value;
  }
  console.log(idx, letter, acc, 'value:', value, acc.high, arr[idx + 1]);
  return acc;
}, { high: 0, Arabic: 0 }).Arabic; // return Arabic value
Enter fullscreen mode Exit fullscreen mode


`