Skip to content
loading...

re: Daily Challenge #142 - Parts of a Whole VIEW POST

FULL DISCUSSION
 

Small JavaScript answer - trying to think how I would do this with a reduce for fun:

const digits = num => {
    const result = [];
    const parts = String(num).split('').map(n => n | 0);
    for(let i = 0; i < parts.length; i++) {
        for (let j = i + 1; j < parts.length; j++) {
            result.push(parts[i] + parts[j]);
        }
    }
    return result;
};
 

something like this maybe

const prepareArray = nr => String(nr).split('').map(Number);
const printPairs = arr => arr.reduce((acc, val, index) => {
  for(let i = index+1; i < arr.length; i++){
    acc.push(val+arr[i]);
  }
  return acc;
}, []);

console.log(printPairs(prepareArray(12345)))
code of conduct - report abuse