Skip to content

Instantly share code, notes, and snippets.

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;
};
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;
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!');
};
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;
};
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;
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;
}
}, '');
};
const isPermutaiton = (s1, s2) => {
let s1letterCounts = s1.split('').reduce((acc, letter) => {
acc[letter] ? acc[letter] += 1 : acc[letter] = 1;
return acc;
}, {});
let s2letterCounts = s2.split('').reduce((acc, letter) => {
acc[letter] ? acc[letter]++ : acc[letter] = 1;
return acc;
}, {});
let s1letters = Object.keys(s1letterCounts);
const isUniqueString = (string) => {
for (let i = string.length - 1; i >= 0; i--) {
if(string.indexOf(string[i]) !== i) {
return false;
}
}
return true;
};
var string = 'hello';
isUniqueString(string);
let num1 = 1;
let num2 = 2;
let num3 = 4;
let num4 = 8;
//assign number to variable
let biwiseORassignment = num2;
//perform a bitwise OR operation with the value of the variable
//and that of a new variable:
biwiseORassignment |= num3;
const generateMaze = (length, width) => {
const directionsVal = { N: 1, S: 2, E: 4, W: 8 };
const generateBoard = (rows, columns) => {
let board = [];
for(var i = 0; i < rows; i++) {
board.push([]);
for(let j = 0; j < columns; j++) {
board[i].push(0);
}