DEV Community

Discussion on: Understanding js reduce with Roman Numerals

Collapse
 
insign profile image
Hélio oliveira

This supposed to be easy? Anyway thanks for your time and effort.

Collapse
 
oinak profile image
Oinak

Hi Hélio, thanks for the feedback

I had not yet qualified its difficulty thing, if it appears as 'easy' it is probably a default value when you don't say otherwise (what number would you suggest on the dev.to scale?).

In this case I would love to know what parts you find less accessible and try to go over them and improve/clarify on this post or on another.

This is a format experiment that benefits/assumes the reading of other materials. It is sure more daunting, but I hoped that it allows to combine several concepts together and see the interactions.

Did you read the linked materials?

  • If yes: were they unhelpful?
  • If not: what deterred you from it? Is it discouraging to refer to other sources?

Cheers!

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


`