diff --git a/solutions/javascript/freelancer-rates/1/freelancer-rates.js b/solutions/javascript/freelancer-rates/1/freelancer-rates.js new file mode 100644 index 0000000..ac3b197 --- /dev/null +++ b/solutions/javascript/freelancer-rates/1/freelancer-rates.js @@ -0,0 +1,71 @@ +// @ts-check +// +// ☝🏽 The line above enables type checking for this file. Various IDEs interpret +// the @ts-check directive. It will give you helpful autocompletion on the web +// and supported IDEs when implementing this exercise. You don't need to +// understand types, JSDoc, or TypeScript in order to complete this JavaScript +// exercise, and can completely ignore this comment block and directive. + +// 👋🏽 Hi again! +// +// A quick reminder about exercise stubs: +// +// 💡 You're allowed to completely clear any stub before you get started. Often +// we recommend using the stub, because they are already set-up correctly to +// work with the tests, which you can find in ./freelancer-rates.spec.js. +// +// 💡 You don't need to write JSDoc comment blocks yourself; it is not expected +// in idiomatic JavaScript, but some companies and style-guides do enforce them. +// +// Get those rates calculated! + +/** + * The day rate, given a rate per hour + * + * @param {number} ratePerHour + * @returns {number} the rate per day + */ + +export function dayRate(ratePerHour) { + return ratePerHour * 8; +} + +/** + * Calculates the number of days in a budget, rounded down + * + * @param {number} budget: the total budget + * @param {number} ratePerHour: the rate per hour + * @returns {number} the number of days + */ +export function daysInBudget(budget, ratePerHour) { + const dayRate = ratePerHour * 8; + return Math.floor(budget/dayRate); + +} + +/** + * Calculates the discounted rate for large projects, rounded up + * + * @param {number} ratePerHour + * @param {number} numDays: number of days the project spans + * @param {number} discount: for example 20% written as 0.2 + * @returns {number} the rounded up discounted rate + */ +export function priceWithMonthlyDiscount(ratePerHour, numDays, discount) { + + const daysInMonth = 22; + const dailyRate = ratePerHour * 8; // assuming 8 work hours per day + + const fullMonths = Math.floor(numDays / daysInMonth); + const remainingDays = numDays % daysInMonth; + + const discountedMonthlyCost = daysInMonth * dailyRate * (1 - discount); + const remainingCost = remainingDays * dailyRate; + + const totalCost = fullMonths * discountedMonthlyCost + remainingCost; + + return Math.ceil(totalCost); // round up to the nearest whole number +} + + +