Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function coins(input) {
- let change = parseInt(Number(input[0]) * 100);
- let coinCounter = 0;
- while (change !== 0) {
- if (change >= 200) {
- change -= 200;
- } else if (change >= 100) {
- change -= 100;
- } else if (change >= 50) {
- change -= 50;
- } else if (change >= 20) {
- change -= 20;
- } else if (change >= 10) {
- change -= 10;
- } else if (change >= 5) {
- change -= 5;
- } else if (change >= 2) {
- change -= 2;
- } else {
- change--;
- }
- coinCounter++;
- }
- console.log(coinCounter);
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- function coins(input) {
- let change = parseInt(Number(input[0]) * 100);
- let coinCounter = 0;
- while (change !== 0) {
- change -= change >= 200 ? 200
- : change >= 100 ? 100
- : change >= 50 ? 50
- : change >= 20 ? 20
- : change >= 10 ? 10
- : change >= 5 ? 5
- : change >= 2 ? 2 : 1;
- coinCounter++;
- }
- console.log(coinCounter);
- }
- РЕШЕНИЕ С МАСИВ:
- function coins(input) {
- let change = parseInt(Number(input[0]) * 100);
- let coins = [200, 100, 50, 20, 10, 5, 2, 1];
- let coinCounter = 0;
- for (let i = 0; i < coins.length; i++) {
- if (change >= coins[i]) {
- coinCounter += parseInt(change / coins[i]);
- change %= coins[i];
- }
- }
- console.log(coinCounter);
- }
- Pешение със forEach():
- function coins(input) {
- let change = parseInt(Number(input) * 100);
- let coinCounter = 0;
- [200, 100, 50, 20, 10, 5, 2, 1].forEach(coin => {
- coinCounter += parseInt(change / coin);
- change %= coin;
- });
- console.log(coinCounter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement