This is the 10th day of my participation in Gwen Challenge

The problem background

I thought of this problem mainly when I was explaining the array reduce method to members. I want to give an example to illustrate the particularity of this function.

Array.reduce()

  • The reduce() method runs the function on each array element to generate (reduce it) a single value.
  • The reduce() method works from left to right in the array.
  • The reduce() method does not reduce the original array.
  • The reduce() method receives a function callback as an accumulator function, and each value in the array (from left to right) is merged to a single value.

grammar

array.reduce(callback, initialValue)
Copy the code

parameter

The functions passed to reduce() and reduceRight() take four arguments

  • Total (initial value/previously returned value)
  • The project value
  • Index of the project
  • The array itself

When the callback function is first executed, the total and the item value can be the same value. If initialValue is provided when reducing () is called, then the first total is equal to initialValue and the item value is equal to the first value in the array; If initialValue is not provided, the total is equal to the first value in the array, and the item value is equal to the second value in the array.

The array iteration method operates on each array item: the return value from each iteration of the function is the first argument to the next iteration.

The following question arises from the benefit that any value returned by each iteration of a function is automatically passed to the next item as the first argument.


Binary tree traversal calculation and evaluation problem

Requirement: Calculate the sum of all nodes without using variables

var a = [{
    count: 1,
    name: 'a',
    children: [{
        count: 2,
        name: 'b',
        children: [{
            count: 3,
            name: 'c'
        },
        {
            count: 4,
            name: 'd'
        }]
    },
    {
        count: 5,
        name: 'e',
        children: [{
            count: 6,
            name: 'f'
        },
        {
            count: 7,
            name: 'g'}}}]]]Copy the code

solution

If you don’t use variables to calculate data, you can only do numerical calculations on your own. It’s just possible to borrow the properties of this method, and then the case of a line of code needs to be considered with self-invocation, and then has a certain chain effect. The immediate expression + reduce approach is used here.

Implementation method

var a = [{
    count: 1,
    name: 'a',
    children: [{
        count: 2,
        name: 'b',
        children: [{
            count: 3,
            name: 'c'
        },
        {
            count: 4,
            name: 'd'
        }]
    },
    {
        count: 5,
        name: 'e',
        children: [{
            count: 6,
            name: 'f'
        },
        {
            count: 7,
            name: 'g'
        }]
    }]
}]
 
console.log((function countF(a) {
    return a.reduce(function(x, y) {
        if(y.dren) {// If there are child nodesreturn x + y.count + countF(y.children)
        } else{// If there are no child nodesreturn x + y.count
        }
 
    },
    0)
})(a))
Copy the code

Returns the result