Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Why is the boolean value being appended to my current value in a reduce array method in JS?

I am surprised why a boolean value is being appended to the current value in my reduce functionality.

var romanToInt = function (s) {
  const DICT = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000,
  }

  let reversedArr = s.split('').reverse()
  reversedArr

  return reversedArr.reduce((sum, cur, i, arr) => {
    let value = DICT[cur]

    // check if preceeding numeral subtracts
    if ((arr[i] += arr[i - 1] === 'VI') && i != 0) {
      sum -= value
    } else {
      // Set sum as the first
      sum += value
    }
    return sum
  }, 0)
}

console.log(romanToInt('III'))

Why is this expression (curr += arr[i - 1] === 'VI') evaluating to false, true, true?

The value of curr after three iterations is Ifalse, Ifalse, Ifalse. How is this happening?

All I want to do is check wheather or not the current value and the preceding value equals the string 'VI'

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

x += y is equivalent to x = x + y, and that whole assignment expression evaluates to the value on the right-hand-side: the x + y. If x + y is truthy, the whole resulting expression will be truthy.

For the same reason, your

if ((arr[i] += arr[i - 1] === 'VI') && i != 0) {

isn't doing what you're thinking it is.

It looks like you don't need to assign to the array index at all here - just compare there, and if the condition is fulfilled, change the sum (without changing anything else inside the loop).

var romanToInt = function (s) {
  const DICT = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000,
  }

  const reversedArr = s.split('').reverse();
  return reversedArr.reduce((sum, cur, i, arr) => {
    let value = DICT[cur]

    // check if preceeding numeral subtracts
    if ((arr[i] + arr[i - 1] === 'VI') && i != 0) {
      sum -= value
    } else {
      // Set sum as the first
      sum += value
    }
    return sum
  }, 0)
}

console.log(romanToInt('III'))

But you still have a ways to go to fix the rest of your algorithm. This is a good approach.

about 4 years ago · Juan Pablo Isaza Report

0

shorter version ;)

const romanToInt = (function()
  {
  const
    DICT = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 } 
  , Narr = [ 'IV','IX', 'XL', 'XC', 'CD', 'CM' ]
    ;
  return (str) =>
    [...str].reverse().reduce((sum, curr, i, {[i-1]:prev}) => 
      sum + (Narr.includes(`${curr}${prev}`) ? -DICT[curr] : DICT[curr]) , 0);
  })()

console.log('III', romanToInt('III'))
console.log('IV',  romanToInt('IV'))
console.log('VII', romanToInt('VII'))
console.log('IX',  romanToInt('IX'))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!