What is wrong with this challenge? Part 1

Tell us what’s happening:
Describe your issue in detail here.

No clue what the issue is. The tests should be passed in my opinion.

   **Your code so far**

let strAns = "";

function romanConv (num, place, placeCheck = 0) {

let initStr = "";
let additionalStr = "";
let i = 0;


if (place == 'thousands'){
 placeCheck =  num.toString().length - 4;
}
else if (place == 'hundreds'){
 placeCheck = num.toString().length - 3;
}
else if (place == 'tens'){
 placeCheck = num.toString().length - 2;
}
else if (place == 'ones'){
 placeCheck = num.toString().length - 1;
}

if (place == 'thousands' && num.toString()[placeCheck] >= 5){
 initStr = "MXbar|Vbar";
 additionalStr = "M";
}
else if (place == 'thousands' && num.toString()[placeCheck] < 5){
 initStr = "MVbar|M";
 additionalStr = "M";
}

if (place == 'hundreds' && num.toString()[placeCheck] >= 5){
 initStr = "CM|D";
 additionalStr = "C";
}
else if (place == 'hundreds' && num.toString()[placeCheck] < 5){
 initStr = "CD|C";
 additionalStr = "C";
}

if (place == 'tens' && num.toString()[placeCheck] >= 5){
 initStr = "XC|L";
 additionalStr = "X";
}
else if (place == 'tens' && num.toString()[placeCheck] < 5){
 initStr = "XL|X";
 additionalStr = "X";
}

if (place == 'ones' && num.toString()[placeCheck] >= 5){
 initStr = "IX|V";
 additionalStr = "I";
}
else if (place == 'ones' && num.toString()[placeCheck] < 5){
 initStr = "IV|I";
 additionalStr = "I";
}


     if(num.toString()[placeCheck] == 4 | num.toString()[placeCheck] == 9){
       strAns += initStr.split('|')[0]
   }
   else if (num.toString()[placeCheck] == 5){
       strAns += initStr.split('|')[1]
   }
   else if (num.toString()[placeCheck] > 5){

     for (i = 0; i < num.toString()[placeCheck]  - 5 + 1; i++){

       if(i == 0){
         strAns += initStr.split('|')[1]
       }
       else{
         strAns += additionalStr
       }

     }

   }
   else if (num.toString()[placeCheck] >= 1 && num.toString()[placeCheck] <= 3){
  
     for (i = 0; i < num.toString()[placeCheck] ; i++){

         strAns += additionalStr

     }

   }

}


function convertToRoman(num) {

 let one = "I";
 let five = "V";
 let ten = "X";
 let fifty = "L";
 let hundred = "C";
 let fivehundred = "D";
 let thousand = "M"
 let i = 0;

 let place = "";
 let placeCheck = 0;

 //let strAns = "";

 //console.log(num)

 
   for (i = num.toString().length; i > 0; i--){

     if(i == 4){
       place = 'thousands'
     }
     else if (i == 3){
       place = 'hundreds'
     }
     else if (i == 2){
       place = 'tens'
     }
     else if (i == 1){
       place = 'ones'
     }

     romanConv(num, place, placeCheck)
     
     //finAns += strAns;
     
   }
 

//console.log(strAns)
let finAns = "";
finAns = strAns.toUpperCase().trim();
num === finAns;

return num;

}

convertToRoman(36);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Roman Numeral Converter

Link to the challenge:

Well, the first test that fails is this one:

convertToRoman(2) should return the string II .

My first instinct is to see what it is returning:

console.log(convertToRoman(2));
// 2

So, I agree with the test result - it expected a string “II” and you returned the number 2.

Part of that is that you made a little mistake on what you return from convertToRoman - or perhaps a few lines before you are confusing the strict equality operator with the assignment operator. Fixing that makes things better, at least real answers.

console.log(convertToRoman(2));
// II

But you have another problem - you are using global variables. Global variables are always “iffy”. There may be some isolated cases where they work, but in this case your use of those global variables in your functions is having unintended side effects.

Consider what happens if you run it a couple times in a row:

console.log(convertToRoman(2));
// II
console.log(convertToRoman(2));
// IIII
console.log(convertToRoman(2));
// IIIIII

You are accumulating cruft that is hurting your answers. Remember that several tests are run in a row.


Some of these are hard. I remember struggling with this one, and the cash register one (for which you also opened a thread). Just keep at it. I might also suggest to get in the habit of cleaning up your code as you go (indenting, formatting, dead code, etc.) - it makes it easier to see things. I know it’s frustrating in the “heat of battle” but if you develop that habit, it really pays off in the long run.

1 Like

Ok. Why 58.3 percent for the joke? Are you just mocking published statistics?

It’s just a joke. I don’t think that it is meant to be taken seriously.


Do you have a question about the advice given for editing your code?

Understandable…just inquiring about the numerical choice

It just sounded like a funny number.

1 Like

I liked the idea of the number - if some is passing a Turing Test slightly more than half of the time, the implication is that they are failing a Turing Test almost half of the time. (It seemed like a funny brag, like a pilot bragging that he successfully lands the plane 79% of the time, the implication is…) A friend of mine once joked that I was the only human he knew that had failed a Turing Test.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.