Popcorn Hacks (Classwork) 🍿
  • All it takes to perform some complex calculations is one method. reduce() sums all the numbers in a given array. More specifically, reduce() takes a callback function (basically a function that can be referenced in later bits of code), which processes all the items in a given array. Create a list that uses this function.
  • Now create another array containing the integers: 3, 4, 8, 9, 1, 80, and 77. Put these integers in RANDOM ORDER. This will be the second popcorn hack. Now use the sort() method to reorganize these integers in numerical order. Now do the same thing again, but this time write out the integers using words. Sort these alphabetically.
%%javascript

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0);

console.log(sum); // Output: 15

// Accumulator holds the running total (or whatever you want to accumulate). Current refers to the current item being processed. The second argument (0) initializes the accumulator.
Javascript Hack 🏬🛒
  • It's time to go shopping! Try to create a program that allows users to manage items using an array, which can be visually represented with a shopping cart.
  • While not necessary, feel free to show the cart filling up with icons (items) as the user adds or subtracts items.