Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here are my solutions to the FCC Basic JavaScript Algorithms. Happy to report all algorithms can be
- written in one (albeit sometimes long) line.
- //================================================================================================//
- Reverse a String:
- const reverseString = (str) => str.split('').reverse().join('');
- //================================================================================================//
- Factorialize a Number
- const factorialize = (num) => num > 1 ? num * factorialize(num-1) : 1;
- //================================================================================================//
- Check for Palindromes
- const palindrome = (str) => str.replace(/\W|_/g, "").toLowerCase() == str.replace(/\W|_/g, "").toLowerCase().split("").reverse().join("");
- better:
- function palindrome(str) {
- let cleanStr = str.replace(/\W|_/g, "").toLowerCase();
- return cleanStr === cleanStr.split("").reverse().join("");
- }
- //================================================================================================//
- Find the Longest Word in a String
- const findLongestWord = (str) => Math.max(...str.split(" ").map(a => a.length));
- const findLongestWord = (str) => str.split(' ').reduce((a,b)=> b.length > a ? b.length : a, 0);
- //================================================================================================//
- Title Case a Sentence
- const titleCase = (str) => str.split(' ').map(v => v[0].toUpperCase() + v.slice(1).toLowerCase()).join(' ');
- //================================================================================================//
- Return Largest Numbers in an Array
- const largestOfFour = (a) => a.map(b => Math.max(...b));
- //================================================================================================//
- Confirm the Ending
- const confirmEnding = (str, target) => str.slice(-target.length) == target;
- //================================================================================================//
- Repeat a string repeat a string
- const repeatStringNumTimes = (str, num) => num > 0 ? str.repeat(num) : "";
- //================================================================================================//
- Truncate a string
- //quick and dirty:
- const truncateString = (str, num) => num >= str.length ? str : num<=3 ? str.slice(0,num)+"..." : str.slice(0, num-3) + "...";
- //longer and easier to read:
- function truncateString(str, num) {
- if (num >= str.length){
- return str;
- } else if (num <= 3){
- return str.slice(0, num) + "...";
- } else {
- return str.slice(0,num-3) + "...";
- }
- }
- //================================================================================================//
- Chunky Monkey
- clever:
- const chunkArrayInGroups = (arr, size) => arr.reduce((a,b,i,arr) => i%size == 0 ? a.concat([arr.slice(i,i+size)]) : a, []);
- classic:
- function chunkArrayInGroups(arr, size) {
- var result = [];
- for (var i=0; i<arr.length; i+=size){
- result.push(arr.slice(i, i+size));
- }
- return result;
- }
- //================================================================================================//
- Slasher Flick
- const slasher = (arr, howMany) => arr.slice(howMany);
- //================================================================================================//
- Mutations
- const mutation = (arr) => arr[1].toLowerCase().split('').every(v => arr[0].toLowerCase().includes(v));
- //================================================================================================//
- Falsy Bouncer
- const bouncer = (arr) => arr.filter(Boolean);
- const bouncer = (arr) => arr.filter(v => v);
- //================================================================================================//
- Seek and Destroy
- const destroyer = (arr,...args) => arr.filter(n => args.includes(n));
- //================================================================================================//
- Where do I belong?
- const getIndexToIns = (arr, num) => arr.filter(a => a < num).length;
- //================================================================================================//
- Caesars Cipher
- const rot13 = (str) => str.replace(/[A-Z]/g, l => String.fromCharCode(l.charCodeAt()%26 + 65));
- function rot13(str) {
- return str.split('').map(l => {
- let i = l.charCodeAt(0);
- return (i < 65 || i > 90) ? l : String.fromCharCode(i%26+65);
- }).join('');
- }
- //================================================================================================//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement