loading...

re: Daily Challenge #50 - Number Neighbor VIEW POST

FULL DISCUSSION
 

One line JS generate all possible neighbor number for any position.

const allNumberNeighbor = number => [...number]
  .map(Number)
  .flatMap((n, i) => Number(n) && [
    n + 1 <= 9 && Object.values({ ...number, [i]: n + 1 }),
    n - 1 >= 0 && Object.values({ ...number, [i]: n - 1 }),
  ])
  .filter(Boolean)
  .map(n => n.join(''));

allNumberNeighbor('555-555-555');
// return
/*
 [
  '655-555-555',
  '455-555-555',
  '565-555-555',
  '545-555-555',
  '556-555-555',
  '554-555-555',
  '555-655-555',
  '555-455-555',
  '555-565-555',
  '555-545-555',
  '555-556-555',
  '555-554-555',
  '555-555-655',
  '555-555-455',
  '555-555-565',
  '555-555-545',
  '555-555-556',
  '555-555-554',
 ];
*/
code of conduct - report abuse