So what would be the result if I change the initial value from zero to 10? I will execute the console.log on the cartTotal again and to the right, we can see that the value has increased by 10. ![]() Down below, I'm gonna console.log cartTotal and I will execute it and to the right is the total of the shoppingCart. And because I'm using zero, it will not change the outcome of this function. After the curly brackets, before the closing parentheses, I will type comma zero. But there is an optional initial value argument that I can pass through. Now, technically, the reduce function is ready to be executed. And inside of the curly brackets, I'm gonna return total plus element.price. I'm using element just to make this clear that reduce will iterate through each element in the given array. So I will need another parenthesis and then total and element. And inside of the parentheses, I'm gonna pass through two arguments. ![]() Const cartTotal equals shoppingCart.reduce parenthesis. And I'm gonna begin by creating an arrow function. I'm going to reduce this array to get the total of the shopping cart. Each object has two properties: a product and a price. On the screen, I have a shoppingCart array, which is an array of objects. The premise behind the reduce function is to reduce an array into a singular value. Introduction to JavaScript higher-order functions: reduce.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |