diff --git a/JavaScript/7-either.js b/JavaScript/7-either.js index fbde416..65bce8a 100644 --- a/JavaScript/7-either.js +++ b/JavaScript/7-either.js @@ -1,5 +1,13 @@ 'use strict'; +//була зроблена функція для обчислення податку з доходу + +//для зчитування введення з клавіатури +const readline = require('node:readline'); + +//податок (24%) +const taxRate = 0.24; + class Either { #left = null; #right = null; @@ -45,7 +53,7 @@ class Either { } // Usage - +/* const success = Either.right(42); const failure = Either.left(500); @@ -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(); +}); \ No newline at end of file