Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЯ С WHILE:
- function moving(input) {
- let width = Number(input[0]);
- let length = Number(input[1]);
- let height = Number(input[2]);
- let volume = width * length * height;
- let index = 3;
- while (true) {
- let box = input[index++];
- if (box === "Done") {
- console.log(`${volume} Cubic meters left.`);
- break;
- }
- volume -= Number(box);
- if (volume < 0) {
- console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
- break;
- }
- }
- }
- Решение със shift():
- function moving(input) {
- let width = Number(input.shift());
- let length = Number(input.shift());
- let height = Number(input.shift());
- let volume = width * length * height;
- while (true) {
- let box = input.shift();
- if (box === "Done") {
- console.log(`${volume} Cubic meters left.`);
- break;
- }
- volume -= Number(box);
- if (volume < 0) {
- console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
- break;
- }
- }
- }
- Решение с shift() и тернарен оператор леко тарикатската:)
- function moving(input) {
- let volume = Number(input.shift()) * Number(input.shift()) * Number(input.shift());
- while (input[0] !== "Done" && input.length !== 0) {
- volume -= Number(input.shift());
- }
- console.log(input[0] === "Done" ? `${volume} Cubic meters left.` :
- `No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
- }
- РЕШЕНИЕ С FOR() ЛЕКО ТАРИКАТСКАТА:
- function moving(input) {
- let volume = Number(input[0]) * Number(input[1]) * Number(input[2]);
- for(let i = 3; i < input.length; i++) {
- if (input[i] === "Done") {
- console.log(`${volume} Cubic meters left.`);
- break;
- }
- volume -= Number(input[i]);
- if (volume < 0) {
- console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
- break;
- }
- }
- }
- РЕШЕНИЕ БЕЗ ЦИКЪЛ:
- function moving(input) {
- let volume = Number(input.shift()) * Number(input.shift()) * Number(input.shift()) -
- input.filter(a => a !== "Done").map(Number).reduce((a, b) => a + b, 0);
- console.log(volume >= 0 ? `${volume} Cubic meters left.` :
- `No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement