Last active
November 26, 2018 09:23
-
-
Save manojd929/1a7c4837d2f1016b9e3465039a6ed31b to your computer and use it in GitHub Desktop.
JS - Cardio Simple Cool Problems from Traversy Media
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
// Anagram Check | |
const anagram = (str1, str2) => helper(str1) === helper(str2); | |
const helper = (str) => str.toLowerCase().split('').sort().join(''); | |
console.log('Anagram: ', anagram('Dormitory', 'DirtyRoom')); | |
const getChunkArray = (arr, len) => { | |
const chunkArray = []; | |
let i = 0; | |
while (i < arr.length) { | |
chunkArray.push(arr.slice(i, i + len)); | |
i = i + len; | |
} | |
return chunkArray; | |
}; | |
console.log('Chunked Array: ', getChunkArray([1, 2, 3, 4, 5, 6, 7], 5)); | |
const flattenArray = (chunkedArrays) => { | |
//return chunkedArray.reduce((a, b) => a.concat(b)); | |
// return [].concat.apply([], chunkedArrays) | |
return [].concat(...chunkedArrays); | |
}; | |
console.log('Flatten Array ', flattenArray([ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ])); | |
const longestWord = (sentence) => { | |
const wordArr = sentence.toLowerCase().match(/[a-z0-9]+/g); | |
const sortedArr = wordArr.sort((a, b) => b.length - a.length); | |
const longestWordArr = sortedArr.filter((word) => word.length >= sortedArr[0].length ); | |
if (longestWordArr.length > 1) { | |
return longestWordArr; | |
} else { | |
return longestWordArr[0]; | |
} | |
}; | |
console.log('Longest Word: ', longestWord('Hello there, How are you doing ?')); | |
const letterChanges = (str) => { | |
let newStr = str.toLowerCase().replace(/[a-z]/gi, char => { | |
if (char === 'z' || char === 'Z') { | |
return 'a'; | |
} else { | |
return String.fromCharCode(char.charCodeAt() + 1); | |
} | |
}); | |
newStr = newStr.replace(/a|e|i|o|i/gi, vowel => vowel.toUpperCase()); | |
return newStr; | |
} | |
console.log('Letter and Vowel capitalization: ', letterChanges('Hello There')); | |
const reverseString = (str) => { | |
return str | |
.split('') | |
.reverse() | |
.join(''); | |
// let newStr = ''; | |
// for (i = 0; i < str.length; i++) { | |
// newStr = str[i] + newStr; | |
// } | |
// return newStr; | |
} | |
console.log('Reverse String ', reverseString('olleH World')); | |
const isPalindrome = (str) => str.toLowerCase() === str.toLowerCase().split('').reverse().join('') | |
console.log('Palindrome: ', isPalindrome('Malayalam')); | |
const reverseInt = (number) => { | |
const reverse = number.toString().split('').reverse().join(''); | |
return parseInt(reverse) * Math.sign(number); | |
} | |
console.log('Reverse Int: ', reverseInt(-105)); | |
const capitalizeFirstLetters = (str) => { | |
// return newStr = str.replace(/\b[a-z]/gi, (char) => char.toUpperCase()); | |
const strArr = str.toLowerCase().split(' '); | |
for(let i = 0; i < strArr.length; i = i + 1) { | |
strArr[i] = strArr[i].substring(0, 1).toUpperCase() + strArr[i].substring(1); | |
} | |
return strArr.join(' '); | |
} | |
console.log('Capitalize First letters: ', capitalizeFirstLetters('js love yo')); | |
const maxCharacter = (str) => { | |
const characterMap = {}; | |
for (const c of str) { | |
if (!characterMap[c]) { | |
characterMap[c] = 1; | |
} else { | |
characterMap[c]++; | |
} | |
} | |
let maxNum = 0; | |
let maxChar = ''; | |
for (let key in characterMap) { | |
if (characterMap[key] > maxNum) { | |
maxNum = characterMap[key]; | |
maxChar = key; | |
} | |
} | |
return maxChar; | |
} | |
console.log('Max Character: ', maxCharacter('BananaTerracottaPie')); | |
// console.log('FizzBuzz: ', fizzBuzz()); | |
function fizzBuzz() { | |
for (let i = 1; i <= 100; i++) { | |
if (i % 15 === 0) { | |
console.log('FizzBuzz'); | |
} else if (i % 5 === 0) { | |
console.log('Buzz'); | |
} if (i % 3 === 0) { | |
console.log('Fizz'); | |
} else { | |
console.log(i); | |
} | |
} | |
return 'Done'; | |
} | |
// CHALLENGE 1: ADD ALL NUMBERS | |
// Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS | |
// ex. addAll(2,5,6,7) === 20 | |
// Solution 1: ES5 arguments & for loop | |
function addAll() { | |
var args = Array.prototype.slice.call(arguments); | |
var total = 0; | |
for (let i = 0; i < args.length; i++) { | |
total += args[i]; | |
} | |
return total; | |
} | |
// Solution 2: ...rest & reduce/forEach | |
function addAll(...numbers) { | |
return numbers.reduce((acc, cur) => acc + cur); | |
} | |
// CHALLENGE 2: SUM ALL PRIMES | |
// Pass in a number to loop up to and add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself | |
// ex. sumAllPrimes(10) == 17 | |
function sumAllPrimes(num) { | |
let total = 0; | |
function checkForPrime(i) { | |
for (let j = 2; j < i; j++) { | |
if (i % j === 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
for (let i = 2; i <= num; i++) { | |
if (checkForPrime(i)) { | |
total += i; | |
} | |
} | |
return total; | |
} | |
// CHALLENGE 3: SEEK & DESTROY | |
// Remove from the array whatever is in the following arguments. Return the leftover values in an array | |
// ex. seekAndDestroy([2, 3, 4, 6, 6, 'hello'], 2, 6) == [3, 4, 'hello'] | |
// Solution 1: arguments, indexOf, filter | |
function seekAndDestroy(arr) { | |
const args = Array.from(arguments); | |
function filterArr(arr) { | |
// Return true if NOT in array | |
return args.indexOf(arr) === -1; | |
} | |
return arr.filter(filterArr); | |
} | |
// Solution 2: ...rest, filter & includes | |
function seekAndDestroy(arr, ...rest) { | |
return arr.filter(val => !rest.includes(val)); | |
} | |
// CHALLENGE 4: SORT BY HEIGHT | |
// Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. | |
// ex. | |
// a = [-1, 150, 190, 170, -1, -1, 160, 180] | |
// sortByHeight(a) == [-1, 150, 160, 170, -1, -1, 180, 190] | |
function sortByHeight(a) { | |
const arr1 = []; | |
const arr2 = []; | |
a.forEach((val, i) => (val === -1 ? arr1.push(i) : arr2.push(val))); | |
const sortArr = arr2.sort((a, b) => a - b); | |
arr1.forEach((val, i) => sortArr.splice(arr1[i], 0, -1)); | |
return sortArr; | |
} | |
// CHALLENGE 5: MISSING LETTERS | |
// Find the missing letter in the passed letter range and return it. If all letters are present, return undefined | |
// ex. | |
// missingLetters("abce") == "d" | |
// missingLetters("abcdefghjklmno") == "i" | |
// missingLetters("abcdefghijklmnopqrstuvwxyz") == undefined | |
function missingLetters(str) { | |
let compare = str.charCodeAt(0); | |
let missing; | |
str.split('').map((char, i) => { | |
if (str.charCodeAt(i) == compare) { | |
++compare; | |
} else { | |
missing = String.fromCharCode(compare); | |
} | |
}); | |
return missing; | |
} | |
// CHALLENGE 6: EVEN & ODD SUMS | |
// Take in an array and return an array of the sums of even and odd numbers | |
// ex. | |
// evenOddSums([50, 60, 60, 45, 71]) == [170, 116] | |
function evenOddSums(arr) { | |
let evenSum = 0; | |
let oddSum = 0; | |
arr.forEach(num => (num % 2 === 0 ? (evenSum += num) : (oddSum += num))); | |
return [evenSum, oddSum]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment