Skip to content

Instantly share code, notes, and snippets.

View dragonza's full-sized avatar

Alex Vuong dragonza

View GitHub Profile
@dragonza
dragonza / maxChar.js
Created February 12, 2018 17:53
Find commonly used character
return Object.keys(obj).reduce((prev, next) => obj[a] >= obj[b] ? a : b);
Object.keys(obj).forEach(char => {
if (obj[char] > max) {
maxChar = char;
max = obj[char];
}
});
@dragonza
dragonza / characterMap.js
Last active February 12, 2018 17:38
Create character map
const obj = {};
str.split('').forEach(char => obj[char] + 1 || 1); // remember to turn the given string into array first as forEach is Array prototype
@dragonza
dragonza / characterMap.js
Created February 12, 2018 17:34
Create character map
const obj = {};
for (let char of str) {
obj[char] = obj[char] + 1 || 1;
}
@dragonza
dragonza / maxChar.js
Last active February 12, 2018 17:32
Find the most commonly used character in string
function maxChar(str) {
const charMap = {};
let max = 0;
let maxChar = '';
// create character map
for (let char of str) {
if (charMap[char]) {
// increment the character's value if the character existed in the map
charMap[char]++;
@dragonza
dragonza / reverseInt.js
Last active February 4, 2018 17:06
Reverse an integer
function reverseInt(n) {
const reversed = n.toString().split('').reverse().join(''); // turn a number into a string, then turn it into an array to reverse.
return Math.sign(n) * parseInt(reversed); // Math.sign will return -1 as for negative number, 1 as for position number, 0 as for zero.
}
@dragonza
dragonza / palindrome.js
Created January 28, 2018 16:28
Palindrome
function palindrome(str) {
for (var i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 -i]) {
return false;
}
}
return true;
}
@dragonza
dragonza / palindrome.js
Last active January 28, 2018 16:13
Palindrome
function palindrome(str) {
const arr = str.split(''); // turn a string in to an array of characters
return arr.every((char, i) => char === str[str.length - 1 - i]); // return true if all elements in the array pass the test implemented by the provided function.
}
@dragonza
dragonza / palindrome.js
Created January 28, 2018 15:42
Palindromes
function palindrome(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
@dragonza
dragonza / reverseString.js
Created January 25, 2018 06:34
String reversal with for loop
function reverse(str) {
let reversed = '';
for (var i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}