admin管理员组

文章数量:1024054

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

Check 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<<<
}

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

Check 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