function numToArray(num) { let result = []; let n = num; while(Math.log10(n) > 0) { let i = n % 10; result.unshift(i) n = Math.floor(n / 10); } return result; } function lonliest(num) { let parts = numToArray(num); let min = Number.MAX_SAFE_INTEGER; let minFriends = Number.MAX_SAFE_INTEGER; let minNum = Number.MAX_SAFE_INTEGER; let scores = parts.map((n, i, arr) => { let lstart = n >= i ? 0 : i - n; let leftArr = arr.slice(lstart, i); let left = leftArr.reduce((acc, v) => acc + v, 0); let rightArr = arr.slice(i+1, i+n+1); let right = rightArr.reduce((acc, v) => acc + v, 0); let friendCount = leftArr.length + rightArr.length let score = left+right; if(score < min) { min = score; minNum = n; minFriends = friendCount; } else if(min == score) { if(minFriends > friendCount) { minFriends = friendCount; minNum = n; } } return score; }); console.log(scores); return minNum; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Discussion on: Daily Challenge #238 - One is the Loneliest Number
For further actions, you may consider blocking this person and/or reporting abuse