JSFiddle - React, Tailwind, and code Playground
by TheDiamondDoge
HTML
function tickets(peopleInLine) {
let cost = 25;
let cashbox = {
x: 0,
y: 0,
z: 0
};
let res;
peopleInLine.forEach((item) => {
console.log(item);
let surr = 0;
if (item == 25) {
cashbox.x++;
} else if (item == 50) {
cashbox.y++;
} else if (item == 100) {
cashbox.z++;
}
console.log(cashbox);
if (item > cost) {
surr = item - cost;
console.log('surr', surr);
}
if (surr == 25 && cashbox.x != 0) {
cashbox.x--;
res = 'YES';
} else if (surr == 75 && cashbox.y != 0 && cashbox.x != 0) {
cashbox.x--;
cashbox.y--;
res = 'YES';
} else if (surr == 75 && cashbox.x > 2) {
cashbox.x -= 3;
res = 'YES';
} else if (surr == 0) {
res = 'YES';
} else {
res = 'NO';
}
console.log('после сдачи', cashbox);
})
// console.log(cashbox);
console.log(res);
return res;
// ...
}
// tickets([25, 25, 50, 50]); //, "YES")
// tickets([25, 25]); //, "YES")
tickets([25, 50, 25, 50, 100, 25, 25, 50]); //, "NO")
// tickets([25, 100]); //, "NO")
// tickets([25, 25, 25, 25, 25, 100, 100]); //, "NO")
JavaScript
function tickets(peopleInLine) {
let cost = 25;
let cashbox = {
25: 0,
50: 0,
100: 0
};
let res = "YES";
peopleInLine.forEach((item) => {
console.log(item);
let surr = 0;
// change += item;
// if (item > cost) {
// change -= item - cost;
// }
if (item == 25) {
cashbox[25]++;
} else if (item == 50) {
cashbox[50]++;
} else if (item == 100) {
cashbox[100]++;
}
console.log(cashbox);
if (item > cost) {
surr = item - cost;
console.log('surr', surr);
}
if (surr == 25 && cashbox[25] != 0) {
cashbox[25]--;
} else if (surr == 75 && cashbox[50] != 0 && cashbox[25] != 0) {
cashbox[25]--;
cashbox[50]--;
} else if (surr == 75 && cashbox[25] > 2) {
cashbox[25] -= 3;
} else if (surr == 0) {
} else {
res = 'NO';
}
console.log('после сдачи', cashbox);
})
// console.log(cashbox);
console.log(res);
return res;
// ...
}
// tickets([25, 25, 50, 50]); //, "YES")
// tickets([25, 25]); //, "YES")
console.log(tickets([25, 50, 25, 50, 100, 25, 25, 50])); //, "NO")
// tickets([25, 100]); //, "NO")
// tickets([25, 25, 25, 25, 25, 100, 100]); //, "NO")