Title photo
frugal technology, simple living and guerrilla large-appliance repair
Sun, 18 Jun 2017

Eloquent Javascript, Chapter 3 (Functions) -- what the hell?

I read Chapter 3 of Eloquent Javascript some time ago, and it's a difficult one. It introduces the concept of functions. Quickly introduced are: Parameters and Scopes, Nested Scopes, Closure and Recursion.

It is too much, too fast with too few examples. I was able to do the first exercise, Minimum, but got lost in the second, Recursion.

Here is my solution for Minimum:

#!/usr/bin/env node
/* Eloquent Javascript, Chapter 3, Page 56, Exercises 
Create a function to find the minimum of two arguments

By Steven Rosenberg, 6/17/2017 */

function smallest(first_number, second_number) {
    if (first_number < second_number)
        return first_number;
    else if (second_number < first_number)
        return second_number;
        console.log("They are equal")

// Output will be the smallest of these two numbers
console.log(smallest(100, 2));

Expressing this as a function doesn't really do much. The program could just as easily have been written in a straight "procedural" format. But it's a function, and it works.

The second problem on recursion stumped me. I'm pretty sure I can figure it out, but I need more time to think (and look up more on recursion).