DEV Community

Dominic Myers
Dominic Myers

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

Free course: The 7-Day JavaScript Challenge

Scrimba are running a week-long JavaScript challenge, so I jumped in.

Day 1

Initial attempt:

function addBorder(array) {const l = Array.from({length: array[0].length + 2 }, () => '\*').join(''); array = array.map((x) => x.padStart(x.length + 1, '\*').padEnd(x.length + 2, '\*')) array.push(l) array.unshift(l)return array;}
Enter fullscreen mode Exit fullscreen mode

After I'd thought about it:

const addBorder = a => ['\*'.repeat(a[0].length), ...a, '\*'.repeat(a[0].length)].map(e => `\*${e}\*`)
Enter fullscreen mode Exit fullscreen mode

Day 2

const addTwoDigits = n => (''+n).split('').reduce((a,c) => ~~a +~~ c)
Enter fullscreen mode Exit fullscreen mode

Day 3

const firstDuplicate = ns => {const s = new Set();for(n of ns){if(s.size === s.add(n).size) return n }return -1}
Enter fullscreen mode Exit fullscreen mode

Day 4

const sumAllPrimes = n => Array.from({length: n-1}, (\_, k) => k+2).reduce((a, c) => a += !'1'.repeat(c).match(/^1?$|^(11+?)\1+$/) ? c : 0)
Enter fullscreen mode Exit fullscreen mode

Day 5

const evenDigitsOnly = n => (''+n).split('').every(n => !(~~n % 2))
Enter fullscreen mode Exit fullscreen mode

Day 6

const makeArrayConsecutive = n => Math.max(...n) - Math.min(...n) - [...new Set(n)].sort().reduce((a, c) => c > Math.min(...n) && c < Math.max(...n) ? a + 1 : a, 0) - 1
Enter fullscreen mode Exit fullscreen mode

Alternatively:

const makeArrayConsecutive = n => ([...new Set(n)].sort()[[...new Set(n)].length - 1] - [...new Set(n)].sort()[0] + 1) - [...new Set(n)].length
Enter fullscreen mode Exit fullscreen mode

Day 7

const properNounCorrection = s => s === s + '' && s.length ? String.fromCharCode(...[...s].map((\_, i) => !i ? s.charCodeAt(i) - (s.charCodeAt(i) >= 97 && s.charCodeAt(i) <= 122 ? 32 : 0) : s.charCodeAt(i) + (s.charCodeAt(i) >= 65 && s.charCodeAt(i) <= 90 ? 32 : 0))) : ''
Enter fullscreen mode Exit fullscreen mode

Think that's all of them now.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay