DEV Community

Cover image for My simplistic entries for Scrimba's 2020 #JavaScriptmas advent calendar
Brandon McConnell
Brandon McConnell

Posted on

My simplistic entries for Scrimba's 2020 #JavaScriptmas advent calendar

↓ Skip to the challenges ↓

Scrimba filled this month with plenty of new JavaScript challenges for us to complete with their #JavaScriptmas tradition, leading up to Christmas day. Here are my highly simplistic solutions to each challenge, using only one-liner JavaScript functions for all non-project-style challenges. For the project-style challenges (Days 8, 15, 23, and an extra bonus challenge Scrimba included between days 23 and 24), I embedded my full solutions using CodePen so you can poke around to see how I solved each one.

Front-end development is my full-time career, but as work tends to be a lot of the same, it's nice to mix it up now and again and tackle some challenges that not only spice up the routine development but also challenge you to learn new methods and techniques. My personal favorite challenges were the Sum Odd Fibonacci Numbers challenge in which I "hijacked" the temporary array being created by the third parameter of the Array.prototype.map() function, and the Max Consecutive Sum challenge which expanded my knowledge of the Array.prototype.fill() function. For some CSS fun, check out the CSS tab of Challenge 8 to see how I created my different dice faces.

Without any further ado — enjoy! ☕️🎄🎁

Table Of Contents

🍬   Challenge 1: Candies

const candies = (children, candy) => Math.floor(candy / children) * children;
candies(3, 10); // 9
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 1    //    ↑ Back to the Table of Contents ↑

⭐️   Challenge 2: Deposit Profit

const depositProfit = (deposit, rate, threshold) => Math.ceil(Math.log(threshold / deposit) / Math.log(1 + (rate / 100)));
depositProfit(100, 20, 170); // 3
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 2    //    ↑ Back to the Table of Contents ↑

🧚   Challenge 3: Chunky Monkey

const chunkyMonkey = (values, size) => values.length <= size ? [values] : [values.slice(0, size), ...chunkyMonkey(values.slice(size), size)];
chunkyMonkey(["a", "b", "c", "d"], 2); // [["a", "b"], ["c", "d"]]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 3    //    ↑ Back to the Table of Contents ↑

🎄   Challenge 4: Century From Year

const centuryFromYear = num => Math.ceil(num/100);
centuryFromYear(1905); // 20
centuryFromYear(1700); // 17
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 4    //    ↑ Back to the Table of Contents ↑

🐑   Challenge 5: Reverse a String

const reverseAString = str => str.split('').reverse().join('');
reverseAString('hello'); // "olleh"
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 5    //    ↑ Back to the Table of Contents ↑

🤶   Challenge 6: Sort by Length

const sortByLength = strs => strs.sort((a,b) => a.length - b.length, 0);
sortByLength(["abc", "", "aaa", "a", "zz"]); // ["", "a", "zz", "abc", "aaa"]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 6    //    ↑ Back to the Table of Contents ↑

🦌   Challenge 7: Count Vowel Consonant

const countVowelConsonant = str => str.split('').reduce((a,b) => a + (/[aAeEiIoOuU]/.test(b) ? 1 : 2), 0);
countVowelConsonant('abcde'); // 8
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 7    //    ↑ Back to the Table of Contents ↑

🔔   Challenge 8: The Rolling Dice


Take a swing at Challenge 8    //    ↑ Back to the Table of Contents ↑

🎺   Challenge 9: Sum Odd Fibonacci Numbers

const sumOddFibonacciNumbers = num => [0,1,1,...Array(num-3).fill()].map((e,i,a) => a[i-2] ? ((a[i] = a[i-2] + a[i-1]) || a[i-2] + a[i-1]) : e).filter(e => e % 2 && e <= num).reduce((a,b) => a + b, 0);
sumOddFibonacciNumbers(10); // 10
sumOddFibonacciNumbers(1000); // 1785
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 9    //    ↑ Back to the Table of Contents ↑

💂‍♀️   Challenge 10: Adjacent Elements Product

const adjacentElementsProduct = nums => Math.max(...nums.map((e,i,a) => a[i-1] ? e * a[i-1] : "").filter(e => e)) || undefined;
adjacentElementsProduct([3, 6, -2, -5, 7, 3]); // 21
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 10    //    ↑ Back to the Table of Contents ↑

🎁   Challenge 11: Avoid Obstacles

const avoidObstacles = nums => Array(Math.max(...nums)).fill().map((e, i) => i + 1).filter(e => !nums.includes(e)).find(e => nums.every(f => f % e !== 0));
avoidObstacles([5, 3, 6, 7, 9]); // 4
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 11    //    ↑ Back to the Table of Contents ↑

❄️   Challenge 12: Valid Time

const validTime = str => str.includes(":") && str.split(":").length === 2 && str.split(":").every(e => !isNaN(e)) && Array(24).fill().map((_,i) => i).includes(parseInt(str.split(":")[0])) && Array(60).fill().map((_,i) => i).includes(parseInt(str.split(":")[1]));
validTime('13:58'); // true
validTime('25:51'); // false
validTime('02:76'); // false
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 12    //    ↑ Back to the Table of Contents ↑

🤴   Challenge 13: Extract Each Kth

const extractEachKth = (nums, index) => nums.filter(e => e % index);
extractEachKth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3); // [1, 2, 4, 5, 7, 8, 10]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 13    //    ↑ Back to the Table of Contents ↑

🧸   Challenge 14: Maximal Adjacent Difference

const arrayMaximalAdjacentDifference = nums => Math.max(...nums.map((e,i,a) => a[i-1] - e).filter(e => e).map(e => Math.abs(e))) || undefined;
arrayMaximalAdjacentDifference([2, 4, 1, 0]); // 3
arrayMaximalAdjacentDifference([2, 9, 1, 0]); // 8
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 14    //    ↑ Back to the Table of Contents ↑

🕊   Challenge 15: Carousel


Take a swing at Challenge 15    //    ↑ Back to the Table of Contents ↑

🧦   Challenge 16: Insert Dashes

const insertDashes = arr => arr.split(" ").map(e => [...e].join("-")).join(" ");
insertDashes("aba caba"); // "a-b-a c-a-b-a"
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 16    //    ↑ Back to the Table of Contents ↑

👑   Challenge 17: Different Symbols Naive

const differentSymbolsNaive = str => [...new Set(str)].length;
differentSymbolsNaive('cabca'); // 3
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 17    //    ↑ Back to the Table of Contents ↑

🎅🏻   Challenge 18: Array Previous Less

const arrayPreviousLess = nums => nums.map((e,i,a) => a[i-1] < e ? a[i-1] : -1);
arrayPreviousLess([3, 5, 2, 4, 5]); // [-1, 3, -1, 2, 4]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 18    //    ↑ Back to the Table of Contents ↑

🐫   Challenge 19: Alphabet Subsequence

const alphabetSubsequence = str => str === [...new Set(str)].sort().join('');
alphabetSubsequence('effg'); // false
alphabetSubsequence('cdce'); // false
alphabetSubsequence('ace');  // true
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 19    //    ↑ Back to the Table of Contents ↑

✨   Challenge 20: Domain Type

const domainType = (domains, domainTypes = { com: "commercial", net: "network", org: "organization", info: "information" }) => domains.map(e => e.split('.')[e.split('.').length-1]).map(e => domainTypes[e]);
domainType(["en.wiki.org", "codefights.com", "happy.net", "code.info"]); // ["organization", "commercial", "network", "information"]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 20    //    ↑ Back to the Table of Contents ↑

🦃   Challenge 21: Sum of Two

const sumOfTwo = (nums1, nums2, value) => nums1.map(e => nums2.map(f => e + f)).flat().some(e => e === value);
sumOfTwo([1, 2, 3], [10, 20, 30, 40], 42); // true
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 21    //    ↑ Back to the Table of Contents ↑

👼   Challenge 22: Extract Matrix Column

const extractMatrixColumn = (matrix, column) => matrix.map(e => e[column]);
extractMatrixColumn([[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]], 2); // [1, 0, 3]
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 22    //    ↑ Back to the Table of Contents ↑

🌠   Challenge 23: Social Media Input


Take a swing at Challenge 23    //    ↑ Back to the Table of Contents ↑

💯   Challenge BONUS: Test Your Agility


Take a swing at this Challenge BONUS    //    ↑ Back to the Table of Contents ↑

☃️   Challenge 24: Max Consecutive Sum

const arrayMaxConsecutiveSum = (nums, elementCount) => Array(nums.length - elementCount + 1).fill().map((_,i) => nums.slice(i, i + elementCount)).map(e => e.reduce((a,b) => a + b, 0)).reduce((a,b) => a > b ? a : b);
arrayMaxConsecutiveSum([2, 3, 5, 1, 6], 2); // 8
Enter fullscreen mode Exit fullscreen mode

Take a swing at Challenge 24    //    ↑ Back to the Table of Contents ↑

Top comments (0)