Ad
  • Custom User Avatar

    function add(a) {
    const sum = b => add (a + b);
    sum.valueOf = () => a;
    total return;
    }

    • Function add() just returns function sum = () => (b) => add(a + b) with sum.valueOf = () => a; where a is the final sum, b is the new addition;
    • Result(final sum) just return when sum.valueOf is run.

    sum.valueOf will run when:
        + Do math
        + Compares with ==;
    

    Example:
    ___ const addTwo = add (2) // sum = (b) => 2 + b; Returns sum function with sum.valueOf = () => 2;
    |
    |__ addTwo + 5 // 7 sum.valueOf ran when doing math and returned 2 so 2 + 5 = 7;
    |
    |__ addTwo + add(1) // 3 Both sum.valueOf of the two sum functions ran and returned 2 and 1 so 2 + 1 = 3

    ___ addTwo(3) // sum = () => 5 + b; Returns sum function with sum.valueOf = () => 5;
    |
    |__ addTwo(3) == 5 // true Because sum.valueOf ran when you compare with "==" and returned 5 so 5 == 5 is true;

    Am I wrong?

  • Custom User Avatar