Last active
May 17, 2019 19:21
-
-
Save jarhoads/c718f4690df716d604449f886cbd54c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// array literal | |
let array1 = [1,2,3,4]; | |
array1.push(5); //array1 = [1,2,3,4,5] | |
array1.push(7); //array1 = [1,2,3,4,5,7] | |
array1.push(2); //array1 = [1,2,3,4,5,7,2] | |
console.log(array1); | |
// deletion: pop returns last item | |
let array2 = [1,2,3,4]; | |
array2.pop(); //returns 4, array2 = [1,2,3] | |
array2.pop(); //returns 3, array2 = [1,2] | |
console.log(array2); | |
// deletion with shift: returns first item | |
let array3 = [1,2,3,4]; | |
array3.shift(); //returns 1, array3 = [2,3,4] | |
array3.shift(); //returns 2, array3 = [3,4] | |
console.log(array3); | |
// array access | |
let array4 = [1,2,3,4]; | |
console.log(array4[0]); //returns 1 | |
console.log(array4[1]); //returns 2 | |
// iteration | |
for (let i=0, len=array1.length; i<len; i++) { | |
console.log(array1[i]); | |
} | |
// for-in: gives index | |
let array5 = ['all','cows','are','big']; | |
for(let i in array5){ console.log(array5[i]); } | |
//for-of gives values | |
for(let element of array5){ console.log(element); } | |
// for-each: takes function with element, index params | |
array5.forEach(function(element, index){ console.log(element); }); | |
console.log('----------'); | |
// using lambdas | |
array5.forEach((element, index) => console.log(element)); | |
// slice - returns portion of array, doesn't modify array | |
var array6 = [1,2,3,4]; | |
array6.slice(1,2); //returns [2], array6 = [1,2,3,4] | |
array6.slice(2,4); //returns [3,4], array6 = [1,2,3,4] | |
// if only beginning passed: end of array assumed | |
array6.slice(1); //returns [2,3,4], array6 = [1,2,3,4] | |
// nothing passed -> copies array (new address) | |
array6.slice(); //returns [1,2,3,4], array6 = [1,2,3,4] | |
// can also make a new copy with from | |
let array7 = [1,2,3,4]; | |
let array8 = Array.from(array7); | |
// splice - splice(begin,size,element1,element2...) | |
// takes three parameters: the beginning index, the size of things to be removed, | |
// and the new elements to add. New elements are added at the position specified by the first parameter. | |
// It returns the removed elements. | |
let array9 = [1,2,3,4]; | |
array9.splice(); //returns [], array9 = [1,2,3,4] | |
array9.splice(1,2); //returns [2,3], array9 = [1,4] | |
// concat | |
let array10 = [1,2,3,4]; | |
array10.concat(); //returns [1,2,3,4], array1 = [1,2,3,4] | |
array10.concat([2,3,4]); //returns [1,2,3,4,2,3,4],array10 = [1,2,3,4] | |
//The .length property returns the size of the array. | |
// Changing this property to a lower size can delete elements from the array. | |
let array11 = [1,2,3,4]; | |
console.log(array11.length); //prints 4 | |
array11.length = 3; // array1 = [1,2,3] | |
//The spread operator, denoted by three periods (…) | |
// used to expand arguments where zero arguments are expected. | |
function addFourNums(a, b, c, d){ return a+b+c+d; } | |
let nums = [1,2,3,4]; | |
console.log(...nums); // 10 | |
// max and min | |
var array12 = [1,2,3,4,5]; | |
Math.max(array12); // 5 | |
// building a range function | |
const range = (start, end) => Array.from({length: (end - start) + 1}, (x,i) => i + start); | |
console.log(range(1,10)); | |
// sequence | |
const sequence = (n) => Array.from({length: n}, (x,i) => i); | |
console.log(sequence(10)); | |
let mapped = sequence(10).map(x => x*2); | |
console.log(mapped); | |
// functional array processing | |
// map | |
let tenTimes = [1,2,3,4,5,6,7].map(function (value){ return value*10; }); // [10, 20, 30, 40, 50, 60, 70] | |
console.log(tenTimes); | |
// can also use lambda functions | |
const double = (val) => val*2; | |
range(1,7).map(double); | |
// fizzbuzz | |
function fb(val){ | |
let out = ''; | |
if(val%3 !== 0 && val%5 !== 0){ return val + ''; } | |
if(val%3 === 0){ out += 'Fizz'; } | |
if(val%5 === 0){ out += 'Buzz'; } | |
return out; | |
} | |
range(1, 100).map(fb).forEach(v => console.log(v)); | |
// filter | |
let greaterOneHundred = [100,2003,10,203,333,12].filter(function (value){ | |
return value > 100; | |
});// [2003, 203, 333] | |
console.log(greaterOneHundred); | |
let totSum = [0,1,2,3,4].reduce( function (prevVal, currentVal, index, array){ | |
return prevVal + currentVal; | |
}); | |
console.log(totSum); | |
// with optional initial value | |
let totSum2 = [0,1,2,3,4].reduce( function (prevVal, currentVal, index, array){ | |
return prevVal + currentVal; | |
}, 1); | |
console.log(totSum2); | |
// javascript has jagged arrays (not multi-dimensional) | |
// can create Matrix object/class | |
function Matrix(rows, cols){ | |
let jaggedArr = new Array(rows); | |
for(let i=0; i<cols; i++){ jaggedArr[i] = new Array(rows); } | |
return jaggedArr; | |
} | |
console.log(Matrix(3,3)); | |
// Given the array arr, find and return two indices of the array that add up to weight | |
// or return -1 if there is no combination that adds up to weight. | |
function findSum(arr, sum){ | |
for(let i=0; i<arr.length; i++){ | |
for(let j=i+1; j<arr.length; j++){ | |
if(arr[i]+arr[j] == sum){ return [i,j]; } | |
} | |
} | |
return -1; | |
} | |
let nums = [1,2,3,4,5]; | |
let idxs = findSum(nums, 9); | |
console.log(`Numbers that add to 9 are: ${nums[idxs[0]]} and ${nums[idxs[1]]}`); | |
// using a lookup table to decrease time complexity (adds space) | |
// add a match for the value to the table and return when found | |
function findSumFast(arr, sum){ | |
let table = {}; | |
for(let i=0; i<arr.length; i++){ | |
let curr = arr[i]; | |
let diff = sum - curr; | |
if(table[curr] != undefined){ return [i, table[curr]]; } | |
else{ table[diff] = i; } | |
} | |
return -1; | |
} | |
let nums2 = [1,2,3,4,5]; | |
let idxs2 = findSumFast(nums2, 9); | |
console.log(`Numbers that add to 9 are: ${nums2[idxs2[0]]} and ${nums2[idxs2[1]]}`); | |
// common elements in list of arrays | |
function commonElements(arrays){ | |
let table = {}; | |
for(arrIdx in arrays){ | |
// put elements into table with element -> count | |
let arr = arrays[arrIdx]; | |
arr.forEach(element => { | |
if(table[element] === undefined){ table[element] = 1; } | |
else{ table[element]++; } | |
}); | |
} | |
// go through table and look for elements with values == number of arrays | |
let commons = []; | |
for(element in table){ | |
if(table[element] === arrays.length){ commons.push(parseInt(element)); } | |
} | |
return commons; | |
} | |
let commmon1 = commonElements([[1 ,2 ,3 ],[1 ,2 ,3 ,4 ],[1 ,2 ]]); // [ 1, 2 ] | |
console.log(commmon1); | |
// matrix spiral printing | |
let M = [[1,2,3,4,5], | |
[6,7,8,9,10], | |
[11,12,13,14,15], | |
[16,17,18,19,20]]; | |
function spiralOrder(M){ | |
let topRow = 0; | |
let leftCol = 0; | |
let btmRow = M.length - 1; | |
let rightCol = M[0].length - 1; | |
let spiral = []; | |
while(topRow < btmRow && leftCol < rightCol){ | |
// top row of spiral - move top row down when done | |
for(let col=0; col <=rightCol; col++){ spiral.push(M[topRow][col]); } | |
topRow++; | |
// right column of spiral - move right column to left by one when done | |
for(let row=topRow; row<=btmRow; row++){ spiral.push(M[row][rightCol]); } | |
rightCol--; | |
// if top row hasn't moved all the way to the bottom - bottom row of spiral | |
// moves right to left (starts at rightCol) - move bottom row up when done | |
if(topRow <= btmRow){ | |
for(let col=rightCol; col>=0; col--){ spiral.push(M[btmRow][col]); } | |
btmRow--; | |
} | |
// if left column isn't all the way to the right - left column of spiral | |
// moves bottom to top (starts at btmRow) - move column to right when done | |
if(leftCol <= rightCol){ | |
for(let row=btmRow; row>topRow; row--){ spiral.push(M[row][leftCol]); } | |
leftCol++; | |
} | |
} | |
return spiral; | |
} | |
let spiralM = spiralOrder(M); | |
spiralM.forEach(i => console.log(i)); | |
// tic-tac-toe | |
function checkRow(rowArr, letter) { | |
for(x of rowArr){ if(x !== letter){ return false; }} | |
return true; | |
} | |
function checkColumn(gameBoard, col, letter){ | |
for(row in gameBoard){ | |
if(gameBoard[row][col] !== letter){ return false; } | |
} | |
return true; | |
} | |
function ticTacToeWinner(gameBoard, letter){ | |
let rowWin = checkRow(gameBoard[0], letter) || | |
checkRow(gameBoard[1], letter) || | |
checkRow(gameBoard[2], letter); | |
let colWin = checkColumn(gameBoard, 0, letter) || | |
checkColumn(gameBoard, 1, letter) || | |
checkColumn(gameBoard, 2, letter); | |
// check diagonals | |
let diagWin = (gameBoard[0][0] === letter && | |
gameBoard[1][1] === letter && | |
gameBoard[2][2] === letter) || | |
(gameBoard[0][2] === letter && | |
gameBoard[1][1] === letter && | |
gameBoard[2][0] === letter); | |
return rowWin || colWin || diagWin; | |
} | |
// tic-tac-toe board | |
let board = [['O','-','X'],['-','O','-'],['-','X','O']]; | |
console.log(ticTacToeWinner(board, 'X')); // false | |
console.log(ticTacToeWinner(board, 'O')); // true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment