loading...

re: Daily Challenge #238 - One is the Loneliest Number VIEW POST

FULL DISCUSSION
 
function lonelyNum(nu){     
    let num = nu.toString().split('').map((e)=>+e);
    let loneliest  = num[0];
    let loneliest_sum = Infinity;
    num.forEach(function(e,i){
        var sum = 0;// 52667776
        for(var j=Math.max(0,i-e);j<Math.min(i+e+1,num.length);j++)
            if(j!=i)
                sum+=num[j];


        if(loneliest_sum>sum && e < loneliest){
            loneliest = e;
            loneliest_sum = sum;
        }
    });
    return loneliest;
}

console.log(lonelyNum(34315)); // 1
console.log(lonelyNum(23456)); // 2
console.log(lonelyNum(42435)); // 2
code of conduct - report abuse