DEV Community

Dominic Myers
Dominic Myers

Posted on • Originally published at drmsite.blogspot.com on

The 10-Day JavaScript Challenge

Yay! There's another one started, such fun!

I'll add to this is I go along.

Day 1

const add = (...a) => a.reduce((t, v) => t + v)
Enter fullscreen mode Exit fullscreen mode

Day 2

const allLongestStrings = a => a.filter(i => i.length === Math.max(...(a.map(a => a.length))))
Enter fullscreen mode Exit fullscreen mode

This is not overly efficient, but it works. Then I got to remembering about default values and altered it to this:

const allLongestStrings = (a, i = Math.max(...(a.map(a => a.length)))) => a.filter(e => e.length === i)
Enter fullscreen mode Exit fullscreen mode

Longer, but we only check for the longest string the once, rather than over each iteration.

Day 3

const allLongestStrings = (a, i = Math.max(...(a.map(a => a.length)))) => a.filter(e => e.length === i)
Enter fullscreen mode Exit fullscreen mode

Day 4

const arrayReplace = (arr, o, n) => arr.map(e => e === o ? n : e)
Enter fullscreen mode Exit fullscreen mode

Day 5

const caseInsensitivePalindrome = (s) => s.toLowerCase().replace(/[\W\_]/g, '') === s.toLowerCase().replace(/[\W\_]/g, '').split('').reverse().join('')
Enter fullscreen mode Exit fullscreen mode

I like this way because it deals with "A man, a plan, a canal, Panama!". But I really like this one (not mine):

const caseInsensitivePalindrome = s => [...s = s.toLowerCase()].reverse().join`` === s;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)