Skip to main content

5. 7 Summarizing with reduce

 Summarizing with reduce   Understanding Reduce Purpose of Reduce : reduce is a higher-order function used to compute a single value from an array. It's instrumental in operations like summing up numbers or finding an item in an array that meets a specific criterion (e.g., the script with the most characters). Operation : It works by repeatedly taking an element from the array and combining it with a current value until all elements have been processed. This process is akin to folding or reducing the array into a single value. 🎨: Think of reduce like making juice from oranges. You start with a bunch of oranges (the array), and then you squeeze them one by one into a jug (the single value). Your hands (the combining function) do the squeezing, and you might already have some juice in the jug to start with (the start value). 👩‍🎓: The Mechanics of reduce - The reduce function takes three arguments: the array to reduce, a combining function, and a starting value. The combin...

Chapter 4.3 Methods

 

Chapter 4 Methods


This paragraph discusses "methods" in JavaScript, particularly focusing on string and array methods. Let's break it down:

  1. Methods on String Values:

    • Methods are properties of values that hold function values.

    • Example with String:

      javascript
    let doh = "Doh";
    console.log(typeof doh.toUpperCase); // → function
    console.log(doh.toUpperCase());      // → DOH

    Here, toUpperCase is a method of the string doh. When called (doh.toUpperCase()), it returns a new string with all letters converted to uppercase.

    Similarly, there's a toLowerCase method for converting to lowercase.

    Importantly, even without explicit arguments, these string methods operate on the string they belong to ("Doh" in this case).

    Methods on Array Values:

    Arrays have their own set of methods.

    Example with Array:

    javascript
    • let sequence = [1, 2, 3];
      sequence.push(4);
      sequence.push(5);
      console.log(sequence);  // → [1, 2, 3, 4, 5]
      console.log(sequence.pop()); // → 5
      console.log(sequence);  // → [1, 2, 3, 4]

      push is a method that adds elements to the end of an array.

      pop is a method that removes the last element of an array and returns it.

  1. Stack Concept in Programming:

    • The terms "push" and "pop" come from the concept of a stack in programming.

    • A stack is a data structure where the last item added is the first item removed (Last In, First Out - LIFO).

    • This concept is seen in various programming scenarios, like the function call stack.

👩‍🎓: Methods like toUpperCase for strings or push and pop for arrays provide convenient ways to manipulate these data types. They are inherent to the data type and can be called directly on the instances of those types.

🎨: Think of each method as a special tool designed for a specific type of data. toUpperCase is like a magnifying glass that enlarges every letter in a string, while push and pop are like adding or removing books from a stack on your desk. They're intuitive and tailored to the nature of the data they work with.

In summary, understanding methods is crucial for effective programming in JavaScript, as they offer powerful ways to interact with and manipulate data.


Link to the chapter in the book Eloquent Javascript:

Comments