Created
May 8, 2021 14:03
-
-
Save borislavstoychev/c230ee26f40ee3faeea5855832eb0758 to your computer and use it in GitHub Desktop.
error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrManipulations(args) { | |
let myArr = args | |
.shift() | |
.split(" ") | |
.map(Number) | |
for (commands of args) { | |
let [command, firstN, secondN] = commands.split(" "); | |
firstN = Number(firstN) | |
secondN = Number(secondN) | |
switch (command) { | |
case "Add": | |
myArr.push(firstN); | |
break; | |
case "Remove": | |
let index = myArr.indexOf(firstN) | |
myArr.splice(index, index) | |
break; | |
case "RemoveAt": | |
myArr.splice(firstN, firstN) | |
break; | |
case "Insert": | |
myArr.splice(secondN, 0, firstN); | |
break; | |
} | |
} | |
return myArr.join(" ") | |
} | |
console.log(arrManipulations(['4 19 2 53 6 43', | |
'Add 3', | |
'Remove 2', | |
'RemoveAt 1', | |
'Insert 8 3' | |
])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment