Challenge - 5 Problems
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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'));Attempts:
2 left
💡 Hint
Remember that when a smaller numeral appears before a larger one, it is subtracted.
✗ Incorrect
The Roman numeral 'MCMXCIV' breaks down as M (1000) + CM (900) + XC (90) + IV (4) = 1994.
❓ Predict Output
intermediateOutput 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'));Attempts:
2 left
💡 Hint
Repeated numerals add up their values.
✗ Incorrect
'III' is three times 'I' which is 1, so total is 3.
❓ Predict Output
advancedOutput 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'));Attempts:
2 left
💡 Hint
XL means 40 because X (10) is before L (50), so subtract 10 from 50.
✗ Incorrect
'XLII' = XL (40) + I (1) + I (1) = 42.
🧠 Conceptual
advancedWhy 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?
Attempts:
2 left
💡 Hint
Think about how Roman numerals like IV and IX are formed.
✗ Incorrect
Roman numerals use subtraction when a smaller numeral precedes a larger one to represent numbers like 4 (IV) or 9 (IX).
🔧 Debug
expertIdentify 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'));Attempts:
2 left
💡 Hint
Check the loop boundary and array indexing carefully.
✗ Incorrect
The original loop condition was i <= s.length, causing s[i] to be undefined and map[s[i]] to throw a TypeError. Changing it to i < s.length fixes the error.
