Build a Roman Numeral Converter Project - Build a Roman Numeral Converter TESTS ARE BEING WEIRD

Tell us what’s happening:

For some reason all my tests work except when 9 is inputted. Although everything seems to work as intended, so I am not sure what is wrong?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset = 'utf-8'>
    <link rel='stylesheet' href="styles.css">
    <link rel="icon" type="image/png" href="https://cdn.freecodecamp.org/universal/favicons/favicon.ico">

    
  </head>

  <body>
    <div id="top-part">
      <img src="https://i.pcmag.com/imagery/reviews/01tPXClg2WjLamQzScplH3y-15.fit_scale.size_1028x578.v1627670281.png"></img>
      
      <h1 id="name">ROMAN NUMERAL CONVERTER<h1>
    </div>

    <form>
      <fieldset>
      <h2>Enter A Number</h2>
      <input type="number" id="number"></input>
      <button id="convert-btn">Convert</button>
      </fieldset>
    </form>
    <div id="output">
    <p id="outputArea"></p>
    </div>
    

  </body>

  <script rel="script" src="script.js">
  </script>
</html>

/* file: script.js */
//creating a table to use for comparison later
const table = [{
  romanNumeral: "M",
  arabicNumeral: 1000
},
{
  romanNumeral: "CM",
  arabicNumeral: 900
},
{
  romanNumeral: "D",
  arabicNumeral: 500
},
{
  romanNumeral: "CD",
  arabicNumeral: 400
},
{
  romanNumeral: "C",
  arabicNumeral: 100
},
{
  romanNumeral: "XC",
  arabicNumeral: 90
},
{
  romanNumeral: "L",
  arabicNumeral: 50
},
{
  romanNumeral: "XL",
  arabicNumeral: 40
},
{
  romanNumeral: "X",
  arabicNumeral: 10
},
{
  romanNumeral: "IX",
  arabicNumeral: 9
},
{
  romanNumeral: "V",
  arabicNumeral: 5
},
{
  romanNumeral: "IV",
  arabicNumeral: 4
},
{
  romanNumeral: "I",
  arabicNumeral: 1
}
]
// getting the element that holds the input value
let number = document.getElementById('number');
//getting the element that will show the answer
let output = document.getElementById('outputArea');
// initializing variables ... we will set this later when convert button is clicked
let desiredNum ="";
let startingNum = 0;
let outputString = "";
const convertBtn = document.getElementById('convert-btn');

convertBtn.addEventListener("click", () => {
  desiredNum = number.value

  if(desiredNum == ""){
    outputString = "Please enter a valid number";
    output.innerHTML = outputString;
  }
  else if(desiredNum < 0){
    outputString = "Please enter a number greater than or equal to 1";
    output.innerHTML = outputString;
  }
  else if(desiredNum > 3999){
    outputString = "Please enter a number less than or equal to 3999";
    output.innerHTML = outputString;
  }else{

  convertNumber(0);
  }
  //for testing to see if correct output works... IT DOES :)
  //console.log(desiredNum);
})

const convertNumber = (currentRow) =>{

  //the while loop compares the desirednum to the startingnum
  while (startingNum != desiredNum){

    //here we check if the current row num + startingNum is greater than our desiredNum. If it is we go to the next row so we increment the index of currentRow
    if((startingNum + table[currentRow].arabicNumeral) > desiredNum){
      currentRow++;
    }
    //here we check if the current row num + startingNum is equal to the desiredNum. If it is we update startingNum and add roman numeral to our outputString
    else if((startingNum + table[currentRow].arabicNumeral) == desiredNum)
    {
      outputString += table[currentRow].romanNumeral;
      startingNum += table[currentRow].arabicNumeral;
      output.innerHTML = outputString;
      break;
    }
    //here we check if the currentrow num + starting num is less than our desiredNum. If it is we update startingNum, and add the roman numeral to our outputString
    else if ((startingNum + table[currentRow].arabicNumeral) < desiredNum){
      outputString += table[currentRow].romanNumeral;
      startingNum += table[currentRow].arabicNumeral;
    }
  }

  //we need to reset the values so next time we use the converter it can reassess from the beginning
startingNum = 0;
outputString = "";
}
 







/* file: styles.css */

*{
  color: white;
}
img {
  width: 17em;
  margin-bottom: 0;
}
@media only screen and (max-width: 400px){
  img{
    width: 15em;
  }
}

body{
  background-color: #0a0a23;
  display: block;
  text-align: center; 
}

#top-part{
  display: block;
  text-align: center;
}
#name{
  margin-top: 0;
  
}
input{
  color: black;
  width: 80%;
  height: 3em;
  text-align: center;
  
}

button{
  color: black;
  background-color: orange;
  width: 40%;
  height: 4em;
  margin-top: 1em;
}

form{
  text-align: center;
  margin: auto;
  background-color: #3b3b4f;
}

@media only screen and (min-width: 800px){
  form{
    margin-left: 20em;
    margin-right: 20em;
  }
  #output{
    margin-left: 20em;
    margin-right: 20em;

  }
}

#output{
  border:  1px solid white;
  margin-top: 1em;
  background-color: #3b3b4f;

}

#outputArea{
  padding: 1em;
}

Your browser information:

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

Challenge Information:

Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

Hi @amrit3 :wave:

I double-checked your work, and even re-wrote the section for 9 as well, just in case you had pasted some hidden characters in there by accident, or some other invisible edge-case. I ran the tests and tested it myself. It works, but the same test fails for me.

Your code looks fine, but if you have deviated from the instructions anywhere, it could have led to an error that falsely points to this step.

You might want to report a bug :bug:

Welcome to the forum @amrit3

Why did you add this code?

Happy coding

Id get rid of these extra global variables (and anything else that is a global variable that isn’t a DOM element)

I made a div to create the box that shows the answer, underneath the convert button, and I had already given it the id of “output”, but later went back to add a p tag to actually display the text. Out of laziness I did not change transfer the id from the div to the p tag.

Thankyou for checking

1 Like

What is wrong with initializing variables that I later use in my code ?

how can I report a bug ?

You should not use global variables like that. It makes a mess of the reusability and is hard to reason about.

1 Like

I’m not sure, sorry.
One of the admins suggested I do this once, for a similar issue.

This project is pretty well tested, so I wouldn’t blame a bug in the tests before a bug in your code.

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

ahh I see, thanks for providing an example