Skip to content

Instantly share code, notes, and snippets.

View dragonza's full-sized avatar

Alex Vuong dragonza

View GitHub Profile
@dragonza
dragonza / chunk.js
Last active May 12, 2019 17:25
Chunk array
function chunk(array, size) {
if (!array) return [];
const firstChunk = array.slice(0, size); // create the first chunk of the given array
if (!firstChunk.length) {
return array; // this is the base case to terminal the recursive
}
return [firstChunk].concat(chunk(array.slice(size, array.length), size));
}
/*
@dragonza
dragonza / anagrams.js
Created March 4, 2018 10:03
Anagrams
function anagrams(stringA, stringB) {
const aCharMap = charMap(stringA.replace(/[^\w]/g, '').toLowerCase());
const bCharMap = charMap(stringB.replace(/[^\w]/g, '').toLowerCase());
// return false immediately when the length of two char Map is not the same
if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
return false;
}
// comparison
@dragonza
dragonza / anagrams.js
Created March 4, 2018 10:18
Anagrams
function anagrams(stringA, stringB) {
return cleanString(stringA) === cleanString(stringB);
}
function cleanString(str) {
return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
}
@dragonza
dragonza / cap.js
Created March 24, 2018 18:07
Sentence Capitalization
function capitalize(str) {
const arr = str.split(' ');
return arr.map(word => {
return word[0].toUpperCase() + word.slice(1);
}).join(' ');
}
@dragonza
dragonza / cap.js
Created March 24, 2018 18:12
Sentence Capitalization
function capitalize(str) {
let result = '';
for (let word of str.split(' ')) {
result += word[0].toUpperCase() + word.slice(1) + ' ';
}
return result.trim();
}
@dragonza
dragonza / cap.js
Created March 24, 2018 18:17
Sentence Capitalization
function capitalize(str) {
let result = str[0].toUpperCase();
for (let i = 1; i < str.length; i++) {
if (str[i - 1] === ' ') {
result += str[i].toUpperCase();
} else {
result += str[i];
}
}