Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda