|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Задание 1: |
| 4 | +// Нужно определить процент скидки на основе типа клиента и суммы покупки, полученных из аргументов командной строки. |
| 5 | + |
| 6 | +function getDiscountPercentage(customerType, purchaseAmount) { |
| 7 | + if (!customerType || typeof customerType !== 'string') { |
| 8 | + return `0%`; |
| 9 | + } |
| 10 | + |
| 11 | + if (!purchaseAmount || typeof purchaseAmount !== 'number') { |
| 12 | + return `0%`; |
| 13 | + } |
| 14 | + |
| 15 | + customerType = customerType.trim(); |
| 16 | + let discountPercent = 0; |
| 17 | + |
| 18 | + if (customerType === 'vip') { |
| 19 | + discountPercent = 15; |
| 20 | + } else if (customerType === 'premium') { |
| 21 | + discountPercent = 10; |
| 22 | + } else if (customerType === 'regular') { |
| 23 | + if (purchaseAmount >= 1000) { |
| 24 | + discountPercent = 5; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return `${discountPercent}%`; |
| 29 | +} |
| 30 | + |
| 31 | +console.log(getDiscountPercentage('regular', 500)); // "0%" |
| 32 | +console.log(getDiscountPercentage('regular', 1500)); // "5%" |
| 33 | +console.log(getDiscountPercentage('premium', 300)); // "10%" |
| 34 | +console.log(getDiscountPercentage('vip', 800)); // "15%" |
| 35 | +console.log(getDiscountPercentage('', 800)); // "0%" |
| 36 | +console.log(getDiscountPercentage('vip')); // "0%" |
| 37 | + |
| 38 | +// Задание 2: |
| 39 | +// Определить тип подписки (BASIC, STANDARD, PREMIUM) на основе количества входов, статуса обучения и возраста аккаунта. |
| 40 | + |
| 41 | +function getSubscriptionType( |
| 42 | + monthlyLogins, |
| 43 | + hasCompletedTutorialStr, |
| 44 | + accountAge |
| 45 | +) { |
| 46 | + if (!monthlyLogins || typeof monthlyLogins !== 'number') { |
| 47 | + return ''; |
| 48 | + } |
| 49 | + |
| 50 | + if (!hasCompletedTutorialStr || typeof hasCompletedTutorialStr !== 'string') { |
| 51 | + return ''; |
| 52 | + } |
| 53 | + |
| 54 | + if (!accountAge || typeof accountAge !== 'number') { |
| 55 | + return ''; |
| 56 | + } |
| 57 | + |
| 58 | + hasCompletedTutorialStr = hasCompletedTutorialStr.trim().toLowerCase(); |
| 59 | + const hasCompletedTutorial = hasCompletedTutorialStr === 'true'; |
| 60 | + |
| 61 | + if (hasCompletedTutorial === false || monthlyLogins < 5) { |
| 62 | + return 'BASIC'; |
| 63 | + } else if ( |
| 64 | + hasCompletedTutorial === true && |
| 65 | + monthlyLogins >= 20 && |
| 66 | + accountAge > 30 |
| 67 | + ) { |
| 68 | + return 'PREMIUM'; |
| 69 | + } else { |
| 70 | + return 'STANDARD'; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +console.log(getSubscriptionType(3, 'false', 45)); // "BASIC" |
| 75 | +console.log(getSubscriptionType(25, 'true', 60)); // "PREMIUM" |
| 76 | +console.log(getSubscriptionType(12, 'true', 15)); // "STANDARD" |
| 77 | +console.log(getSubscriptionType(8, 'false', 100)); // "BASIC" |
| 78 | +console.log(getSubscriptionType(1, ' TRUE', 10)); // "BASIC" |
| 79 | +console.log(getSubscriptionType()); // "" |
0 commit comments