Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let capacity = Number(input[0]);
- let countLoaded = 0;
- let result = 0;
- let sum = 0;
- for (let i = 1; i < input.length + 1; i++) {
- if (input[i] !== 'End' && input[i] !== undefined) {
- sum = +input[i];
- countLoaded += 1;
- }
- result = countLoaded / 3;
- if (Number.isInteger(result) && i > 1) {
- sum = sum + (+input[i] + (+input[i] * (10 / 100)));
- }
- if (input[i] !== undefined) {
- capacity = capacity - sum;
- }
- if (input[i] === 'End' || capacity <= 0 && input[i] === input[input.length - 2] && input[input.length - 1] === 'End' || input[i] === undefined) {
- console.log(`Congratulations! All suitcases are loaded!`)
- break;
- } else if (capacity <= 0 && input[i] !== 'End' && i > 1) {
- console.log(`No more space!`);
- break;
- }
- }
- console.log(`Statistic: ${countLoaded} suitcases loaded.`);
- }
- solve([
- '1700.5',
- '180',
- '340.6',
- '126',
- '222'
- // '1500.2',
- // '260',
- // '380.5',
- // '125.6',
- // '305',
- // '700',
- // 'End'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement