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

273
Visualizações
Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string

I am bit confused with this warning:

Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string

Take the following peace of code:

function createCalculator() {

    let calculator = {
        sum() {
            return this.a + this.b;
        },

        mul() {
            return this.a * this.b;
        },

        read() {
            this.a = +prompt('a?', 0);
            this.b = +prompt('b?', 0);
        }
    };

    calculator.read([1,3,6]);
    console.log( calculator.sum() );
    console.log( calculator.mul() );

}
let calculator;
calculator = createCalculator();

Also I have one warning:

Void function return value is used

I want the follow:

The function createCalculator() returns an object with three methods:

  • read(arr) accepts a table of numbers and saves it in its field object.
  • sum() returns the sum of the table values
  • mul() returns the product of the table values
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The warning is because your object constructor is not returning an object

I have inserted a return function which should make your warning disappear.

function createCalculator() {

  let calculator = {
    sum() {
      return this.a + this.b;
    },

    mul() {
      return this.a * this.b;
    },

    read() {
      this.a = +prompt('a?', 0);
      this.b = +prompt('b?', 0);
    }
  };

  calculator.read(); // Don't bother to send in the [1,3,6] because you are not using it in the function.
  console.log(calculator.sum());
  console.log(calculator.mul());

  return calculator // Do this so that your final statement really has something to receive!

}

let calculator;
calculator = createCalculator();

Perhaps you intended to write this, though?

function createCalculator() {

  return {
    sum() {
      return this.a + this.b;
    },

    mul() {
      return this.a * this.b;
    },

    read() {
      this.a = +prompt('a?', 0);
      this.b = +prompt('b?', 0);
    }
  };


}

const calculator = createCalculator();

calculator.read();
console.log(calculator.sum());
console.log(calculator.mul());

I now see you intended to read a list of values, rather than input them "live"

So how about this:

function createCalculator() {
  return {

    memory: [],

    read(arr) {
      this.memory = arr
    },

    sum() {
      return this.memory.reduce((x, y) => x + y, 0)
    },

    mul() {
      return this.memory.reduce((x, y) => x * y, 1)
    },

  };
}

const calculator = createCalculator();

calculator.read([1, 3, 6]);
console.log(calculator.sum());
console.log(calculator.mul());

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