How can I make it so that my function goes through all my if statements not just one
if(myNum.length ===2){
myNum[0] = numToRoman[10]
if(myNum.length ===3){
myNum[0] = numToRoman[100]
if(myNum.length ===4){
myNum[0] = numToRoman[1000]
so that it goes through that until it finds the correct if statement. As of right now I am just getting undefined in the console.
**Your code so far**
function convertToRoman(num) {
let myNum = num.toString().split("").map(Number)
let numToRoman ={1:"I", 2:"II", 3:"III", 4:"IV",5:"V",6:"VI",7:"VII",8:"VIII",9:"IX", 10:"X",40:"XL",50:"L",90:"XC",100:"C",400:"CD",500:"D",900:"CM", 1000:"M"}
if(myNum.length ===2){
myNum[0] = numToRoman[10]
if(myNum.length ===3){
myNum[0] = numToRoman[100]
if(myNum.length ===4){
myNum[0] = numToRoman[1000]
if(myNum[myNum.length-1] >5 && myNum[myNum.length-1] <9){
myNum[myNum.length-1] = numToRoman[[myNum[myNum.length-1]]]
}
if(myNum[myNum.length-1] === 9){
myNum[myNum.length-1] = numToRoman[[myNum[myNum.length-1]]]
}
}
}
return myNum.join("")
}
}
console.log(convertToRoman(1900));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Challenge: Roman Numeral Converter
Link to the challenge:
If I format your code, it looks suspicious:
function convertToRoman(num) {
let myNum = num.toString().split("").map(Number)
let numToRoman = { 1: "I", 2: "II", 3: "III", 4: "IV", 5: "V", 6: "VI", 7: "VII", 8: "VIII", 9: "IX", 10: "X", 40: "XL", 50: "L", 90: "XC", 100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M" }
if (myNum.length === 2) {
myNum[0] = numToRoman[10]
if (myNum.length === 3) {
myNum[0] = numToRoman[100]
if (myNum.length === 4) {
myNum[0] = numToRoman[1000]
if (myNum[myNum.length - 1] > 5 && myNum[myNum.length - 1] < 9) {
myNum[myNum.length - 1] = numToRoman[[myNum[myNum.length - 1]]]
}
if (myNum[myNum.length - 1] === 9) {
myNum[myNum.length - 1] = numToRoman[[myNum[myNum.length - 1]]]
}
}
}
return myNum.join("")
}
}
console.log(convertToRoman(1900));
In what way is it suspicious?
This condition is inside of this condition:
Will those ever both be true at the same time?
No they will not. That’s what I was thinking.
looks like it works with this format
if(myNum.length ===2){
myNum[0] = numToRoman[10]
}
if(myNum.length ===3){
myNum[0] = numToRoman[100]
}
if(myNum.length ===4){
myNum[0] = numToRoman[1000]
}
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 (').
Formatted normally:
if (myNum.length === 2) {
myNum[0] = numToRoman[10]
}
if (myNum.length === 3) {
myNum[0] = numToRoman[100]
}
if (myNum.length === 4) {
myNum[0] = numToRoman[1000]
}
system
Closed
December 29, 2022, 10:41am
9
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.