scrblnrd3
scrblnrd3

Reputation: 7426

Purpose of dividing by 1 javascript

I was working on a simple programming exercise my teacher gave us, and I noticed several times that in Javascript, I have to divide a number by 1, otherwise it will return a ridiculous value. Any explanations? I have a 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 take out the divide by 1, and click generate, it will return ridiculous lengths Thanks

Upvotes: 7

Views: 820

Answers (1)

John Conde
John Conde

Reputation: 219894

It makes JavaScript type juggle forcing the value of document.getElementById('width').value to become numeric.

A better way to do it would be parseInt(document.getElementById('width').value, 10)

Upvotes: 11

Related Questions