diff --git a/src/roman-numerals/index.js b/src/roman-numerals/index.js index 38afb19..67a3c34 100644 --- a/src/roman-numerals/index.js +++ b/src/roman-numerals/index.js @@ -4,6 +4,30 @@ * @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive). * @returns {number} The decimal equivalent. */ -function romanToDecimal(roman) {} - +function romanToDecimal(roman) { + var romObj = { + I: 1, + V: 5, + X: 10, + L: 50, + C: 100, + D: 500, + M: 1000, + }; + let result = 0; + for (let i = 0; i < roman.length; i++) { + result += romObj[roman[i]]; + // Check the order of the characters in the string + if ((roman[i] == "I" && roman[i + 1] == "V") || (roman[i] == "I" && roman[i + 1] == "X")) { + result -= 2 * romObj["I"]; + } + if ((roman[i] == "X" && roman[i + 1] == "L") || (roman[i] == "X" && roman[i + 1] == "C")) { + result -= 2 * romObj["X"]; + } + if ((roman[i] == "C" && roman[i + 1] == "D") || (roman[i] == "C" && roman[i + 1] == "M")) { + result -= 2 * romObj["C"]; + } + } + return result; +} module.exports = romanToDecimal; diff --git a/src/transpose/index.js b/src/transpose/index.js index adec201..2fa4585 100644 --- a/src/transpose/index.js +++ b/src/transpose/index.js @@ -4,6 +4,17 @@ * @param {number[]} array The array to transpose * @returns {number[]} The transposed array */ -function transpose(array) {} +function transpose(array) { + let result = []; + // The length of the inner array is the same as the length of of the outer array. + for (let i = 0; i < array[0].length; i++) { + let newArray = []; + for (let j = 0; j < array.length; j++) { + newArray.push(array[j][i]); + } + result.push(newArray); + } + return result; +} module.exports = transpose;