Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/roman-numerals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 12 additions & 1 deletion src/transpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;