DEV Community

loading...

Discussion on: Daily Challenge #9 - What's Your Number?

Collapse
wolverineks profile image
Kevin Sullivan

codesandbox.io/embed/daily-challen...

const iterative = (input: number[], schema: string = "(###) ###-####") => {
  let result = schema;
  input.forEach(number => (result = result.replace("#", String(number))));

  return result;
};

const recursive = (
  input: number[],
  schema: string = "(###) ###-####",
  result: string = schema
): string => {
  const [head, ...rest] = input;
  if (head === undefined) return result;

  return recursive(rest, schema, result.replace("#", String(head)`));
};

export const createPhoneNumber = {
  iterative,
  recursive
};