Advertisement
Spocoman

04. Clever Lily

Dec 24th, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cleverLily(input) {
  2.     let age = Number(input[0]);
  3.     let machine = Number(input[1]);
  4.     let toyPrice = Number(input[2]);
  5.     let sum = 0;
  6.     let toy = 0;
  7.     let birthday = 10;
  8.  
  9.     for (let i = 3; i < age + 3; i++) {
  10.         if (i % 2 === 0) {
  11.             sum += birthday - 1;
  12.             birthday += 10;
  13.         } else {
  14.             toy++;
  15.         }
  16.     }
  17.     sum += toy * toyPrice;
  18.  
  19.     if (sum >= machine) {
  20.         console.log(`Yes! ${(sum - machine).toFixed(2)}`);
  21.     } else {
  22.         console.log(`No! ${(machine - sum).toFixed(2)}`);
  23.     }
  24. }
  25.  
  26. Решение с тернарен оператор леко тарикатската:
  27.  
  28. function cleverLily(input) {
  29.     let age = Number(input[0]);
  30.     let machine = Number(input[1]);
  31.     let toyPrice = Number(input[2]);
  32.  
  33.     for (let i = 1; i <= age; i++) {
  34.         machine -= i % 2 == 0 ? 5 * i - 1 : toyPrice
  35.     }
  36.  
  37.     console.log(`${machine > 0 ? "No" : "Yes"}! ${Math.abs(machine).toFixed(2)}`);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement