A place to keep all the algorithm exercises I've done.
array.unshift(somethang)
- adds to the beginning of the arrayarray.push(sumthing)
- adds to the end of the arrayarray.shift()
- removes and returns the first elementarray.pop()
- removes and returns the last element- Use
slice
instead ofdelete
to delete an item from an array. Usingdelete
replaces the item withundefined
instead of removing it from the array array.slice(fromThisIndex, rightBeforeThisIndex)
- returns a new array with values from start and end paramentersarray.splice(index, deleteCount, addThis)
- returns a new array with value at index and the # of elements deleted, and replacing it with addThis value- Careful: "In-place" does not mean "without creating any additional variables"! Rather, it means "without creating a new copy of the input." In general, an in-place function will only create additional variables that are O(1) space
Array.prototype.push.apply(array1, array2)
- to combine two arrays. array1 now has array2 appended to it. Calling the method returns the length thoNumber.isInteger(35)
returns true if the arugment is an integerArray.isArray([35])
returns true if the argument is an array- Get the max or the min of an array of numbers
var numbers = [5, 458, -215];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Max.min.apply(Math, numbers);
- Get the max or the min of two numbers:
Math.min(25,2)
,Math.max(25,2)
- first in, last out
- one way:
unshift()
andshift()
- another way:
push()
andpop()
push()
appends an item to the end of the arraypop()
removes and returns the last item in the array
stack.add = stack.push(somethang);
stack.remove = stack.pop();
var stack = [];
stack.push(2); // stack is now [2]
stack.push(5); // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i); // displays 5
- first in, first out
- one way:
unshift()
andpop()
- another way:
push()
andshift()
push()
inserts the passed argument at the end of the arrayshift()
removes and returns the first item
queue.add = queue.push;
queue.remove = queue.shift();
var queue = [];
queue.push(2); // queue is now [2]
queue.push(5); // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i); // displays 2