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
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