Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Let me start by saying I know absolutely NOTHING about javascript. I've got a one time problem and don't know where to start.

I'm trying to set up a semi-automated PDF for people at work to use. There is a quantity field and a 'fee' field. I'd like to put some javascript in the 'fee' field so that it calculates the price based on the quantity.


If it was straight quantity*price-per, it would be easy and I think I could do it myself. The problem is that the 'fee' is a combination of one price for the first thousand and a different price for each additional thousand or fraction thereof.

Example:

First 1,000 costs $5
Additional 1,000s or fractions thereof are $3

So a quantity of 4211 should cost:
$5 for the first thousand
$12 for the remainder (3 thousands + 211[counts as a thousand]= 4 thousands)
$17 in total

Any help would be greatly appreciated... especially considering I have no idea what I'm doing!
 

Mr Evil

Senior member
Jul 24, 2015
464
187
116
mrevil.asvachin.com
It sounds like your problem is more with turning the maths into an algorithm than the JavaScript. Assuming I understand the rules, it would look something like this in pseudo-code:
Code:
chargableAmount = amount rounded up to next thousand
fee = 5
fee += 3 for each thousand above 1000 in chargableAmount
And since I'm still in programming mode, here's an implementation in JavaScript:
Code:
function FeeFor (amount) {
  var increment = 1000,
      first = 5,
      subsequent = 3;
  var thousands = Math.ceil(amount/increment);
  return thousands > 0
    ? first + ((thousands - 1) * subsequent)
    : 0;
}
Ceil rounds up to the next whole number, so it will round 4211/1000 to 5. FeeFor(4211) will return 17.
 
Reactions: Netopia

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Mr Evil.... that was BEAUTIFUL! Though I'm not much of a programmer, I can follow the logic in your code. Thank you!

I was looking for 'round' and 'ceil'ing hadn't even occurred to me, though I use it in Excel.

A question because of my javascript ignorance:

Why do you do a single declaration for the first three variables, but then have to type in 'var' again on the fourth line? Could you have left that second 'var' out, or is there a difference that requires you to use it again?

One more time.... thank you!
 

Mr Evil

Senior member
Jul 24, 2015
464
187
116
mrevil.asvachin.com
How many "var"s you use is mainly a matter of style. I divided it into two blocks to make it easier to see which are literals and which is the calculated value. You could declare them all in one "var" if you prefer.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |