Skip to content

Instantly share code, notes, and snippets.

@DevGW
Last active April 5, 2023 14:39
Show Gist options
  • Save DevGW/77a8ad5acf66caf776259f033f06edec to your computer and use it in GitHub Desktop.
Save DevGW/77a8ad5acf66caf776259f033f06edec to your computer and use it in GitHub Desktop.
Javascript :: Arrays I & Methods #js
// Methods
array.push("element") // adds element to end of array
array.pop // removes and returns most recently added element
array.shift // removes and returns first element of array
array.unshift("George") // puts George in beginning of array
array.indexOf("George") // returns what element George is in array (-1 means not found)
array.slice(0,2) // gets first two elements
array[0] // returns first element of array by index [bracket access]
array[2] = "David" // replaces third element with the value "David"
array[array.length -1] // returns last element of array
array.includes("Example") // returns boolean whether Example is in array
array.reverse // reverses order of array (first to last, last to first)
array.isArray(arrayName) // returns boolean if it is array
array.splice(1, 2, "Ringo", "Star") // splices out 2 elems at index 1 and adds Ringo and Star as new elems at the same index
array.join(, ) // puts , [comma space] in between all elems of array
array.concat(otherArray) // puts contents of both arrays together into first array as elements
// Math.floor(x/y) ---> rounds down
// EXAMPLES FOLLOW
function oddCouple(nums) {
let oddNums = [];
for (i=0; i < nums.length; i++) {
if (nums[i] % 2 !== 0) {
oddNums.push(nums[i]);
}
}
return oddNums.slice(0,2);
}
// returns an array with the first two odd numbers from the input
// returns an array with the only odd number if the input has only one odd number
// returns an empty array if the input has no odd numbers
// if searchValue found inside array, return true
function myIncludes(array, searchValue) {
if (array.indexOf(searchValue) > -1) {
return true;
} else {
return false;
}
}
// looking for a match inside array from right to left
function myLastIndexOf(array, searchValue, startIdx=array.length) {
for (let i=startIdx; i >= 0; --i) {
const currentValue = array[i];
if (currentValue === searchValue) return i;
} return -1;
}
// making a reverse array without using .reverse
function myReverse(anArr) {
const reversedArray = [];
for (let i = anArr.length -1; i >= 0; --i) {
const currentElem = anArr[i];
reversedArray.push(currentElem);
}
return reversedArray;
}
// returns a new array with the value as the first index
function myUnshift(anArr, value) {
anArr.unshift(value);
return anArr;
}
// do the same thing but without unshift using .concat:
function myUnshift(anArr, elem) {
// [elem, rest of anArr]
// .concat : take one array and append another onto its end:
return [elem].concat(anArr);
}
// do the same thing using spread operator
function myUnshift(anArr, elem) {
return [elem, ...anArr]; // using the new spread operator
}
// refactoring spread operator method and const instead of function:
const myUnshift = (anArr, elem) => [elem, ...anArr]; // using the new spread operator
// shift and unshift and slice and pop with a conditional const
// but this one is inefficent because it will rotate more than the size of the array (so see below this one)
function rotateArray(anArr, rotateNum) {
const direction = rotateNum >= 0 ? 'right' : 'left';
let shiftedArray = anArr.slice();
for (let i = 0; i < Math.abs(rotateNum); i++) {
if (direction === 'right') {
shiftedArray.unshift(shiftedArray.pop());
}
else {
shiftedArray.push(shiftedArray.shift());
}
}
return shiftedArray;
}
// more efficient
function rotateArray(anArr, rotateNum) {
const direction = rotateNum >= 0 ? 'right' : 'left';
let shiftedArray = anArr.slice();
for (let i = 0; i < Math.abs(rotateNum) % anArr.length; i++) { // we added modulus anArr.length
if (direction === 'right') {
shiftedArray.unshift(shiftedArray.pop());
}
else {
shiftedArray.push(shiftedArray.shift());
}
}
return shiftedArray;
}
// my own reverse method
function reverseArray(anArr) {
for (let i = 0; i < Math.floor(anArr.length / 2); i++) {
let rightPos = (anArr.length -1) -i;
let leftElem = anArr[i];
let rightElem = anArr[rightPos];
anArr[i] = rightElem;
anArr[rightPos] = leftElem;
}
return anArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment