Advertisement
Spocoman

05. Salary

Dec 24th, 2021 (edited)
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЯ С FOR:
  2.  
  3. function salary(input) {
  4.     let num = parseInt(input[0]);
  5.     let salary = parseInt(input[1]);
  6.  
  7.     for (let i = 2; i < num + 2 && salary > 0; i++) {
  8.         let apps = input[i];
  9.         if (apps === "Facebook") {
  10.             salary -= 150;
  11.         } else if (apps === "Instagram") {
  12.             salary -= 100;
  13.         } else if (apps === "Reddit") {
  14.             salary -= 50;
  15.         }
  16.     }
  17.  
  18.     if (salary <= 0) {
  19.         console.log("You have lost your salary.");
  20.     } else {
  21.         console.log(salary);
  22.     }
  23. }
  24.  
  25. Pешение с for, тернатен оператор и леко тарикатската:)
  26.  
  27. function salary(input) {
  28.     let num = parseInt(input[0]);
  29.     let salary = parseInt(input[1]);
  30.  
  31.     for (let i = 2; i < num + 2 && salary > 0; i++) {
  32.         salary -= input[i] === "Facebook" ? 150 : input[i] === "Instagram" ? 100 : input[i] === "Reddit" ? 50 : 0;
  33.     }
  34.     console.log(salary > 0 ? salary : "You have lost your salary.");
  35. }
  36.  
  37. РЕШЕНИЕ С WHILE:
  38.  
  39. Решение с while и shift():
  40.  
  41. function salary(input) {
  42.  
  43.     let num = parseInt(input.shift());
  44.     let salary = parseInt(input.shift());
  45.  
  46.     while (num-- !== 0 && salary > 0) {
  47.         let apps = input.shift();
  48.         if (apps === "Facebook") {
  49.             salary -= 150;
  50.         } else if (apps === "Instagram") {
  51.             salary -= 100;
  52.         } else if (apps === "Reddit") {
  53.             salary -= 50;
  54.         }
  55.     }
  56.  
  57.     if (salary <= 0) {
  58.         console.log("You have lost your salary.");
  59.     } else {
  60.         console.log(salary);
  61.     }
  62. }
  63.  
  64. Решение с while, shift() и тернарен оператор:
  65.  
  66. function salary(input) {
  67.     let num = parseInt(input.shift());
  68.     let salary = parseInt(input.shift());
  69.  
  70.     while (num-- !== 0 && salary > 0) {
  71.         let apps = input.shift();
  72.         salary -= apps === "Facebook" ? 150 : apps === "Instagram" ? 100 : apps === "Reddit" ? 50 : 0;
  73.     }
  74.  
  75.     console.log(salary > 0 ? salary : "You have lost your salary.");
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement