Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayManipulator(numbers, commands) {
- while (commands.length !== 0) {
- let command = commands.shift().split(' ');
- if (command[0] === 'add') {
- numbers.splice(Number(command[1]), 0, Number(command[2]));
- } else if (command[0] === 'addMany') {
- let input = command.slice(1).map(Number);
- let index = input.shift();
- for (let i = index; i < input.length + index; i++) {
- numbers.splice(i, 0, input[i - index]);
- }
- } else if (command[0] === 'contains') {
- console.log(numbers.indexOf(Number(command[1])));
- } else if (command[0] === 'remove') {
- numbers.splice(Number(command[1]), 1);
- } else if (command[0] === 'shift') {
- for (let i = 0; i < Number(command[1]); i++) {
- numbers.push(numbers.shift());
- }
- } else if (command[0] === 'sumPairs') {
- let x = Math.floor(numbers.length / 2);
- for (let i = 0; i < x; i++) {
- numbers[i] += numbers[i + 1];
- numbers.splice(i + 1, 1);
- }
- } else if (command[0] === 'print') {
- console.log(`[ ${numbers.join(', ')} ]`);
- }
- }
- }
- РЕШЕНИЕ СЪС SWITCH:
- function arrayManipulator(numbers, commands) {
- while(commands.length !== 0){
- let command = commands.shift().split(' ');
- switch (command[0]){
- case 'add':
- numbers.splice(Number(command[1]), 0, Number(command[2]));
- break;
- case 'addMany':
- let input = command.slice(1).map(Number);
- let index = input.shift();
- for (let i = index; i < input.length + index; i++) {
- numbers.splice(i, 0, input[i - index]);
- }
- break;
- case 'contains':
- console.log(numbers.indexOf(Number(command[1])));
- break;
- case 'remove':
- numbers.splice(Number(command[1]), 1);
- break;
- case 'shift':
- for (let i = 0; i < Number(command[1]); i++){
- numbers.push(numbers.shift());
- }
- break;
- case 'sumPairs':
- let x = Math.floor(numbers.length / 2);
- for (let i = 0; i < x ; i++){
- numbers[i] += numbers[i + 1];
- numbers.splice(i + 1, 1);
- }
- break;
- case 'print':
- console.log(`[ ${numbers.join(', ')} ]`);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement