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'