Roman numeral converter not working in fcc

Hi All,

I have just finished my roman numeral converter. I have made it in vscode and copied it over to fcc. It doesn’t work in fcc. It works on vscode and also codepen: https://codepen.io/Jago971/pen/YzMRPyZ

The console reads:
Uncaught TypeError: Cannot read properties of null (reading ‘addEventListener’)

When playing around with it in fcc, it appears not to read any of my variables. I changed the event listener to document instead of convertBtn and it just throws an error for the next use of a variable.

Can someone please help me in getting FCC to work.

Thanks, MM

Your <script> tag is inside the <head> tag with an attribute defer.

The issue here is that defer attribute means that the file is executed after parsing HTML code but BEFORE the event “DOMContentLoaded” which constructs your DOM (Document Object Model), so you can’t work with event listeners or target any elements before that event fires.


Here’s the sequence of events when using a script with the defer attribute:

  1. HTML parsing begins.
  2. Script with defer attribute is encountered.
  3. HTML parsing continues.
  4. Script with defer attribute is executed.
  5. DOMContentLoaded event fires after the script with defer has executed.

What i did is that i removed that <script> tag from your head and placed it to the very end before your closing </body> tag and it works fine (You can remove defer attribute now).

That way, the file is executed at the very end after your HTML is loaded and DOM is constructed so you can now work as you like.

You can read more here at MDN Documentation about defer attribute.

1 Like

in short, defer doesn’t work in the environment we have

Just to be super clear. It is only an issue with the fCC editor.

The script file is not used as a file, the content is added as an inline script to the script element. And defer only works with an external script, not inline script.

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