OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Check if number is within a range

  • Thread starter Thread starter RRB
  • Start date Start date
R

RRB

Guest
I have a total amount and then I have an amount that a user can insert.

I want to check whether the amount that the user inserts is within 0.2 of the total amount.

eg. Total amount is 58.79 and the user inserts 58.78 or 58.80, if this is the case then I want to console out "Allowed" else if not the case then "Not allowed".

another eg. Total amount is 60 and the user inserts 59.99 or 60.01, if this is the case then I want to console out "Allowed" else if not the case then "Not allowed".

How can I go about this?

My attempt

Code:
  const Range = totalAmount - userInsertedAmount;
  if ( (Range < 0.2) || (Range > 0.2)) {
    console.log('valid');
  } else {
    console.log('invalid');
  }

One last thing, is there a way to do it without using any built in Javascript methods like abs etc.

<p>I have a total amount and then I have an amount that a user can insert.</p>
<p>I want to check whether the amount that the user inserts is within 0.2 of the total amount.</p>
<p>eg. Total amount is 58.79 and the user inserts 58.78 or 58.80, if this is the case then I want to console out "Allowed" else if not the case then "Not allowed".</p>
<p>another eg. Total amount is 60 and the user inserts 59.99 or 60.01, if this is the case then I want to console out "Allowed" else if not the case then "Not allowed".</p>
<p>How can I go about this?</p>
<p>My attempt</p>
<pre><code> const Range = totalAmount - userInsertedAmount;
if ( (Range < 0.2) || (Range > 0.2)) {
console.log('valid');
} else {
console.log('invalid');
}
</code></pre>
<p>One last thing, is there a way to do it without using any built in Javascript methods like abs etc.</p>
 
Top