Calculate the discounted rate for large projects

I don’t know how to solve the task below. Can someone help me?

Often, the freelancer’s clients hire them for projects spanning over multiple months. In these cases, the freelancer decides to offer a discount for every full month, and the remaining days are billed at day rate. Every month has 22 billable days. Help them estimate their cost for such projects, given an hourly rate, the number of days the project spans, and a monthly discount rate. The discount is always passed as a number, where 42% becomes 0.42. The result must be rounded up to the nearest whole number.

What language are you working in? Or do you want to discuss it abstractly?

@glennj To clarify, the text posted above is part of the freelancer rate exercise in the JavaScript track.

@junedev Thanks. I know elixir has that exercise too, but I didn’t look up the actual instructions.

@Norb1 in this task, you are given an hourly rate, a number of days and the discount rate. Only the days in full months are discounted. You need to calculate how many of the days are part of full months, and how many days are leftover. The leftover days are not discounted. You can reuse the dayRate function.

1 Like

yes it is a part of JS.

Hi are you still stuck? If yes, can you please post what you have tried so that i can figure out the best way to help you?

Hi Siraj,
Can you help me? I am doing the same exercise and I’ve been stuck for over a week. I don’t know where my logic is failing but the code is not even passing the tests. Please see below, highly appreciated if you can share some light :)

export const priceWithMonthlyDiscount = function (
) {
  // Calculate Price Per Day based on the given price per hour.
  const pricePerDay = dayRate(pricePerHour);

  // Calculate discount to be applied
  const daysInMonth = numDays - (numDays % 22); // every month has 22 billable days
  const discount = pricePerDay * daysInMonth * discountPercentage;

  // Calculate price for days in a month (price - discount)
  const pricePerMonth = daysInMonth * pricePerDay - discount;

  // Calculate price for remaining days
  const priceExceedingDays = numDays % 22 * pricePerDay;

  // Sum Up price for discounted Month + exceeding days
  return priceExceedingDays + pricePerMonth;

The above code is the closest I have managed to get, it passes two tests but fails the last two:


Thanks for your help in advance!

1 Like

All you are missing is just rounding the result.

Please change the last line to this and the 2 failing tests will pass.

return Math.ceil(priceExceedingDays + pricePerMonth);

Sorry to hear that you struggled with this for a week. But the good news is that we learn a lot more deeply when we allow ourselves to struggle and not lookup the answers immediately. Please let me know if you need further help.

Your code is very readable - excellent variable names, clear comments - there is so much to appreciate about your code :)


Ah, I just had a “face-palm” moment. :woman_facepalming:t3: :sweat_smile: :melting_face:

Thank you so much for your quick response! I think I re-read the instructions so many times that I totally forgot the rounding part. I appreciate you taking the time to help me :slight_smile:

Don’t worry and embrace these moments - anyone who starts on anything new in programming faces similar moments. You are not alone! These are the moments that stick in our head and helps us to remember these ideas for a very long time (in this world of fleeting memory)

I did almost exact same, but cant pass the task

export function priceWithMonthlyDiscount(ratePerHour, numDays, discount) {
  totalBillMonths = numDays / 22;
  totalBillDays = totalBillMonths * 22;
  remainingDays = numDays % 22;
  pricePerDay = dayRate(ratePerHour);
  billWithoutDis = totalBillDays * pricePerDay;
  discountAmount = totalBillDays * pricePerDay * discount;
  totalDiscount = billWithoutDis - discountAmount;
  remainBill = remainingDays * pricePerDay;
  return totalSum = remainBill + totalDiscount;

what is wrong?

What is the error?

Note, you call the floor and ceil functions but don’t do anything with the return value so those lines have no effect.

I rewrote the code and everything worked out. I must have missed something. however, I realized one thing, that comments help to write code. thank you!!

//ratePerHour == 89, numDays == 230, discount == 0.42
function priceWithMonthlyDiscount(ratePerHour, numDays, discount) {

  //calculating all billable months
  let billableMonths = numDays % 22; // 10 nonths
  //calculating the number of days exceeding full months
  let restDays = numDays % 22; // 10 days

  //calculating all billable days
  let billableDays = numDays - restDays; // 220 days

  //calculating price for billableDays WITHOUT discount
  let fullPrice = ( dayRate( ratePerHour ) * billableDays); // 156 640

  //calculating amount of discount
  let discountAmount = fullPrice * discount; // 65 788.80

  //calculating price for billableDays WITH discount
  let discountPrice = fullPrice - discountAmount; // 90 851.20

  //calculating price for rest days
  let restDaysPrice = ( dayRate( ratePerHour )) * restDays; // 7 120

  //calculating total price for projec
  let totalPrice = discountPrice + restDaysPrice; // 97 971.20

  //rounding up
  return Math.ceil( totalPrice ); //97 971

Just to clarify what you missed so you don’t go on without knowing. As mentoned by @IsaacG earlier you weren’t storing your values for Math.ceil. Math.ceil is a function that returns a value, in your first example you did not store this value to be used again. You simply called the function. Hope this helps.

1 Like