{"id":3163,"date":"2014-03-17T17:44:54","date_gmt":"2014-03-17T17:44:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/17\/javascript-calculate-value-of-element-with-id-like-collection-of-common-programming-errors\/"},"modified":"2014-03-17T17:44:54","modified_gmt":"2014-03-17T17:44:54","slug":"javascript-calculate-value-of-element-with-id-like-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/17\/javascript-calculate-value-of-element-with-id-like-collection-of-common-programming-errors\/","title":{"rendered":"Javascript &#8211; Calculate value of element with id like-Collection of common programming errors"},"content":{"rendered":"<p>You are building the price array based on the &#8216;i&#8217; loop counter. The 10.50 goes into price[0], The quanty of 1 that goes with it is put in qty[1], then i goes through values 2 and 3 with no changes to the arrays. Then when i=4, price[4] gets 19.20 and then qty[5] gets 2 then next time through.<\/p>\n<p>The resulting arrays look like<\/p>\n<pre><code>price[0] = 10.5, price[4] = 19.20\nqty[1] = 1, qty[5] = 2\n<\/code><\/pre>\n<p>When you loop through the last time to try and so the math, you are essentially doing this:<\/p>\n<pre><code>\/\/ i = 0\ntotal = total + price[0] * qty [0] \/\/ price[0] = 10.50, but qty[0] = undefined, so total = Nan\n\n\/\/ i = 1\ntotal = total + price[1] * qty [1] \/\/ price[1] = undefined, qty[1] 1, so total = Nan\n\n\/\/ etc.\n<\/code><\/pre>\n<p>Make sense?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are building the price array based on the &#8216;i&#8217; loop counter. The 10.50 goes into price[0], The quanty of 1 that goes with it is put in qty[1], then i goes through values 2 and 3 with no changes to the arrays. Then when i=4, price[4] gets 19.20 and then qty[5] gets 2 then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3163","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3163"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3163\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}