At the bottom of our file, let's add a console.log(cart) so we can see the output when we run our code. That's it! Now, for each iteration of our loop we modify acc storing the final result in the variable cart. reduce() updates the value of acc after we've modified it. reduce() we make sure to return the acc value after we've modified it. The important part: notice that at the very bottom of the function we pass to. Feel free to play and change this as you see fit (just make sure you use the same key names on each object and the correct data types for the values). amount - The amount per item as a float (decimal) number.quantity - Quantity of this item being purchased as an integer.name - A name for the item as a string.id - A unique ID for the item as an integer.Each item in the cart will have four properties: The first thing we need to do is create an array of items that will represent our cart. Next, cd into that directory and create an index.js file (this is where we'll write our code for the tutorial): Once you have Node.js installed on your computer, from your projects folder on your computer (e.g., ~/projects), create a new folder for our work: If you don't already have Node.js installed on your computer, read this tutorial first and then come back here. Because the code we're writing for this tutorial is "standalone" (meaning it's not part of a bigger app or project), we're going to create a Node.js project from scratch.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |