0
Problem
I have a problem with writting in js tab in sololearn. I wrote simple input with id and button with function that adds 1 to the value of input with id and it prints me that it can't set value of null https://code.sololearn.com/W1DLAuJajaVB/?ref=app
1 Respuesta
+ 2
1.
If you removed the scripts in the JS tabs, there is no null error, but the result is appending "1" to ones, outcome: "1", "11", "111",....
reason is Type Coercion
because the plus operator have method overloading, when both operands are number, it performs Number
addition(); however, when one of the operand is string, it performs String.concantenation().
Solution is to convert the type with Number() or parseInt()
let curValue = inputDOM.value;
curValue = parseInt(curValue);
curValue = curValue + 1;
https://www.sololearn.com/post/115762/?ref=app
2. Now, back to your null error, because SoloLearn JS tab puts your scripts in <head>, your getElementById() is trying to look for a DOM which has not existed yet, that's why it is null.
Solution is to
window.onload = function() {
your scripts
}
if you want to put scripts in JS Tab.
https://www.sololearn.com/post/90825/?ref=app
3.
https://code.sololearn.com/WKKkpq0efxai/?ref=app