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

216
Visualizações
How to get rid of the complexity in the algorithm?

Exercise: Write a multiple (a, b) function that multiplies the number a by the number b without using the "*" operator or the Math.imul method.

 multiple(1, 1) // 1
 multiple(1, 2) // 2
 multiple(0, 0) // 0

Code:

export default function multiple(a, b) {
  if (a === Infinity && b === 0 || a === -Infinity && b === 0 || a === 0 && b === Infinity || a === 0 && b === -Infinity) {
    return NaN;
  }

  if (b === Infinity) {
    if (a < 0) {
      return -Infinity;
    }
    return Infinity;
  } if (b === -Infinity) {
    if (a < 0) {
      return Infinity;
    }
    return -Infinity;
  }

  if (b === 1 || b === -1) {
    if (a < 0 && b < 0 || a > 0 && b < 0) {
      return -a;
    }
    return a;
  }

  if (b < 0) {
    return -a + multiple(a, b + 1);
  }
  return a + multiple(a, b - 1);
}

Ok, I write this code and it passes the tests. But eslint complains about over complexity function: Function 'multiple' has a complexity of 15. Maximum allowed is 10. eslint (complexity)

How to reduce complexity my function?

UPDATE

const multiple = (a, b) => a / (1 / b);

Yes its really worked, but if look on my code, where I can short a repeated operations, I looks blind, but I want to understand it.

UPDATE 2

Solution must goes all tests:

const random = () => Math.floor(Math.random() * 100) * (Math.random() < 0.5 ? -1 : 1)

const cases = [
    [0, 1],
    [1, 0],
    [1, 1],
    [1, 2],
    [0, 0],
    [5, 5],
    [5, -5],
    [290, -41],
    [-5, 5],
    [-5, -5],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [10, -Infinity],
    [10, Infinity],
    [-10, Infinity],
    [-10, -Infinity],
    [Infinity, 10],
    [-Infinity, -10],
    [Infinity, -10],
    [-Infinity, 10],
    [0, Infinity],
    [0, -Infinity],
    [Infinity, 0],
    [-Infinity, 0]
]

cases.forEach(([a, b]) => {
    console.log(`\na:${a} b:${b}`)
    console.log('my:', multiple(a, b), 'Fact:', a * b)
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can reduce the number of tests where you test the sign. You could instead translate the call with a negative number to a call with a positive number and negate the result.

Here is a more compact version.

NB: I assume the spirit of the exercise was to not use / either, as otherwise it is trivial to use the fact that a*b === a/(1/b)

function multiple(a, b) {
  return a < b ? multiple(b, a)
       : b < 0 ? -multiple(a, -b)
       : a === Infinity ? (b ? a : NaN)
       : b === Infinity || !b ? b
       : a + multiple(a, b - 1);
}

// Tests:

const random = () => Math.floor(Math.random() * 200) - 100;

const cases = [
    [0, 1],
    [1, 0],
    [1, 1],
    [1, 2],
    [0, 0],
    [5, 5],
    [5, -5],
    [290, -41],
    [-5, 5],
    [-5, -5],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [random(), random()],
    [10, -Infinity],
    [10, Infinity],
    [-10, Infinity],
    [-10, -Infinity],
    [Infinity, 10],
    [-Infinity, -10],
    [Infinity, -10],
    [-Infinity, 10],
    [0, Infinity],
    [0, -Infinity],
    [Infinity, 0],
    [-Infinity, 0]
]

cases.forEach(([a, b]) => {
    let result = multiple(a, b);
    if (!Object.is(result, a*b)) {
        console.log(`\na:${a} b:${b} my result: ${result}, expected: ${a*b}`);
    }
})
console.log("all done");

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use the identity |a*b|=exp(ln(|a|) + ln(|b|) or 0 iff a==0 or b==0.

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