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; }
After I'd thought about it:
const addBorder = a => ['*'.repeat(a[0].length), ...a, '*'.repeat(a[0].length)].map(e => `*${e}*`)
Day 2
const addTwoDigits = n => (''+n).split('').reduce((a,c) => ~~a + ~~c)
Day 3
const firstDuplicate = ns => { const s = new Set(); for(n of ns){ if(s.size === s.add(n).size) return n } return -1 }
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)
Day 5
const evenDigitsOnly = n => (''+n).split('').every(n => !(~~n % 2))
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
Alternatively:
const makeArrayConsecutive = n => ([...new Set(n)].sort()[[...new Set(n)].length - 1] - [...new Set(n)].sort()[0] + 1) - [...new Set(n)].length
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))) : ''
Think that's all of them now.
No comments:
Post a Comment