Can YOU catch the bug?

Howdy,

I’ve spent my fair share of time head bashing to figure out this odd behavior.

Input 33, get the right answer
Input 32, previous answer clears and get the right answer
input 2024, previous answer clears and get the right answer
input 2022, previous answer DOES NOT clear and desired output not achieved.

I want to learn. Can someone see the bug?

SPOILER ALERT–> Code below has a workable solution for a roman numeral converter.

Thanks!


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input id='number' type='text' />
    <button id='convert-btn'>Convert</button>
    <div id='output'></div>
    <script src='script.js'></script>
</body>
</html>
const output = document.getElementById('output');
const convertBtn = document.getElementById('convert-btn');
const inputNum = document.getElementById('number');

convertBtn.addEventListener('click', () => {
  let romNumeral = numToNumeral(inputNum.value, romNumTables);
  output.innerHTML = `${romNumeral}`;
})


const romNumTables = [
  {
    0: '',
    1: 'I',
    2: 'II',
    3: 'III',
    4: 'IV',
    5: 'V',
    6: 'VI',
    7: 'VII',
    8: 'VIII',
    9: 'IX'
  },
  {
    0: '',
    1: "X",
    2: "XX",
    3: "XXX",
    4: "XL",
    5: "L",
    6: "LX",
    7: "LXX",
    8: "LXXX",
    9: "XC"

  },
  {
    0: '',
    1: "C",
    2: "CC",
    3: "CCC",
    4: "CD",
    5: "D",
    6: "DC",
    7: "DCC",
    8: "DCCC",
    9: "DM"
  },
  {
    0: '',
    1: "M",
    2: "MM",
    3: "MMM"
  }
]

const numToNumeral = (num, table) => {
  let romNumeral = '';
  let reversedNum = num.toString().split('').reverse();
  console.log(reversedNum);
  let i = 0;
  while (i < reversedNum.length) {
    romNumeral = table[i][reversedNum[i]] += romNumeral
    i++;
  }
  console.log(romNumeral);
  return romNumeral;
}


the bug is not only that, try pressing multiple times the button


your bug is from this line

romNumeral = table[i][reversedNum[i]] += romNumeral

can you figure out why?

1 Like

Thank you for pointing me in the right direction. I am glad that I was able to pass the tests though, was a little boost to the mood that I needed today.

What strikes me as odd is that it only exhibits the behavior on the next number being submitted. If I refresh the page and submit 2025, for example, I get the desired output. This leads me to think it has something to do with the HTML not being a form.

What I don’t understand is why a second call to the handler function interacts with the first call to the handler function.

Does that make sense?

It’s a side-effect caused by the bug, take a look at the contents of romNumTables after one number is displayed. The more numbers converted the bigger side-effects.

When page is reloaded, code is freshly executed, with romNumTables starting again with initial value from the code.

it’s specifically that line!
you also have two assignments on the same line which is not really a good thing to write

you need to figure out what that line should be doing, what it is actually doing

Hey User,
Thanks for sharing your concerns. The line which has bug is:
romNumeral = table[i][reversedNum[i]] += romNumeral

Mod Edit: SOLUTION REMOVED

Add it and enjoy your coding journey!

Please do not try to write solutions for users, especially on certification projects

2 Likes

Thank you! Yesterday I caught the double assignment after posting. Yes this syntax error indeed is messing up my registry tables that my function is using to reference, thus causing the unexpected results. It makes me think I should definitely re-write single lines of code rather than cutting and pasting parts of a line of code and moving it. That resulted in this silly error. I can see it clearly now. Thanks for the breadcrumbs!

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