DEV Community

Cover image for Front-End Analogies: Closure's Coffee — Recursion
Kevin K. Johnson
Kevin K. Johnson

Posted on

Front-End Analogies: Closure's Coffee — Recursion

Cleaning Shop

Recursion

Jack is a clean freak. Leave him unattended, and the whole place will be spotless. Great and all, but, uh, we're actually trying to sell coffee here.

Over time, we've gotten a good estimate of how long it'll take him to do his tasks. Give him a hard time limit and neither one of us will be driven up a wall. Or murdered.


"use strict";

const timeLimit = 45;
const cleaningTasks = [15, 10, 8, 5, 3, 25, 7];
const tasksCompleted = [];

// Jack's thinking: "I've only got 45 minutes?!"
// "I'll just go down the list, one by one."

const jackCleaning = (_tasks) => {
    return (_timeLimit) => {
        return (_tasksCompleted) => {
            console.log('_tasks', _tasks);

            let taskTimeChecker = _tasks.find(task => task <= _timeLimit);
            // "Find the first one, get it done."

            console.log('taskTimeChecker', taskTimeChecker);

            if (taskTimeChecker != undefined) {
                _timeLimit -= taskTimeChecker;
                console.log('_timeLimit', _timeLimit);
                // "Runnin' out of time."

                _tasksCompleted.push(taskTimeChecker);
                console.log('_tasksCompleted', _tasksCompleted);
                // "Another one bites the dust."

                _tasks.splice(_tasks.indexOf(taskTimeChecker), 1);
                // "Check that one off the list."
            }
            else {
                console.log(_tasksCompleted);
                return `_tasksCompleted: ${_tasksCompleted}`;
            }

            return jackCleaning(_tasks)(_timeLimit)(_tasksCompleted);
            // "All play and no work makes Jack a dull boy."
            // And he just keeps going down the list like a madman.
        }
    }
}

console.log( jackCleaning(cleaningTasks)(timeLimit)(tasksCompleted) );

// "_tasks" [15, 10, 8, 5, 3, 25, 7]
// "taskTimeChecker" 15
// "_timeLimit" 30
// "_tasksCompleted" [15]

// He's done one 15-minute task, and has 30 minutes left…

// "_tasks" [10, 8, 5, 3, 25, 7]
// "taskTimeChecker" 10
// "_timeLimit" 20
// "_tasksCompleted" [15, 10]


// "_tasks" [8, 5, 3, 25, 7]
// "taskTimeChecker" 8
// "_timeLimit" 12
// "_tasksCompleted" [15, 10, 8]


// "_tasks" [5, 3, 25, 7]
// "taskTimeChecker" 5
// "_timeLimit" 7
// "_tasksCompleted" [15, 10, 8, 5]


// "_tasks" [3, 25, 7]
// "taskTimeChecker" 3
// "_timeLimit" 4
// "_tasksCompleted" [15, 10, 8, 5, 3]


// "_tasks" [25, 7]
// "taskTimeChecker" undefined
// [15, 10, 8, 5, 3]
// "_tasksCompleted: 15,10,8,5,3"

// With only 4 minutes left, neither one of those tasks is short enough.
// Mr. Scrubbing Bubbles has to go back to slingin' beans.

Bonus Lesson!

Variable Names

Didn't all those examples seem real easy to follow with all 'em good names? If they weren't, I had no part in this and you never saw me.

On the way out, we pass a sign that says:

A. always
B. be
C. coding

Top comments (0)