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

186
Visualizações
Function will not return an integer when multiplying two variables with numerical values (JS)

I am having issues getting my function to return product of two variables with numerical values.

The directions are: You want to write a function that takes in the name of a stock as a string and returns for you the total amount of money you have invested in that stock. e.g. if your function is given ‘AMZN’ it should return 4000.

This is my function, I keep returning NaN when I should be returning 4000.

let investments = [
  {stock: "AMZN", price: 400, numOfShares: 10},
  {stock: "AAPL", price: 300, numOfShares: 5},
  {stock: "BIDU", price: 250, numOfShares: 4}
  ];

calculateInvestedAmt = (stock) => {
  const totalAmt = investments.price * investments.numOfShares;
  return totalAmt; 
};

calculateInvestedAmt("AMZN");
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

investments.price and investments.numOfShares is undefined. That's why it is producing NaN, because you are multiplying two undefined values. You should first iterate over investments to find the stock you want.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to use find method

const getStockPrice = (name) => {
    const stock = investments.find(s => s.stock === name);

    if (!stock) return undefined; // can't find stock

    returb stock.price * stock.numOfShares;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

let investments = [
  {stock: "AMZN", price: 400, numOfShares: 10},
  {stock: "AMZN", price: 400, numOfShares: 10},
  {stock: "AAPL", price: 300, numOfShares: 5},
  {stock: "BIDU", price: 250, numOfShares: 4}
  ];

calculateInvestedAmt = (stockName) => {

  var totalAmt;
  
  var investedItem = investments.filter(invst => invst.stock === stockName);
  
  investedItem.map(item => { 
     totalAmt = item.price * item.numOfShares;
  });
  return totalAmt;
  
};

console.log(calculateInvestedAmt("AMZN"));

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