0

I have a function that calculates a price based off of square footage and a base price.

The price goes up 10% for every 500sqft after 2000sqft. Anything 2000sqft and below is 149.99.

The function is below...

checkPrice = () => {
   debugger;
   let base_price = 149.99;
      if(this.state.propertySqft > 2000){
   let overage = this.state.propertySqft - 2000;
   let percentage = Math.floor(overage % 500) * 10;
   base_price += base_price * percentage;
   this.setState({ totalPrice: base_price });

  }
}


the issue I am having, is the percentage comes up as '0' if the square footage is a round number, and the math.ceil(overage % 500) seems to not be working properly.

for example.... if i was to put in 5001 sqft, the price should be 149.99 * 60% which would be 239.98, but it comes up as 164.98 as the percentage only ends up being 10, instead of 60.

If i put in 5000 sqft, the percentage comes up as '0', which this is the case for any round number I enter as the square footage.

Does anyone have any idea what Im doing wrong here or why this is not working the way Im expecting it to?

7
  • 2
    I guess you want Math.floor(overage / 500) ... Commented Oct 24, 2019 at 15:54
  • Also your percentage is in percent, you want to turn it into [0, 1] by dividing it with 100. Then add 1, and multiply the price with that. Commented Oct 24, 2019 at 15:55
  • I tried that as well! Sorry I should have clarified that I tried that already. It gives me the same issue which is why Im so confused lol. Thanks for commenting! Commented Oct 24, 2019 at 15:57
  • 3000 % 500 is 0 ... your debugger should tell you that ... Commented Oct 24, 2019 at 15:57
  • It does tell me its 0 but I just wasnt sure why it was 0, i was under the impression that it would be 6. Also I didnt try to turn it into [0, 1] by dividing it with 100. Then add 1.. maybe thats my issue? Ill try that now. Commented Oct 24, 2019 at 16:00

1 Answer 1

1

The modulo operator is used to get the distance to the next lower multiple, e.g. 2006 % 500 would be 6, as 500 * 4 + 6 = 2006. You want to divide, to get 4 in this case: 2006 / 500 = 4.00..., then floor it to the next integer.

Also if you multiply the base price with 10, you don't increase it by 10%, rather by 1000%. You probably want 0.1 (10 per hundred).

I'd write it as:

 const basePrice = 149.99;

 const checkPrice = () => {
  this.setState(({ propertySqft }) => { // asynchronous access to this.state is dangerous, use a callback!     
    const percentage = Math.max(
      0, // ensure that it only gets more 
      Math.floor((propertySqft - 2000) / 500) * (10 / 100)
    );

    return { totalPrice: basePrice * (1 + percentage),  };
  });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.