0
Why my code is not working on sololearn but on computer ?
I have created a simple calculator using JS .. trying to run it on sololearn but it didn't work, on the other hand it's perfectly works on computer.. why? https://code.sololearn.com/WzapFTCpqJVh/?ref=app
3 Answers
+ 1
(1st part)
because localy your js script is linked at end of <body> (when DOM is almost completly loaded) but in sololearn code playground context the JS tab (as for the CSS tab) is dynamically inserted (in relavant tag: <script> for JS and <style> for CSS, obviously) at end of <head>...
Conversely to CSS wich doesn't really matter where and when style are inserted and parsed/evaluated, for JS it would become critical (unless you didn't access the DOM before it will be ready)...
There are at least two workarounds:
+ copy/paste the JS tab where you link your external JS script, but inlined (enclosed in <script></script> pair), WITHOUT THE SRC ATTRIBUTE (wich else would cause your enclosed script to be ignored ^^).
+ enclose all your JS in an IIFE assigned to the 'load' or 'DOMContentLoaded' event listeners...
The simplest way in your case would be either the first one, or the second one with a 'DOMContentLoaded' event handler...
(to be continued)
+ 1
(2nd part)
The downside of the former is less comfortable edition/debug, the downside of the later is to be carefull of globals variables needed to stay in the global space to be accessible from anywhere (ie from html inlined event listener attributes -- but you seems to keep the good practice of defining them through JS, so that's not a problem here) and not overiding or bypassing your own 'load' event listener ('DOMContentLoaded' help in your case, as it was trigered before the 'load' event)...
Anyway, in your code you're assigning many times the 'onload' ATTRIBUTE, so laters overide formers ^^
The solution is to register the event listeners with the addEventListener method (wich could register any number of listeners for the same event) ;)
0
Thank you very much for your time 🙏🙏