The goal of dividing by 1 javascript

I was working on a simple programming exercise that my teacher gave me, and I noticed several times that in Javascript I need to divide the number by 1, otherwise it will return a ridiculous value. Any explanation? I have jsfiddle http://jsfiddle.net/TpNay/1/

var widthrand=Math.floor(Math.random()*widthRange); width=widthrand + document.getElementById('width').value/1;

If you look at line 22 and pull out the division by 1 and press β€œgenerate”, it will return funny lengths Thanks

+6
source share
1 answer

This does juggling type JavaScript, causing the value of document.getElementById('width').value become numeric.

The best way to do this would be parseInt(document.getElementById('width').value, 10)

+9
source

All Articles