html - Purpose of dividing by 1 javascript - Stack Overflow

I was working on a simple programming exercise my teacher gave us, and I noticed several times that in

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 /

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

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/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

Share Improve this question edited Feb 15, 2013 at 1:38 John Conde 220k99 gold badges463 silver badges502 bronze badges asked Feb 15, 2013 at 1:20 scrblnrd3scrblnrd3 7,4349 gold badges36 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745658045a4638661.html

相关推荐

  • html - Purpose of dividing by 1 javascript - Stack Overflow

    I was working on a simple programming exercise my teacher gave us, and I noticed several times that in

    22天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信