admin管理员组文章数量:1429032
I am learning JavaScript with the p5js library and have decided to make a very basic game.
I simply want the game to end when the condition of an if statement is met. Such as
if (x == y) {
>>>endcode here<<<
}
however I need to add a tolerance of 50 to each value
so the code would be something like
if (x == y (give or take 50) ){
>>>endcode here<<<
}
I am unsure of how to add the tolerance to the statement so I have e here for some help. Thanks :)
I am learning JavaScript with the p5js library and have decided to make a very basic game.
I simply want the game to end when the condition of an if statement is met. Such as
if (x == y) {
>>>endcode here<<<
}
however I need to add a tolerance of 50 to each value
so the code would be something like
if (x == y (give or take 50) ){
>>>endcode here<<<
}
I am unsure of how to add the tolerance to the statement so I have e here for some help. Thanks :)
Share Improve this question edited Apr 27, 2019 at 11:33 Nick Parsons 51.2k6 gold badges57 silver badges78 bronze badges asked Apr 27, 2019 at 11:30 test_subject_8055test_subject_8055 651 silver badge6 bronze badges2 Answers
Reset to default 7Check to see if the absolute value of the difference is less than 50:
if (Math.abs(x - y) <= 50) {
// etc
}
I'm assuming you want to permit, eg 50 and 99, but prohibit 50 and 101. If you want the allow a difference of up to 100, then pare against 100 instead of 50:
if (Math.abs(x - y) <= 100) {
// etc
}
Or check upper and lower borders which you might want to change borders nonsymmetrically
if (x =< y+50 and x >= y-50 ){
>>>endcode here<<<
}
本文标签: How do I add tolerance to an if statement in JavascriptStack Overflow
版权声明:本文标题:How do I add tolerance to an if statement in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745535335a2662245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论