One of the last five challenges.
I see all the correct results returned, but it’s not passing?
If I don’t know what’s wrong, then how can I fix it ?
Hope someone can give me some direction.
Thanks
function convertToRoman(num) {
let strnum = num.toString(10);
let length = strnum.length;
let numarr = [{number:8544,value:"1"},{number:8545,value:"2"},{number:8546,value:"3"},{number:8547,value:"4"},{number:8548,value:"5"},{number:8549,value:"6"},{number:8550,value:"7"},{number:8551,value:"8"},{number:8552,value:"9"},{number:8553,value:"10"},{number:8556,value:"50"},{number:8557,value:"100"},{number:8558,value:"500"},{number:8559,value:"1000"}];
let number=""; let str=[];
const zeroNum = 0; const eightNum = 8; const oneStr = "1"; const tenStr = "10"; const threeNum = 3; const fourNum = 4; const fiveNum = 5; const fiveStr= "5"; const oneNum = 1; const fourStr = "4"; const fiftyStr ="50";
const nineStr = "9"; const hunderdStr = "100"; const tenNum = 10; const fivehunderdStr ="500"; const thousandStr ="1000";
let returnThousand = function(num) {
str =[];
for(let i=0;i<num;++i){
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===thousandStr){
number= numarr[i].number;
str.push(number);
}
}
}
return str;
};
let returnHundred = function(num) {
str =[];
switch(num){
case 1:
case 2:
case 3:
for(let i=0;i<num;++i){
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===hunderdStr){
number= numarr[i].number;
str.push(number);
}
}
}
return str;
break;
case 4:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===hunderdStr){
number= numarr[i].number;
}
}
str.push(number);
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fivehunderdStr){
number= numarr[i].number;
}
}
str.push(number);
return str;
break;
case 5:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fivehunderdStr){
number= numarr[i].number;
str.push(number);
}
}
return str;
break;
case 6:
case 7:
case 8:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fivehunderdStr){
number= numarr[i].number;
str.push(number);
}
}
num = num - 5;
for(let i=0;i<num;++i){
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===hunderdStr){
number= numarr[i].number;
str.push(number);
}
}
}
return str;
break;
case 9:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===hunderdStr){
number= numarr[i].number;
str.push(number);
}
}
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===thousandStr){
number= numarr[i].number;
str.push(number);
}
}
return str;
break;
}
};
let returnTen = function(num) {
str =[];
switch(num){
case 1:
case 2:
case 3:
for(let i=0;i<num;++i){
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===tenStr){
number= numarr[i].number;
str.push(number);
}
}
}
return str;
break;
case 4:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===tenStr){
number= numarr[i].number;
str.push(number);
}
}
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fiftyStr){
number= numarr[i].number;
str.push(number);
}
}
return str;
break;
case 5:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fiftyStr){
number= numarr[i].number;
str.push(number);
}
}
return str;
break;
case 6:
case 7:
case 8:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===fiftyStr){
number= numarr[i].number;
str.push(number);
}
}
num = num - 5;
for(let i=0;i<num;++i){
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===tenStr){
number= numarr[i].number;
str.push(number);
}
}
}
return str;
break;
case 9:
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===tenStr){
number= numarr[i].number;
str.push(number);
}
}
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===hunderdStr){
number= numarr[i].number;
str.push(number);
}
}
return str;
break;
default:
break;
}
};
let returnSingleNum = function(counter) {
str =[];
for(let i=0;i<numarr.length;++i){
if(numarr[i].value ===strnum[counter]){
number= numarr[i].number;
str.push(number);
}
}
return str;
};
let counter=0;
let arr=[]; let result =""; let array =[];
while(counter != (length)){
let numAtPostion = parseInt(strnum[counter],10);
//if numAtPostion is zero , continue to next number position
if(numAtPostion != zeroNum){
if((counter === 0) && (length ===4)) {
//call 1000s
result = returnThousand(numAtPostion);
}
if(((counter === 1) && (length ===4)) || ((counter===0)&&(length===3)) ){
//call 100s
result = returnHundred(numAtPostion);
}
if(((counter === 2) && (length ===4)) || ((counter===1)&&(length===3)) || ((counter===0)&&(length===2)) ){
//call 10s
result = returnTen(numAtPostion);
}
if(((counter === 3) && (length ===4)) || ((counter===2)&&(length===3)) || ((counter===1)&&(length===2)) || ((counter===0)&&(length===1))){
//call 1s
result =returnSingleNum(counter);
}
}
//not in cases of 100,200,30 etc.
if(numAtPostion!=zeroNum){
arr.push(result);
}
++counter;
}
arr = arr.flat();
for(let i=0;i<arr.length;++i){
let temp = String.fromCharCode(arr[i]);
array.push(temp);
}
num = array.join("");
return num;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
.
Challenge: Roman Numeral Converter
Link to the challenge: