Skip to content

Update JavaScript\7-either.js #1

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 56 additions & 1 deletion JavaScript/7-either.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

//була зроблена функція для обчислення податку з доходу

//для зчитування введення з клавіатури
const readline = require('node:readline');

//податок (24%)
const taxRate = 0.24;

class Either {
#left = null;
#right = null;
Expand Down Expand Up @@ -45,7 +53,7 @@ class Either {
}

// Usage

/*
const success = Either.right(42);
const failure = Either.left(500);

Expand All @@ -58,3 +66,50 @@ const result = failure.match(
);

console.log({ result });
*/



//функція для обчислення податку з доходу
function calculateTax(income) {
if (isNaN(income) || income <= 0) {
// Якщо ні то помилка (left)
return Either.left('Invalid income amount');
}

const tax = income * taxRate;

//якщо немає помилки (right)
return Either.right({ tax, rate: taxRate });
}


//зчитування введення користувача
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});


//логіка взаємодії з користувачем
rl.question('Введіть ваш дохід: ', (input) => {
const income = Number(input);

//виклик функції розрахунку податку
calculateTax(income)
//знаки після коми при успіху (right)
.map(({ tax, rate }) => ({
tax: tax.toFixed(2),
rate: (rate * 100).toFixed(0) + '%'
}))
//обробка результату
.match(
(err) => console.log('Помилка:', err),
({ tax, rate }) => {
console.log('Податок становить:', tax, 'при ставці', rate);
console.log('[LOG] Розрахунок виконано успішно.');
}
);

rl.close();
});