Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЯ С FOR:
- function salary(input) {
- let num = parseInt(input[0]);
- let salary = parseInt(input[1]);
- for (let i = 2; i < num + 2 && salary > 0; i++) {
- let apps = input[i];
- if (apps === "Facebook") {
- salary -= 150;
- } else if (apps === "Instagram") {
- salary -= 100;
- } else if (apps === "Reddit") {
- salary -= 50;
- }
- }
- if (salary <= 0) {
- console.log("You have lost your salary.");
- } else {
- console.log(salary);
- }
- }
- Pешение с for, тернатен оператор и леко тарикатската:)
- function salary(input) {
- let num = parseInt(input[0]);
- let salary = parseInt(input[1]);
- for (let i = 2; i < num + 2 && salary > 0; i++) {
- salary -= input[i] === "Facebook" ? 150 : input[i] === "Instagram" ? 100 : input[i] === "Reddit" ? 50 : 0;
- }
- console.log(salary > 0 ? salary : "You have lost your salary.");
- }
- РЕШЕНИЕ С WHILE:
- Решение с while и shift():
- function salary(input) {
- let num = parseInt(input.shift());
- let salary = parseInt(input.shift());
- while (num-- !== 0 && salary > 0) {
- let apps = input.shift();
- if (apps === "Facebook") {
- salary -= 150;
- } else if (apps === "Instagram") {
- salary -= 100;
- } else if (apps === "Reddit") {
- salary -= 50;
- }
- }
- if (salary <= 0) {
- console.log("You have lost your salary.");
- } else {
- console.log(salary);
- }
- }
- Решение с while, shift() и тернарен оператор:
- function salary(input) {
- let num = parseInt(input.shift());
- let salary = parseInt(input.shift());
- while (num-- !== 0 && salary > 0) {
- let apps = input.shift();
- salary -= apps === "Facebook" ? 150 : apps === "Instagram" ? 100 : apps === "Reddit" ? 50 : 0;
- }
- console.log(salary > 0 ? salary : "You have lost your salary.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement