This file contains 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
var getCurrentRow = function(board, row, col) { | |
return board[row]; | |
}; | |
var getCurrentCol = function(board, row, col) { | |
var flatCol = []; | |
board.forEach(function(row) { | |
flatCol.push(row[col]); | |
return row; | |
}); |
This file contains 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
var sortedIndex = function (array, value) { | |
var low = 0, | |
high = array.length; | |
while (low < high) { | |
var mid = (low + high) >>> 1; | |
if (array[mid] < value) low = mid + 1; | |
else high = mid; | |
} | |
return low; | |
}; |
This file contains 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
function Person() { | |
if(typeof Person.instance === 'object') | |
return Person.instance; | |
Person.instance = this; | |
return this; | |
} |
This file contains 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
const countSubstringOccurrances = function(string, query) { | |
let count = 0; | |
while(string.includes(query)) { | |
count++; | |
let index = string.indexOf(query); | |
string = string.slice(-(string.length-1-string.indexOf(query))); | |
} | |
return count; | |
}; | |
This file contains 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
function diagonal(matrix) { | |
let row = matrix.length - 1; | |
let col = matrix[row] ? matrix[row].length - 1 : null; | |
let matrixDiagonal = [matrix[row][col]]; | |
let count = 0; | |
while(row !== 0 || col !== 0) { | |
if(!matrix[row+1] || !matrix[row+1][col-1]) { | |
row -= count; | |
col += count; | |
count = 0; |
This file contains 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
const assert = function (condition, message) { | |
console.assert.apply(console, arguments); | |
typeof message === 'string' && condition ? | |
console.log('✔ ' + message) : | |
console.log('✖ ' + message); | |
}; | |
const test = () => { | |
//assert(condition, message); example: | |
//assert(2 > 0, 'Two should be greater than zero!'); | |
}; |
This file contains 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
String.prototype.hashCode = function(max){ | |
var hash = 0; | |
if (!this.length) return hash; | |
for (i = 0; i < this.length; i++) { | |
char = this.charCodeAt(i); | |
hash = ((hash<<5)-hash)+char; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return Math.abs(max?hash%max:hash); | |
}; |
This file contains 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
const hasLessThanTwoEdits = (string1, string2) => { | |
if(string1.length - string2.length > 1) { | |
return false; | |
} | |
let changes = 0; | |
(string1.length >= string2.length ? string1 : string2).split('').forEach((char, i) => { string1[i] !== string2[i] ? changes++ : null }); | |
return changes < 2; | |
}; |
This file contains 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
const isPalindrome = (string1, string2) => { | |
if(string1.length !== string2.length) { | |
return false; | |
} | |
let stringChars = string1.split('').reduce((acc, char) => { | |
acc[char] ? acc[char]++ : acc[char] = 1; | |
return acc; | |
}, {}); | |
let string2chars = string2.split('').reduce((acc, char) => { | |
acc[char] ? acc[char]++ : acc[char] = 1; |
This file contains 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
const urlify = (string) => { | |
return string.split('').reduce((acc, char, i, array) => { | |
if(char === ' ') { | |
return acc += array[i+1] === ' ' || !array[i+1] ? '' : '%20'; | |
} else { | |
return acc += char; | |
} | |
}, ''); | |
}; |