Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

384
Visualizações
How to implement function pipe using JavaScript, and throw errors?
  1. I have to implement function pipe.
  2. The function is supposed to accept value and a sequence of functions.
  3. Each function should operate on the provided value and pass the output to the other function in a sequence.
  4. If the provided argument is not a function, pipe should immediately throw an error and stop the execution.
  5. Use function isFunction and if it gives false, throw an error.

This is what I have so far:

function isFunction(functionToCheck) {
    return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}

////////////////// Implementing pipe function ////////////////////
const pipe = (value, ...funcs) => {
  try {
    const result = funcs.reduce(function (acc, currFunc) {
      if (isFunction(currFunc) === false)
        throw new ('Provided argument at position 2 is not a function!');

      return currFunc(acc);
    }, value);

    return result;
  } catch (err) {
    return err.message;
  }
};
///////////////////////////////////////////////////////////////

const replaceUnderscoreWithSpace = (value) => value.replace(/_/g, ' ');
const capitalize = (value) =>
    value
        .split(' ')
        .map((val) => val.charAt(0).toUpperCase() + val.slice(1))
        .join(' ');
const appendGreeting = (value) => `Hello, ${value}!`;

const error = pipe('john_doe', replaceUnderscoreWithSpace, capitalize, '');

alert(error); // Provided argument at position 2 is not a function!

const result = pipe('john_doe', replaceUnderscoreWithSpace, capitalize, appendGreeting);

alert(result); // Hello, John Doe!

I couldn’t find the solution. Probably somebody could help me. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use Array#reduce to use each function and pass the value as the accumulator, you can also use instanceofFunction to make sure that each argument is a function.

const isFunction = (func) => func instanceof Function;

const pipe = (value, ...funcs) => {
  return funcs.reduce((acc, func, idx) => {
    if (!isFunction(func)) {
      throw new Error(
        `Provided argument at position ${idx} is not a function!`
      );
    }
    return func(acc);
  }, value);
};

const split = (x) => x.split("");
const reverse = (x) => x.reverse();
const join = (x) => x.join("");

const test = pipe("a man a plan a canal, panama", split, reverse, join);

console.log(test);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda