-1

I am currently trying to solve the "Roman to Integer" question on Leetcode.

My code works with roman numerals such as ("III (3)", "V (5)", "X (10)", "C (50)") for some examples. Where my code breaks down is when roman numerals such as ("IV (4)", "IX (9)", "XL (40)").

 let romanNum = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
 let specCases = {"IV":4, "IX":9, "XL":40, "XC":90, "CD":400, "CM":900}
 arr.reduce((acc,cur,i) => {
    //check if it is one of the special cases
    let lastTwo = ""
    if (i !== 0) {
        let lastTwo = arr[i-1].concat(cur)
        console.log(lastTwo)
        console.log(Object.keys(specCases))
        console.log(typeof(lastTwo))
    }
    if (lastTwo in specCases) {
        acc = acc - romanNum[arr[i-1]] + specCases[lastTwo]
        return console.log(acc)
    }
    //for non-sepcial cases run the code below
    acc = acc + romanNum[cur]
    return acc
}, 0)

I've also included the relevant pieces of code above. I checked to make sure the "lastTwo" variable was assigned correctly, that the specCases object keys were being accessed correctly, and the typeof(lastTwo) was a string. All of the things I checked above seemed to be correct.

Below is link to the photo the leetcode output when using "IX":

Evaluates to 11 when it should be 9

When running the code, the second conditional statement is not met, and that portion of the code is not ran.

Why is the second condition not being met?

rankin
  • 1
  • 2
  • `acc = acc - arr[i-1] + specCases[lastTwo]` makes no sense. `arr[i-1]` is not a number, you can't subtract it. I think you mean `romanNum[arr[i-1]]` – Barmar Dec 09 '21 at 02:08
  • `return console.log(acc)` exits the function and doesn't return the accumulator. – Barmar Dec 09 '21 at 02:10
  • Sorry I forgot to add a piece of code. I changed the string input to ```arr```, so that does exist. Regardless, the issue im ruining into is the condition is not being met, so its not even getting to that portion of the code. Ill edit my original post. – rankin Dec 09 '21 at 02:11
  • I know you changed it to an array. But it's still an array of roman numerals, you can't subtract those from the accumulator. – Barmar Dec 09 '21 at 02:12
  • I get that part. The issue im having is with the ```if (lastTwo in specCases)```. For some reason that is not triggering my if statement. – rankin Dec 09 '21 at 02:13
  • Ok, so that sounds like another issue I need to address then. Ill go ahead and change that . But for some reason the if statement isnt triggering either. The reason I know that is because I added a console log right after the if statement, and it wasnt logging. – rankin Dec 09 '21 at 02:14
  • Change `let lastTwo = arr[i-1].concat(cur)` to `lastTwo = arr[i-1].concat(cur)`. You're creating a new variable in the scope of the `if`, not assigning the outer variable. – Barmar Dec 09 '21 at 02:17
  • Thanks so much! Fairly new to all this, really appreciate your time. – rankin Dec 09 '21 at 02:19

1 Answers1

0

Barmar answered this question:

Change let lastTwo = arr[i-1].concat(cur) to lastTwo = arr[i-1].concat(cur). You're creating a new variable in the scope of the if, not assigning the outer variable.

rankin
  • 1
  • 2