im coding some front end code right now and a user submits a form with a name of a product and a price of the product. this then gets saved to a database.we send that data to the frontend with PUG this is the simple pug file html(lang="en") head meta(charset="UTF-8") meta(name="viewport", content="width=device-width, initial-scale=1.0") meta(http-equiv="X-UA-Compatible", content="ie=edge") title Document body each a in {user} each i in a.items p #{i.price} script(src="./js/sum.js") basically this just returns all items prices in each individual paragraph which ill then retrieve and parse into integers here is my js codefunction addAll(){ const all = document.getElementsByTagName('p'); let arr = []; for (let i = 0; i < all.length; i++) { const element = all[i].innerText; let update = arr.push(element); } arr.reduce((acc, current) => { const summedUp = parseInt(acc) + parseInt(current) console.log(summedUp) }) } window.addEventListener("load", addAll); however when i add two items it returns the total prices of all items (which is what its supposed to do) for example say i have an array of two items pricesvar arr = [1,2]then itll will console.log the array as 3 (great it works)however when i add another one and make it [1,2,3] it will output 3 and a NaN . why is it only reducing when i have 2? wtf is this error.....
Submitted April 03, 2018 at 02:50PM by SeanCarroll17
No comments:
Post a Comment