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) => {

// "Find the first one, get it done."

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

// "Another one bites the dust."

// "Check that one off the list."
}
else {
}

// "All play and no work makes Jack a dull boy."
// And he just keeps going down the list like a madman.
}
}
}

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

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

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

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

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

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

// "_tasks" [25, 7]
// [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.

A. always
B. be
C. coding