Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
La función no devolverá un número entero al multiplicar dos variables con valores numéricos (JS)

Tengo problemas para que mi función devuelva el producto de dos variables con valores numéricos.

Las instrucciones son: desea escribir una función que tome el nombre de una acción como una cadena y le devuelva la cantidad total de dinero que ha invertido en esa acción. por ejemplo, si su función recibe 'AMZN', debería devolver 4000.

Esta es mi función, sigo devolviendo NaN cuando debería devolver 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 answers
Answer question

0

investments.price e investments.numOfShares no están definidos. Es por eso que produce NaN, porque estás multiplicando dos valores indefinidos. Primero debe iterar sobre investments para encontrar las acciones que desea.

about 4 years ago · Juan Pablo Isaza Report

0

Tienes que usar el método de find

 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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!