## DEV Community is a community of 865,621 amazing developers

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 Matt Ellen • Edited on
``````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;
}
``````