Roman Numeral Reduction - Interview assessment Coderbyte - Code challenge - Source Code Answers

preview_player
Показать описание
#coderbyte #codechallenge #solution
Roman Numeral Reduction - Interview assessment Coderbyte - Code challenge - Source Code Answers
Source code with comments - JavaScript:

Thank you for watching!
Рекомендации по теме
Комментарии
Автор

do we need to handle subtractive cases like 'iV'?

maazahmedpoke
Автор

Thanks a lot, bro can i contact you regarding coderbyte sql answers it would mean world to me if u reply!! Please

rajgaurav
Автор

function RomanNumeralReduction(str) {
const romToInt = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};

let sum = 0;
let tempSum = 0;
let result = "";

for (let i = 0; i < str.length; i++) {
sum += romToInt[str[i]];
}

while (sum !== tempSum) {
const romans = Object.keys(romToInt);

for (let i = romans.length - 1; i >= 0; i--) {
const roman = romans[i];
if (romToInt[roman] <= sum - tempSum) {
result += roman;
tempSum += romToInt[roman];
break;
}
}
}
return result;
}

ritikjain
welcome to shbcf.ru