Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Andrew Cauthorn
Andrew Cauthorn
5,446 Points

Can't get the totalValue to log to console

The total value logged in the console is NaN. I've tried a variety of methods to convert the value to a number without any luck. Code is posted below:

let products = [
 {
  name: "Lord of the Rings Trilogy",
  inventory: 20,
  unit_Price: 49.99 
 },
 {
  name: "The Complete Star Wars Saga",
  inventory: 20,
  unit_Price: 99.99 
 },
  {
  name: "Dune: Part I",
  inventory: 20,
  unit_Price: 29.99 
 }
]

function listProducts(prods){
  let product_names = [];
for (let i = 0; i < prods.length; i+=1) {
  product_names.push(prods[i].name);
  }
  return product_names;
  }

console.log(listProducts(products));


function totalValue(prods) {
  let inventoryValue = 0;
  for (let i = 0; i < prods.length; i+=1) {
  inventoryValue += prods[i].inventory * prods[i].unit_price;
  }
  return inventoryValue;
  }

console.log(totalValue(products));

2 Answers

Steven Parker
Steven Parker
229,582 Points

It's just a spelling error/typo. The objects contain a "unit_Price" (capital "P"), but the later code is trying to access "unit_price" (lower case "p").

Steven Parker
Steven Parker
229,582 Points

Andrew Cauthorn — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

Andrew Cauthorn
Andrew Cauthorn
5,446 Points

Thank you, Steven! I can believe I missed that...