Skip to content

Instantly share code, notes, and snippets.

@danedavid
danedavid / stringPermutation.js
Created May 2, 2018 16:59
Permutations of a String in JavaScript
// returns an array containing all permutations of a given string
const permute = str => {
let permutations = [];
if ( str.length <= 1 ) {
return [str];
}
for ( let i = 0; i < str.length; i++ ) {
let char = str[i];
@danedavid
danedavid / balancedParanthesis.js
Created April 4, 2018 05:51
Function to check for balanced parenthesis in any string using Array.prototype.reduce(). Inspiration: https://medium.freecodecamp.org/check-out-these-useful-ecmascript-2015-es6-tips-and-tricks-6db105590377
const isBalanced = str => {
const arr = str.split('');
const res = arr.reduce((acc, item) => {
if ( acc.slice(-1)[0] === '(' ) {
if ( item === ')' ) {
acc.pop();
return acc;
}
}
if ( item === '(' || item === ')' ) {