DEV Community

Discussion on: Daily Challenge #50 - Number Neighbor

Collapse
 
andre000 profile image
André Adriano

My take on this challenge with JS

const neighbors = number => {
    const format = n => {
        n = `${n}`.padStart(10, '0');
        return `${`${n}`.substr(0,3)}-${`${n}`.substr(3, 3)}-${`${n}`.substr(6, 4)}`;
    };

    const nArray = [...number].reduce((t, d, i) => {
        const n1 = Number(number) + Math.pow(10, i);
        const n2 = number - Math.pow(10, i);

        t.push(format(n1));
        t.push(format(n2));

        return t;
    }, [])

    return nArray;
}

/**

neighbors("5555555555");

[
  "555-555-5556",
  "555-555-5554",
  "555-555-5565",
  "555-555-5545",
  "555-555-5655",
  "555-555-5455",
  "555-555-6555",
  "555-555-4555",
  "555-556-5555",
  "555-554-5555",
  "555-565-5555",
  "555-545-5555",
  "555-655-5555",
  "555-455-5555",
  "556-555-5555",
  "554-555-5555",
  "565-555-5555",
  "545-555-5555",
  "655-555-5555",
  "455-555-5555"
]

**/