Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //08. Array Manipulator
- function manipolator(sequence, operations) {
- let newArr = sequence.slice();
- let outPrint = 0;
- let contains = 0;
- let flag = false;
- for (let i = 0; i < operations.length; i++) {
- let splited = operations[i].split(' ')
- let command = [];
- for (let ind = 0; ind < splited.length; ind++) {
- command.push(splited[ind]);
- }
- switch (command[0]) {
- case "add":
- newArr.splice(command[1], 0, Number(command[2]));
- break;
- case "addMany":
- let newPosion = Number(command[1]);
- for (let many = 1; many < command.length - 1; many++) {
- newArr.splice(newPosion, 0, Number(command[many + 1]));
- newPosion++;
- }
- break;
- case "contains":
- flag = true;
- console.log(newArr.indexOf(Number(command[1])));
- break;
- case "remove":
- newArr.splice(command[1], 1)
- break;
- case "shift":
- let tempo = newArr.slice(0, command[1]);
- for (let a of tempo) {
- newArr.push(a);
- newArr.shift();
- }
- break;
- case "sumPairs":
- sumPairs()
- break;
- case "print":
- break;
- }
- }
- function fn(x) {
- newArr.push(newArr[many + 1])
- }
- function sumPairs() {
- newArr = newArr.map((e, i, newArr) => i < newArr.length - 1 ? (newArr[i] += newArr[i + 1]) : newArr[i] = newArr[i]).filter((e, i) => i % 2 === 0);
- }
- console.log('[ ' + newArr.join(', ') + ' ]');
- //console.log(newArr.map(Number));
- }
- //manipolator([1, 2, 4, 5, 6, 7], ['add 1 8', 'contains 1', 'contains 3', 'print']);
- // manipolator([2, 2, 4, 2, 4], ["add 1 4", "sumPairs", "print"])
- //manipolator([1, 2, 3, 4, 5], ["addMany 5 9 8 7 6 5", "contains 15", "remove 3", "shift 1", "print"])
- manipolator([2, 2, 4, 2, 4],["add 1 4", "sumPairs", "print"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement