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

187
Views
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 answers
Answer question

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 Report

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 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!