Skip to content

Instantly share code, notes, and snippets.

@sagmor
Last active September 19, 2019 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagmor/7a53398106192088aa012a887faffdcf to your computer and use it in GitHub Desktop.
Save sagmor/7a53398106192088aa012a887faffdcf to your computer and use it in GitHub Desktop.
Algorithms For Carito
// x = [1,5,7,4]
// return [5,6,4]
function pop(x) {
var n = [];
for (var i = 0; i < x.length -1; i++) {
n[i] = x[i+1];
}
return n
}
console.log(pop([1,5,7,4])) // => [5,6,4]
// x = [1,5,7,4]
// n = 6
// return [6,1,5,6,4]
function push(x,n) {
for (var i = x.length-1; i >= 0; i--) {
x[i+1] = x[i];
}
x[0] = n
return x
}
console.log(push([1,5,7,4],6)) // =>[6,1,5,6,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment