Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Roman to Integer Conversion in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Roman to Integer Conversion Function
What is the output of the following JavaScript function when input is 'MCMXCIV'?
DSA Javascript
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for (let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]];
    if (next && current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
console.log(romanToInt('MCMXCIV'));
A1994
B1904
C2094
DNone of the above
Attempts:
2 left
💡 Hint
Remember that when a smaller numeral appears before a larger one, it is subtracted.
❓ Predict Output
intermediate
2:00remaining
Output of Roman to Integer Conversion with Repeated Numerals
What is the output of the function romanToInt when input is 'III'?
DSA Javascript
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for (let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]];
    if (next && current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
console.log(romanToInt('III'));
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Repeated numerals add up their values.
❓ Predict Output
advanced
2:00remaining
Output of Roman to Integer Conversion with Subtraction Cases
What is the output of the function romanToInt when input is 'XLII'?
DSA Javascript
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for (let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]];
    if (next && current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
console.log(romanToInt('XLII'));
A52
B44
C40
D42
Attempts:
2 left
💡 Hint
XL means 40 because X (10) is before L (50), so subtract 10 from 50.
🧠 Conceptual
advanced
1:30remaining
Why does the Roman to Integer Conversion use subtraction?
Why does the algorithm subtract the value of a Roman numeral when it appears before a larger numeral?
ABecause the algorithm treats all numerals as negative values initially.
BBecause the algorithm always subtracts the first numeral regardless of position.
CBecause in Roman numerals, a smaller numeral before a larger one means subtraction.
DBecause the algorithm adds all numerals and then subtracts the total at the end.
Attempts:
2 left
💡 Hint
Think about how Roman numerals like IV and IX are formed.
🔧 Debug
expert
2:30remaining
Identify the error in this Roman to Integer Conversion code
What error does the following code produce when input is 'IX'?
DSA Javascript
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for (let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]];
    if (next && current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
console.log(romanToInt('IX'));
ASyntaxError due to loop condition
BReturns 9 correctly
CTypeError: Cannot read property of undefined
DReturns 11 incorrectly
Attempts:
2 left
💡 Hint
Check the loop boundary and array indexing carefully.

Practice

(1/5)
1. What is the main rule to convert Roman numerals to integers?
easy
A. Subtract all values from the largest symbol
B. Add values of symbols, subtract if a smaller symbol is before a larger one
C. Add values only if symbols are the same
D. Multiply values of symbols in order

Solution

  1. Step 1: Understand Roman numeral rules

    Roman numerals are added normally, but if a smaller numeral appears before a larger one, it is subtracted.
  2. Step 2: Apply addition and subtraction rules

    For example, IV is 5 - 1 = 4, while VI is 5 + 1 = 6.
  3. Final Answer:

    Add values of symbols, subtract if a smaller symbol is before a larger one -> Option B
  4. Quick Check:

    Roman numeral rules = Add and subtract [OK]
Hint: Subtract when smaller numeral precedes larger one [OK]
Common Mistakes:
  • Always adding without subtraction
  • Multiplying values incorrectly
  • Ignoring order of symbols
2. Which of the following is the correct JavaScript syntax to get the integer value of a Roman numeral character from a map object named romanMap?
easy
A. romanMap['V']
B. romanMap.get('V')
C. romanMap.V
D. romanMap('V')

Solution

  1. Step 1: Identify the data structure type

    If romanMap is a plain JavaScript object, access values using square brackets with the key as a string.
  2. Step 2: Choose correct syntax for object property access

    Using romanMap['V'] correctly accesses the value for key 'V'. romanMap.get('V') is for Map objects, not plain objects.
  3. Final Answer:

    romanMap['V'] -> Option A
  4. Quick Check:

    Object property access = romanMap['V'] [OK]
Hint: Use square brackets for object keys as strings [OK]
Common Mistakes:
  • Using .get() on plain objects
  • Using dot notation with string keys
  • Calling object like a function
3. What will be the output of the following JavaScript code?
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for(let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]] || 0;
    if(current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
console.log(romanToInt('IX'));
medium
A. 9
B. 11
C. 1
D. Error

Solution

  1. Step 1: Trace the loop for input 'IX'

    For 'I' (1), next is 'X' (10), since 1 < 10, subtract 1 (total = -1). For 'X' (10), next is undefined (0), so add 10 (total = 9).
  2. Step 2: Calculate final total

    Total after loop is 9, which is the integer value of 'IX'.
  3. Final Answer:

    9 -> Option A
  4. Quick Check:

    'IX' = 9 [OK]
Hint: Subtract if current < next, else add [OK]
Common Mistakes:
  • Adding all values without subtraction
  • Ignoring next character check
  • Off-by-one errors in loop
4. Find the error in this JavaScript function for Roman to Integer conversion:
function romanToInt(s) {
  const map = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
  let total = 0;
  for(let i = 0; i <= s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i+1]] || 0;
    if(current < next) {
      total -= current;
    } else {
      total += current;
    }
  }
  return total;
}
medium
A. Use '==' instead of '<' in if condition
B. map object keys should be lowercase
C. total should be initialized to 1
D. Loop condition should be i < s.length, not i <= s.length

Solution

  1. Step 1: Analyze loop boundary

    The loop runs from i = 0 to i <= s.length, which means it accesses s[s.length], an undefined index causing current to be undefined.
  2. Step 2: Correct loop condition

    Changing loop condition to i < s.length prevents accessing out-of-bound index and avoids errors.
  3. Final Answer:

    Loop condition should be i < s.length, not i <= s.length -> Option D
  4. Quick Check:

    Loop boundary off-by-one error fixed [OK]
Hint: Use i < length to avoid out-of-bound errors [OK]
Common Mistakes:
  • Using <= instead of < in loops
  • Wrong case for map keys
  • Incorrect comparison operators
5. Given the Roman numeral string 'MCMXCIV', what integer should the conversion function return?
hard
A. 2004
B. 1904
C. 1994
D. 1094

Solution

  1. Step 1: Break down the Roman numeral 'MCMXCIV'

    M = 1000, CM = 900 (1000 - 100), XC = 90 (100 - 10), IV = 4 (5 - 1).
  2. Step 2: Add all parts

    1000 + 900 + 90 + 4 = 1994 total.
  3. Final Answer:

    1994 -> Option C
  4. Quick Check:

    'MCMXCIV' = 1994 [OK]
Hint: Identify subtractive pairs and sum all values [OK]
Common Mistakes:
  • Adding all numerals without subtraction
  • Misreading subtractive pairs
  • Confusing similar numerals
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard