Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let array = input.shift().split(' ').map(Number);
- input.forEach(element => {
- element = element.split(' ');
- let [a, b, c] = element;
- if (a === 'decrease') {
- array = array.map(function (x) {
- return x - 1
- });
- } else if (a === 'swap') {
- let valueB = array[b];
- let valueC = array[c];
- array.splice(b, 1, valueC);
- array.splice(c, 1, valueB);
- } else if (a === 'multiply') {
- let tempoA = array[b];
- let tempoB = array[c];
- let result = tempoA * tempoB;
- array.splice(b, 1, result);
- } else if (a === 'end') {
- console.log(array.join(', '));
- }
- });
- }
- solve(['1 2 3 4', 'swap 0 1', 'swap 1 2', 'swap 2 3', 'multiply 1 2', 'decrease', 'end']);
- solve(['23 -2 321 87 42 90 -123', 'swap 1 3', 'swap 3 6', 'swap 1 0', 'multiply 1 2', 'multiply 2 1', 'decrease', 'end']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement