Skip to content

Instantly share code, notes, and snippets.

@borislavstoychev
Created May 8, 2021 14:03
Show Gist options
  • Save borislavstoychev/c230ee26f40ee3faeea5855832eb0758 to your computer and use it in GitHub Desktop.
Save borislavstoychev/c230ee26f40ee3faeea5855832eb0758 to your computer and use it in GitHub Desktop.
error
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