+ 2

Why does it crazy things?

I tried to make a crypting machine, but when I tried to change the steps with an input, it did weird things! Can you say me what I have to change so that I can input the steps in the "stpinp"-input and it does not use greech letters? Thanks! https://code.sololearn.com/Wmfhu4HeN8ys/#

5th Aug 2017, 8:26 PM
Raphael Burkardt
Raphael Burkardt - avatar
2 Réponses
+ 6
The 'value' attribute is always of type string (as all html attributes, except the booleans type, as 'disabled' or 'checked')... So, when you do: wrk += ssteps; ... even if 'wrk' is initially of type Number, ssteps is of type String, but adding Number and String will implicitly cast the number to String and do concatenation ^^ (so for wrk==42, and ssteps="6", wrk+=ssteps will result to "426" and not 48: this explain the greek letters which are at the resulted String, implicitly converted to Number later)... By your "weird" correction, you implicitly cast (convert) the wrk String to Number (incrementation is implicitly only done on a Number value, there's no meaning for String)... It's not awfull, but it can be done explicitly cleaner by: ssteps = parseInt(document.getElementById("stepsinput").value);
6th Aug 2017, 6:06 PM
visph
visph - avatar
0
I "corrected" it. Now it seems to work, but the correction is REALLY WEIRD. I just added 1 to ssteps and then toke 1 away. That is working. Can you help me why? https://code.sololearn.com/Wmfhu4HeN8ys/?ref=app
5th Aug 2017, 9:50 PM
Raphael Burkardt
Raphael Burkardt - avatar