## DEV Community

Brandon McConnell

Posted on

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

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! ☕️🎄🎁

### 🍬   Challenge 1: Candies

``````const candies = (children, candy) => Math.floor(candy / children) * children;
candies(3, 10); // 9
``````

### ⭐️   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
``````

### 🧚   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"]]
``````

### 🎄   Challenge 4: Century From Year

``````const centuryFromYear = num => Math.ceil(num/100);
centuryFromYear(1905); // 20
centuryFromYear(1700); // 17
``````

### 🐑   Challenge 5: Reverse a String

``````const reverseAString = str => str.split('').reverse().join('');
reverseAString('hello'); // "olleh"
``````

### 🤶   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"]
``````

### 🦌   Challenge 7: Count Vowel Consonant

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

### 🎺   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
``````

### 💂‍♀️   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
``````

### 🎁   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
``````

### ❄️   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
``````

### 🤴   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]
``````

### 🧸   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
``````

### 🧦   Challenge 16: Insert Dashes

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

### 👑   Challenge 17: Different Symbols Naive

``````const differentSymbolsNaive = str => [...new Set(str)].length;
differentSymbolsNaive('cabca'); // 3
``````

### 🎅🏻   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]
``````

### 🐫   Challenge 19: Alphabet Subsequence

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

### ✨   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"]
``````

### 🦃   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
``````

### 👼   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]
``````

### ☃️   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
``````