Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jeremiah and Tianlin's attempt roman numerals #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 34 additions & 2 deletions js/romanNumerals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
exports.toRoman = function(num) {
// exports.toRoman = function(num) {

};
// };

const toRoman = (num) => {
let answer = "";

const map = {
M:1000,CM:900, D:500,CD:400, C:100, XC:90,L:50, XV:40, X:10, IX:9, V:5, IV:4, I:1
}
let a;
// Checking for invalid number
if (num < 1 || num > 3000) {
return "Invalid";
//
} else {
for (let key in map) {
// Getting index of num and assigning to a variable
a = Math.floor(num / map[key]);
// Checking our new variable location in map
if(a >= 0) {
// Filters the zeros out
for(let i = 0; i < a; i++){
// Updates the answer with the correct index location
answer += key;
}
}
// Stops infinite
num = num % map[key];
}
}
return answer;
}

console.log(toRoman(100));