Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved #5110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

solved #5110

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
17 changes: 16 additions & 1 deletion src/calculateRentalCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@
*
* @return {number}
*/

const LONG_TERM = 7;
const LONG_TERM_DISCOUNT = 50;
const SHORT_TERM = 3;
const SHORT_TERM_DISCOUNT = 20;
const RENT = 40;

function calculateRentalCost(days) {
// write code here
if (days >= LONG_TERM) {
return RENT * days - LONG_TERM_DISCOUNT;
Comment on lines +14 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for applying the long-term discount is correct. However, consider adding comments to explain the logic for better readability.

}

if (days >= SHORT_TERM) {
return RENT * days - SHORT_TERM_DISCOUNT;
Comment on lines +18 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for applying the short-term discount is correct. Adding comments to explain the conditions and calculations could improve code readability.

}

return RENT * days;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return statement for rentals less than the short-term threshold is correct. Consider adding a comment to clarify that this is the default case with no discounts applied.

}

module.exports = calculateRentalCost;
12 changes: 4 additions & 8 deletions src/calculateRentalCost.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
const calculateRentalCost = require('./calculateRentalCost');

test(`Do not add any discount for less than 3 days`, () => {
expect(calculateRentalCost(2))
.toBe(80);
expect(calculateRentalCost(2)).toBe(80);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected value for 2 days of rental should be 80, which is correct given the rental rate of 40 per day with no discount.

});

test('Should add the basic discount from 3 to 6 days of rent', () => {
expect(calculateRentalCost(3))
.toBe(100);
expect(calculateRentalCost(3)).toBe(100);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected value for 3 days of rental should be 100, which is correct as it includes the short-term discount of 20.

});

test('Should add the basic discount from 3 to 6 days of rent', () => {
expect(calculateRentalCost(6))
.toBe(220);
expect(calculateRentalCost(6)).toBe(220);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected value for 6 days of rental should be 220, which is correct as it includes the short-term discount of 20.

});

test('Should add an additional discount for 7 and more days of rent', () => {
expect(calculateRentalCost(7))
.toBe(230);
expect(calculateRentalCost(7)).toBe(230);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected value for 7 days of rental should be 230, which is correct as it includes the long-term discount of 50.

});
Loading