If you're like me and find yourself trying to gain a better understanding of algorithms, you've come to the right place. Why do I need to know them, you ask? That is outside the scope of this post, but there are lots of articles out there on the subject. At the end of the day: you just need to know them.
So as I go on my pilgrimage of algorithm enlightenment, I have found myself frequently going back to certain things and needing to look them up online. Often, these things will be basic parts of the language, but under the pressure of finding a solution to an algorithm, I find my mind going blank. To overcome that, I've come up with a list functions and a quick description of what it does to help jog my memory. Here it is for you too in case you need that lightbulb to go off.
I will note that these are meant to be quick notes, not a full summary of what the function can do. Always read the documentation to fully understand what you are using!
array.pop() => removes the last element of an array
array.shift() => removes the first element of an array
array.push() => adds element(s) to the end of an array
array.unshift() => adds element(s) to the beginning of an array
array.slice() => cut at a starting index, and optionally an end index. Original array not modified
array.splice() => changes contents of array in place
- const a = "index to start at"
- const b = "number of elements to be removed"
- const c = "element(s) to be placed"
- => to insert: .splice(a, b, c)
- => to delete: .splice(a, b)
- => to replace: .splice(a, 1, c)
string.split('') => string to array of string elements
array.join('') => array to string of array elements
parseInt(stringThatRepresentsNumber) => string to number
Math.max(num1, num2, ...) => find largest number
Math.min(num1, num2, ...) => find smallest number
Math.sqrt(num) => square root of num
Math.pow(num, power) => num to power x
Math.ceil(num) => rounds up
Math.floor(num) => rounds down
[...new Set(array)] => creates an array with unique elements
array.sort((a,b)=> a-b) => appropriately sorts an array of numbers
Need to get each individual digit of an array?
while(num > 0) {
last_digit = num % 10
console.log(last_digit)
num = Math.floor(num / 10)
}
I am sure there are plenty more helpful bits out there, but these are the ones I run into most frequently. Have any other "go-tos" that I didn't list here? Add a comment with them and we can keep this list growing!
Top comments (0)