JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

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

I have the same expected answers. but the compiler doesn’t want my code. can someone help/explain why this is happening. thanks in advance.

Your code so far

let array = "";
let value = 0;
function convertToRoman(num) {
if(num >= 1000){
  value = num -= 1000;
  array += 'M';
  convertToRoman(value);
}else if (num >= 900){
  value = num -= 900;
  array += 'CM';
  convertToRoman(value);
}else if (num >= 500){
  value = num -= 500;
  array += 'D';
  convertToRoman(value);
}else if (num >= 400){
  value = num -= 400;
  array += 'CD';
  convertToRoman(value);
}else if (num >= 100){
  value = num -= 100;
  array += 'C';
  convertToRoman(value);
}else if (num >= 90){
  value = num -= 90;
  array += 'XC';
  convertToRoman(value);
}else if (num >= 50){
  value = num -= 50;
  array +='L';
  convertToRoman(value);
}else if (num >= 40){
  value = num -= 40;
  array += 'XL';
  convertToRoman(value);
}else if (num >= 10){
  value = num -= 10;
  array += 'X';
  convertToRoman(value);
}else if (num >= 9){
  value = num -= 9;
  array += 'IX';
  convertToRoman(value);
}else if (num >= 5){
  value = num -= 5;
  array += 'V';
  convertToRoman(value);
}else if (num >= 4){
  value = num -= 4;
  array += 'IV';
  convertToRoman(value);
}else if (num >= 1){
  value = num -= 1;
  array += 'I';
  convertToRoman(value);
}

console.log(array)
 return array;
}

//convertToRoman(36);
//convertToRoman(2014);
convertToRoman(501);

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

The array and value are global variables, outside of the convertToRoman function scope. This means that if both of these will not be emptied after the function is used, then the next time result will be composed from two answers.

console.log(convertToRoman(5)); // V
console.log(convertToRoman(3)); // VIII

Generally using global variables, or variables from outside of the function scope should be done with care, as it’s easy to end up with unexpected side-effects.

2 Likes

Oh! thank you so much for this explanation the code runs now.
So I made another function which I transferred all those lengthy codes I made and had it called for recursion. And the global variable back then were now inside the function convertToRoman which will be emptied every time the value is equal to zero.

function phoneticLookup(val) {
let result = “”;

// Only change code below this line

  var lookup = {
  "alpha": "Adams",
  "bravo": "Boston",
  "charlie": "Chicago",
  "delta": "Denver",
  "echo": "Easy",
  "foxtrot": "Frank"
  };

result = lookup [val];
result = phoneticLookup[val];

// Only change code above this line
}

I have tried to get the result but it always come up with " phoneticLookup(“alpha”)
should equal the string Adams and so on. Can you please show me the correct program code

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

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