• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

How to compute the sum of numerical values of a map?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JavaScript, how can I compute the sum of the numerical values of a map? Why do the following returns 0 instead of 35? In the exercise, the Map structure shop1 was given so the solution is supposed to do without modifying the lines where shop1 is constructed.
 
Saloon Keeper
Posts: 12499
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

It's because you used "rest syntax" for your createSumMap function. You declared the shop parameter with three periods, which means that all arguments that you call the function with will be wrapped in a single array. When you call createSumMap(shop1), the shop parameter will hold an array containing a single element of type Map. Since Map is not a number, nothing gets added to your total.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic