Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let moneyNeededForEcur = Number(input[0]);
- let moneySheHave = Number(input[1]);
- let index = 2;
- let counter = 0;
- let counter2 = 0;
- while (input[index] !== undefined) {
- counter2 += 1;
- if (input[index] === 'spend') {
- counter += 1;
- if (moneySheHave < Number(input[index + 1])) {
- moneySheHave = 0;
- } else {
- moneySheHave = moneySheHave - Number(input[index + 1]);
- }
- if (counter > 4) {
- console.log(`You can't save the money.`);
- console.log(counter);
- break;
- }
- } else if (input[index] === 'save') {
- moneySheHave += Number(input[index + 1]);
- if (moneySheHave >= moneyNeededForEcur) {
- console.log(`You saved the money for ${counter2} days.`);
- break;
- }
- }
- index = index + 2;
- } // While loop
- }
- solve([
- // '2000',
- // '1000',
- // 'spend',
- // '1200',
- // 'save',
- // '2000'
- '250',
- '150',
- 'spend',
- '50',
- 'spend',
- '50',
- 'save',
- '100',
- 'save',
- '100'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement