function rom(num) {
if (num >= 1000) {
const roman1 = convertToRoman(num - 1000);
roman1.unshift("M");
return roman1;
} else if (num >= 900) {
const roman2 = convertToRoman(num - 900);
roman2.unshift("CM");
return roman2
} else if (num >= 500) {
const roman3 = convertToRoman(num - 500);
roman3.unshift("D")
return roman3
} else if (num >= 400) {
const roman4 = convertToRoman(num - 400);
roman4.unshift("CD")
return roman4
} else if (num >= 100) {
const roman5 = convertToRoman(num - 100);
roman5.unshift("C")
return roman5
} else if (num >= 90) {
const roman6 = convertToRoman(num - 90);
roman6.unshift("XC")
return roman6
} else if (num >= 50) {
const roman7 = convertToRoman(num - 50);
roman7.unshift("L")
return roman7
} else if (num >= 40) {
const roman8 = convertToRoman(num - 40);
roman8.unshift("XL")
return roman8
} else if (num >= 10) {
const roman9 = convertToRoman(num - 10);
roman9.unshift("X")
return roman9
} else if (num >= 9) {
const roman10 = convertToRoman(num - 9);
roman10.unshift("IX")
return roman10
} else if (num >= 5) {
const roman11 = convertToRoman(num - 5);
roman11.unshift("V")
return roman11
} else if (num >= 4) {
const roman12 = convertToRoman(num - 4);
roman12.unshift("IV")
return roman12
} else if (num >= 1) {
const roman13 = convertToRoman(num - 1);
roman13.unshift("I")
return roman13
} else {
return []
};
}
function convertToRoman(num) {
let arr = rom(num);
let atr = arr.join("")
return atr;
}
console.log(convertToRoman(3));
What is this challenge, what error is it showing?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
The error you see is because you cannot Unshift a value which is not an Array,
return [atr] instead of return atr;
and your problem is solved.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.