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

478
Visualizações
How to implement a division function without the operator, loop or recursion?

I need to convert this function using the functional programming paradigm but I don't know how, I can use reducer or map creating an array but I don't know how to implement it, i can't use divide operator, loop or recursion;

function divide(dividend, divisor) {
  var result = 0;
  while (dividend >= divisor) {
    dividend -= divisor;
    result++;
  }
  return result;
}

console.log(divide(100, 2));

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

0

The way to do it declaratively is with a recursive function....

const divide = (t, b, depth = 0) => t < b ? depth : divide(t-b, b, depth+1);

console.log(`150 / 3 = ${divide(150, 3)}`);
console.log(`24 / 3 = ${divide(24, 3)}`);
console.log(`4 / 3 = ${divide(4, 3)}`);

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm a bit puzzled by the requirements. My understanding is that loops or recursion aren't prohibited in functional programming. Assuming this is an exercise (it has to be) then here's another way to look at it:

To solve a / b you can count how many b you can fit in a. So for example:

10 / 2 -> [2, 2, 2, 2, 2] -> 5

or:

            +2 +2 +2 +2 (map)
10 / 2 -> [2, 4, 6, 8, 10] -> 5
           ^           ^^
          (x)         (pred)

So we can unfold the divisor into a list of sums of itself:

const unfold = (pred, map, x) => {
  const ys = [];
  for (let y = x; pred(y); y = map(y)) ys.push(y);
  return ys;
}

unfold(x => x <= 10, x => x + 2, 2);
//=> [2, 4, 6, 8, 10]

Now we can implement divide with unfold and return the length of the list:

const divide = (a, b) =>
  unfold(x => x <= a, x => x + b, b)
    .length;

divide(10, 2);
//=> 5
about 4 years ago · Juan Pablo Isaza Relatório

0

My Final solution is this:

const adition = (a, b) => a + b;

const subtraction = (a, b) => a - b;

const multiplication = (a, b) => {
    return b >= 0 ? [...Array(b)].reduce((acc) => adition(acc, a), 0) : [...Array(a)].reduce((acc) => adition(acc, b), 0);
};

const division = (a, b) => {
    return a === 0 || b === 0 ? 'Error' : b > 1 ? [...Array(a).keys()].reduce((acc, num) => multiplication(num, b) <= a ? adition(acc, 1) : acc, -1) : a;
};
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