JavaScript Fundamentals for Functional Programming v3

Hi,

I'm

Bianca

andcoffee.io

...but it will prepare you well if you decide to take the next step in that journey and most importantly, it will make you a more confident and capable JavaScript programmer.

This course is not about functional programming

MONAD

IDEMPOTENT

REFERENTIAL TRANSPARENCY

POINTED FUNCTOR

LAMBDA CALCULUS

ENDOMORPHISM

SETOID

COMONAD

POINT FREE STYLE

BEFORE

MONAD

IDEMPOTENT

REFERENTIAL TRANSPARENCY

POINTED FUNCTOR

LAMBDA CALCULUS

ENDOMORPHISM

SETOID

COMONAD

POINT FREE STYLE

AFTER

AFTER

SCOPE

CURRYING

CALLBACKS

HIGHER-ORDER FUNCTIONS

PARAMETERS

LODASH

IMMUTABILITY

SIDE EFFECTS

ES6

ARGUMENTS

BRACKETS + DOTS

CLOSURE

Part 1: Data Structures

Mastering objects & arrays by accessing, assigning, transforming, and organizing your data.

 

 

Part 2: Functions

Getting comfortable with creating and using functions in a more functional programming style.

 

Note: The first example of a concept tends to have intentional mistakes in them that are corrected in later slides. Be aware of this when copy/pasting slides.

Strengthen your understanding

of tricky JavaScript concepts by:

  • Understanding how to use functional utility methods

  • Understanding how to implement those methods

  • Applying JS fundamentals throughout the process

  • Hands-on practice

This course is about figuring out what happened to Mr. Boddy

... what?

Clue

Discover

WHO

killed Mr.Boddy

with what

WEAPON

and in which

ROOM

Sincere, not serious.

This is an interactive course.  

There will be uncomfortable moments!

Expect to have to think critically, answer questions, ask questions.

 

OBJECTS

DATA STRUCTURES

const who = {
  name: "Mrs. White"
};

  
const who = {
  name: "Mrs. White"
};

  
const who = {};

who.name = "Mrs. White";

When are some other times you have seen other dots in JS?

const who = {
  name: "Mrs. White"
};

  
const who = {};

who.name = "Mrs. White";

who

"name" :

"Mrs. White"

who

"name" :

"Mrs. White"

const who = {
  name: "Mrs. White"
};

who.name; // → ?

const suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

who

"name" :

"Mrs. White"

suspect

"Mrs. White"

const who = {
  name: "Mrs. White"
};

who.name; // → ?

const suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

who.story; // → ?

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

"Mrs. White"

const who = {
  name: "Mrs. White"
};

who.name; // → ?

const suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

"Mrs. White"

const who = {
  name: "Mrs. White"
};

who.name; // → ?

const suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

"Mrs. White"

const who = {
  name: "Mrs. White"
};

who.name; // → ?

const suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

const who = {
  name: "Mrs. White"
};

who.name; // → ?

let suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

"Mrs. White"

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

const who = {
  name: "Mrs. White"
};

who.name; // → ?

let suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

who.story; // → ?

"Mrs. White"

"Mr.Green"

who

"name" :

"Mrs. White"

"Mr.Green"

suspect

const who = {
  name: "Mrs. White"
};

who.name; // → ?

let suspect = who.name;  

who.name = "Mr. Green";

suspect; // → ?

suspect = "Mr. Green";

suspect; // → ?

story; // → ?

"Mrs. White"

"Mr.Green"

OBJECTS

EXERCISE

_ = {};

_.get = function(object, path, defaultVal) {
  if (object[path] === undefined) {
    return defaultVal;
  }
  
  return object[path];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → "Colonel Mustard"
 
_.get(who, 'story', 'We don\'t know yet'); 
// → "We don't know yet"

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object.path === undefined) {
    return defaultVal;
  }
  
  return object.path;
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → ??
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

undefined

default

"name" 

path

{ who }

object

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object.path === undefined) {
    return defaultVal;
  }
  
  return object.path;
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → ??
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

undefined

default

"name" 

path

{ who }

object

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object.path === undefined) {
    return defaultVal;
  }
  
  return object.path;
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → undefined
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object.path === undefined) {
    return defaultVal;
  }
  
  return object.path;
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → undefined
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object[path] === undefined) {
    return defaultVal;
  }
  
  return object[path];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → ??
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

undefined

default

"name" 

path

{ who }

object

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object[path] === undefined) {
    return defaultVal;
  }
  
  return object[path];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → "Colonel Mustard"
 
_.get(who, 'story', 'We don\'t know yet'); 
// → ??

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

undefined

default

"name" 

path

{ who }

object

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object[path] === undefined) {
    return defaultVal;
  }
  
  return object[path];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → undefined
 
_.get(who, 'story', 'We don\'t know yet'); 
// → "We don't know yet"

"We don't know"

default

"story" 

path

{ who }

object

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object[path] === undefined) {
    return defaultVal;
  }
  
  return object[path];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → "Colonel Mustard"
 
_.get(who, 'story', 'We don\'t know yet'); 
// → "We don't know yet"

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"We don't know"

default

path

{ who }

object

"story" 

const _ = {};

_.get = function(object, path, defaultVal) {
  if (object['path'] === undefined) {
    return defaultVal;
  }
  
  return object['path'];
}



const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → undefined
 
_.get(who, 'story', 'We don\'t know yet'); 
// → "We don't know yet"

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

const _ = {};

_.get = function(object, path, defaultValue) {
  const result = object == null ? undefined : object[path];
  return result === undefined ? defaultValue : result;
}

const who = { name : 'Colonel Mustard' };
 
_.get(who, 'name'); // → "Colonel Mustard"
 
_.get(who, 'story', 'We don\'t know yet'); 
// → "We don't know yet"

"name" :

"Col Mustard"

who

_

"get" :

Function () {}

"path" :

"We don't know" 

default

"story" 

path

{ who }

object

Arrays

DATA STRUCTURES

But Col Mustard is an

array of sunshine!

const who = {};

who.name = "Professor Plum";

let suspect = who.name;  

who.name = "Mrs. Scarlett";

suspect; // → ?

suspect = "Mrs. Scarlett";

suspect; // → ?

who.story; // → ?

who

"name" :

"Prof. Plum"

"Mrs. Scarlett"

suspect

const who = {};

who.name = "Professor Plum";

let suspect = who.name;  

who.name = "Mrs. Scarlett";

suspect; // → ?

suspect = "Mrs. Scarlett";

suspect; // → ?

who.story; // → ?

"Prof. Plum"

"Mrs. Scarlett"

const who = [];

who.name = "Professor Plum";

let suspect = who.name;  

who.name = "Mrs. Scarlett";

suspect; // → ?

suspect = "Mrs. Scarlett";

suspect; // → ?

who.story; // → ?
const who = [];

who.name = "Professor Plum";

let suspect = who.name;  

who.name = "Mrs. Scarlett";

suspect; // → ?

suspect = "Mrs. Scarlett";

suspect; // → ?

who.story; // → ?

When are some other times you have seen other dots in JS?

who

"name" :

suspect

"Prof. Plum"

"Mrs. Scarlett"

"Prof. Plum"

"Mrs. Scarlett"

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who[plea] = "I would never!";

who

"name" :

"Mrs. Scarlett"

who

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who[plea] = "I would never!";

"name" :

"Mrs. Scarlett"

who

"0" :

"I was not in..."

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who[plea] = "I would never!";

"name" :

"Mrs. Scarlett"

"0" :

"I was not in..."

who

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who[plea] = "I would never!";

const plea = "didShe";

"name" :

"Mrs. Scarlett"

"0" :

"I was not in..."

??

plea

who

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

const plea = "didShe";

who[plea] = "I would never!";

"name" :

"Mrs. Scarlett"

"0" :

"I was not in..."

plea

"didShe"

"didShe" :

"I would never!"

who

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who["plea"] = "I would never!";

"name" :

"Mrs. Scarlett"

"0" :

"I was not in..."

"plea" :

"I would never!"

who

const who = [];

who.name = "Mrs. Scarlett";

who[0]; // → ?

who[0] = "I was not in the Billiard Room.";

who[0]; // → ?

who.plea = "I would never!";

"name" :

"Mrs. Scarlett"

"0" :

"I was not in..."

"plea" :

"I would never!"

who

const who = {};
const where = '^&*';

who['name'] = 'Mrs. Peacock';

who[0] = 'in the hall';

who['^&*'] = 'I\'m not sure what you are implying.';

who['^&*']; // → ?

who[2 - 2] = 'in the library';

who[3 + 2 - 5]; // → ?

who[where]; // → ?

"name" :

"Mrs. Peacock"

"0" :

"in the hall"

"^&*" :

"I'm not sure..."

who

"in the library"

where

"^&*"

THE RULES

Dots

Brackets

strings

numbers

quotations

weird characters

expressions

 

strings

numbers

variables

weird characters

expressions

 

Arrays

EXERCISE

Transformations

DATA STRUCTURES

const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {

  return {
    name: name,
    color: name.split(" ")[2],
    speak: function() {
      console.log(`my name is ${name}`);
    }
  };
}


Can you find the bug?

const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {

  return {
    name: name,
    color: name.split(" ")[1],
    speak: function() {
      console.log(`my name is ${name}`);
    }
  };
}


const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {

  return {
    name,
    color: name.split(" ")[1],
    speak: function() {
      console.log(`my name is ${name}`);
    }
  };
}


const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {
  const color = name.split(" ")[1];
  return {
    name,
    color,
    speak: function() {
      console.log(`my name is ${name}`);
    }
  };
}


const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {
  const color = name.split(" ")[1];
  return {
    name,
    color,
    speak() { console.log(`my name is ${name}`); }
  };
}


const suspectNames = [
  "Miss Scarlet",
  "Col. Mustard",
  "Mrs. White",
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum"
];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}


const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

// ??



  
 

Let's add suspects to the suspectListLoop array

const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (const i = 0; i < suspectNames.length; i++) {
  // ??
  
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (let i = 0; i < suspectNames.length; i++) {
  // ??
  
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (let i = 0; i < suspectNames.length; i++) {
  const suspectName = suspectNames[i];
  // ??
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (let i = 0; i < suspectNames.length; i++) {
  const suspectName = suspectNames[i];
  createSuspect(suspectName);
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListLoop = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (let i = 0; i < suspectNames.length; i++) {
  const suspectName = suspectNames[i];
  suspectListLoop.push(createSuspect(suspectName));
}
  
 

How can we improve?

const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListForIn = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (const i in suspectNames) {
  // ??
  
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListForIn = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (const i in suspectNames) {
  const suspectName = suspectNames[i];
  suspectListForIn.push(createSuspect(suspectName)); 
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListForIn = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (const i in suspectNames) {
  const suspectName = suspectNames[i];
  suspectListForIn.push(createSuspect(suspectName));
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListForIn = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

for (const i in suspectNames) {
  const suspectName = suspectNames[i];
  suspectListForIn.push(createSuspect(suspectName));
}
  
 
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListEach = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

suspectNames.forEach(

);

 
  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListEach = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

suspectNames.forEach(suspectName => {

});

 
  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListEach = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

suspectNames.forEach(suspectName => {
  createSuspect(suspectName);
});

 
  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListEach = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

suspectNames.forEach(suspectName => {
  suspectListEach.push(createSuspect(suspectName));
});

 
  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const suspectListMap = [];

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

suspectNames.map(suspectName => {

});

  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };

function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

const suspectListMap = suspectNames.map(suspectName => {

});

  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];


function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

const suspectListMap = suspectNames.map(suspectName => {
  createSuspect(suspectName);
});


  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };


function createSuspect(name) {
  const color = name.split(" ")[1];
  const speak = () => { console.log(`my name is ${name}`); };

  return { name, color, speak };
}

const suspectListMap = suspectNames.map(suspectName => {
  return createSuspect(suspectName);
});


  
const suspectNames = ["Miss Scarlet", "Col. Mustard", "Mrs. White"];
suspectNames.metaData = { provenInnocent: [] };
const speak = () => { console.log(`my name is ${name}`); };


function createSuspect(name) {
  const color = name.split(" ")[1];

  return { name, color, speak };
}

const suspectListMap = suspectNames.map(suspectName => {
  return createSuspect(suspectName);
});


  

Transformations

EXERCISE

Organization

DATA STRUCTURES

[
  {
    "Mr. Green": [0, 0, 0, 0],
    "Mrs. Peacock": [0, 0, 0, 0],
    "Prof. Plum": [0, 0, 0, 0],
    "Mrs. White": [0, 0, 0, 0],
    "Miss Scarlet": [0, 0, 0, 0],
    "Col. Mustard": [0, 0, 0, 0]
  },
  {
    "Revolver": [0, 0, 0, 0],
    "Knife": [0, 0, 0, 0],
    "Lead Pipe": [0, 0, 0, 0],
    "Candlestick": [0, 0, 0, 0],
    "Rope": [0, 0, 0, 0],
    "Wrench": [0, 0, 0, 0]
  },
  {
    "Billiard Room": [0, 0, 0, 0],
    "Kitchen": [0, 0, 0, 0],
    "Library": [0, 0, 0, 0],
    "Conservatory": [0, 0, 0, 0],
    "Hall": [0, 0, 0, 0],
    "Dining Room": [0, 0, 0, 0]
  }
];
const suspectNames = [
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum",
  "Mrs. White",
  "Miss Scarlet",
  "Col. Mustard"
];

const weapons = [
  "Revolver",
  "Knife",
  "Lead Pipe",
  "Candlestick",
  "Rope",
  "Wrench"
];

const rooms = [
  "Billiard Room",
  "Kitchen",
  "Library",
  "Conservatory",
  "Hall",
  "Dining Room"
];

Input

const suspectNames = [
  "Mr. Green",
  "Mrs. Peacock",
  "Prof. Plum",
  "Mrs. White",
  "Miss Scarlet",
  "Col. Mustard"
];

const weapons = [
  "Revolver",
  "Knife",
  "Lead Pipe",
  "Candlestick",
  "Rope",
  "Wrench"
];

const rooms = [
  "Billiard Room",
  "Kitchen",
  "Library",
  "Conservatory",
  "Hall",
  "Dining Room"
];
[
  {
    "Mr. Green": [0, 0, 0, 0],
    "Mrs. Peacock": [0, 0, 0, 0],
    "Prof. Plum": [0, 0, 0, 0],
    "Mrs. White": [0, 0, 0, 0],
    "Miss Scarlet": [0, 0, 0, 0],
    "Col. Mustard": [0, 0, 0, 0]
  },
  {
    "Revolver": [0, 0, 0, 0],
    "Knife": [0, 0, 0, 0],
    "Lead Pipe": [0, 0, 0, 0],
    "Candlestick": [0, 0, 0, 0],
    "Rope": [0, 0, 0, 0],
    "Wrench": [0, 0, 0, 0]
  },
  {
    "Billiard Room": [0, 0, 0, 0],
    "Kitchen": [0, 0, 0, 0],
    "Library": [0, 0, 0, 0],
    "Conservatory": [0, 0, 0, 0],
    "Hall": [0, 0, 0, 0],
    "Dining Room": [0, 0, 0, 0]
  }
];

Input

Output






const suspectNames = [
  "Mr. Green"
];



 
{
  "Mr. Green": [0, 0, 0, 0]
}

Input

Desired Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}


const suspectNames = [
  "Mr. Green"
];

??

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}
const emptyTracker = createEmptyArray(4,0);

const suspectNames = [
  "Mr. Green"
];

[0, 0, 0, 0] // emptyTracker

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}
const emptyTracker = createEmptyArray(4,0);
const tracker = {};
const suspectNames = [
  "Mr. Green"
];
[0, 0, 0, 0] // emptyTracker

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}
const emptyTracker = createEmptyArray(4,0);
const tracker = {};
const suspectNames = [
  "Mr. Green"
];
const suspect = suspectNames[0];


 
{
  "Mr. Green": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}
const emptyTracker = createEmptyArray(4,0);
const tracker = {};
const suspectNames = [
  "Mr. Green"
];
const suspect = suspectNames[0];

tracker[suspect] = emptyTracker;
{
  "Mr. Green": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = [
  "Mr. Green"
];
const suspect = suspectNames[0];

tracker[suspect] = createEmptyArray(4,0);
{
  "Mr. Green": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = [
  "Mr. Green"
];
const suspect = suspectNames[0];

tracker[suspect] = createEmptyArray(4,0);
{
  "Mr. Green": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];







   
{
  "Mr. Green": [0, 0, 0, 0],
  "Mrs. Peacock": [0, 0, 0, 0],
  "Prof. Plum": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];

suspectNames.forEach(name => {
  ??
});



 
{
  "Mr. Green": [0, 0, 0, 0],
  "Mrs. Peacock": [0, 0, 0, 0],
  "Prof. Plum": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];

suspectNames.forEach(name => {
  tracker[name] = ??
});



 
{
  "Mr. Green": [0, 0, 0, 0],
  "Mrs. Peacock": [0, 0, 0, 0],
  "Prof. Plum": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];

suspectNames.forEach(name => {
  tracker[name] = createEmptyArray(4,0);
});



 
{
  "Mr. Green": [0, 0, 0, 0],
  "Mrs. Peacock": [0, 0, 0, 0],
  "Prof. Plum": [0, 0, 0, 0]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];


 

  
[
  {
    "Mr. Green": [0, 0, 0, 0],
    "Mrs. Peacock": [0, 0, 0, 0],
    "Prof. Plum": [0, 0, 0, 0]
  },
  {
    "Revolver": [0, 0, 0, 0],
    "Knife": [0, 0, 0, 0]
  },
  {
    "Billiard Room": [0, 0, 0, 0],
    "Kitchen": [0, 0, 0, 0]
  }
];

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = [suspectNames, weapons, rooms];


  
[
  [
    "Mr. Green",
    "Mrs. Peacock",
    "Prof. Plum"
  ],
  [
    "Revolver",
    "Knife"
  ],
  [
    "Billiard Room",
    "Kitchen"
  ]
]

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {
  suspectNames,
  weapons, 
  rooms
};


  
{
  "suspectNames": [
    "Mr. Green",
    "Mrs. Peacock",
    "Prof. Plum"
  ],
  "weapons": [
    "Revolver",
    "Knife"
  ],
  "rooms": [
    "Billiard Room",
    "Kitchen"
  ]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {
  suspectNames: suspectNames,
  weapons: weapons,
  rooms: rooms
};


  
{
  "suspectNames": [
    "Mr. Green",
    "Mrs. Peacock",
    "Prof. Plum"
  ],
  "weapons": [
    "Revolver",
    "Knife"
  ],
  "rooms": [
    "Billiard Room",
    "Kitchen"
  ]
}

Input

Output

function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = [ "Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};







 
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0],
    "Mrs. White": [0,0,0,0],
    "Miss Scarlet": [0,0,0,0],
    "Col. Mustard": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0],
    "Lead Pipe": [0,0,0,0],
    "Candlestick": [0,0,0,0],
    "Rope": [0,0,0,0],
    "Wrench": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0],
    "Library": [0,0,0,0],
    "Conservatory": [0,0,0,0],
    "Hall": [0,0,0,0],
    "Dining Room": [0,0,0,0]
  }
}

Input

Better Result

{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0],
    "Mrs. White": [0,0,0,0],
    "Miss Scarlet": [0,0,0,0],
    "Col. Mustard": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0],
    "Lead Pipe": [0,0,0,0],
    "Candlestick": [0,0,0,0],
    "Rope": [0,0,0,0],
    "Wrench": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0],
    "Library": [0,0,0,0],
    "Conservatory": [0,0,0,0],
    "Hall": [0,0,0,0],
    "Dining Room": [0,0,0,0]
  }
}

Previous Result

Better Result

[
  {
    "Mr. Green": [0, 0, 0, 0],
    "Mrs. Peacock": [0, 0, 0, 0],
    "Prof. Plum": [0, 0, 0, 0],
    "Mrs. White": [0, 0, 0, 0],
    "Miss Scarlet": [0, 0, 0, 0],
    "Col. Mustard": [0, 0, 0, 0]
  },
  {
    "Revolver": [0, 0, 0, 0],
    "Knife": [0, 0, 0, 0],
    "Lead Pipe": [0, 0, 0, 0],
    "Candlestick": [0, 0, 0, 0],
    "Rope": [0, 0, 0, 0],
    "Wrench": [0, 0, 0, 0]
  },
  {
    "Billiard Room": [0, 0, 0, 0],
    "Kitchen": [0, 0, 0, 0],
    "Library": [0, 0, 0, 0],
    "Conservatory": [0, 0, 0, 0],
    "Hall": [0, 0, 0, 0],
    "Dining Room": [0, 0, 0, 0]
  }
];
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

for (const section in gameData) {
  tracker[section] = ??
    
    
    
    
    
}
  
??

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

for (const section in gameData) {
  tracker[section] = {};
  
  
  
  
  
}
  
??

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

for (const section in gameData) {
  tracker[section] = {};





}
  
{
  "suspectNames": {},
  "weapons": {},
  "rooms": {}  
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData);
[ 
  "suspectNames", 
  "weapons", 
  "rooms" 
]

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  
  
  
  
  
});
  
{
  "suspectNames": {},
  "weapons": {},
  "rooms": {}  
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  
  
    section.forEach(item => {
      console.log(item);
    });
});
  
?

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  
  
    section.forEach(item => {
      console.log(item);
    });
});
  
?

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(section);
  
   // section.forEach(item => {
   //   console.log(item);
   // });
});
  
?

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(section);
  
   // section.forEach(item => {
   //   console.log(item);
   // });
});
  
"suspectNames"
"weapons"
"rooms"

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(gameData[section]);
  
   // section.forEach(item => {
   //   console.log(item);
   // });
});
  
["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
["Revolver", "Knife"]
["Billiard Room", "Kitchen"]

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
   // section.forEach(item => {
   //   console.log(item);
   // });
});
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = [ "Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    console.log(item);
  });
});
  
??

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = [ "Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    console.log(item);
  });
});
  
suspectNames : Mr. Green,Mrs. Peacock,Prof. Plum
"Mr. Green"
"Mrs. Peacock"
"Prof. Plum"
weapons : Revolver,Knife
"Revolver"
"Knife"
rooms : Billiard Room,Kitchen
"Billiard Room"
"Kitchen"

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[section] = "some string";
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

??

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[section] = "some string";
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

{
  "suspectNames": "some string",
  "weapons": "some string",
  "rooms": "some string"
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[item] = "some string";
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

??

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[item] = "some string";
  });
});

console.log(tracker);
  

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

{
  "suspectNames": {},
  "Mr. Green": "some string",
  "Mrs. Peacock": "some string",
  "Prof. Plum": "some string",
  "weapons": {},
  "Revolver": "some string",
  "Knife": "some string",
  "rooms": {},
  "Billiard Room": "some string",
  "Kitchen": "some string"
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[item] = createEmptyArray(4,0);
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

{ // ??
  "suspectNames": {},
  "Mr. Green": "some string",
  "Mrs. Peacock": "some string",
  "Prof. Plum": "some string",
  "weapons": {},
  "Revolver": "some string",
  "Knife": "some string",
  "rooms": {},
  "Billiard Room": "some string",
  "Kitchen": "some string"
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[item] = createEmptyArray(4,0);
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

{
  "suspectNames": {},
  "Mr. Green": [0,0,0,0],
  "Mrs. Peacock": [0,0,0,0],
  "Prof. Plum": [0,0,0,0],
  "weapons": {},
  "Revolver": [0,0,0,0],
  "Knife": [0,0,0,0],
  "rooms": {},
  "Billiard Room": [0,0,0,0],
  "Kitchen": [0,0,0,0]
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};
  console.log(`${section} : ${gameData[section]}`);
  
  gameData[section].forEach(item => {
    tracker[section][item] =
      createEmptyArray(4,0);
  });
});

console.log(tracker);
  
suspectNames: ["Mr. Green", "Mrs. Peacock", "Prof. Plum"]
weapons: ["Revolver", "Knife"]
rooms: ["Billiard Room", "Kitchen"]

{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
  }
}

Input

Output

// Desired Result
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
}
function createEmptyArray(length, value) {
  return Array(length).fill(value);
}

const tracker = {};
const suspectNames = ["Mr. Green", "Mrs. Peacock", "Prof. Plum"];
const weapons = ["Revolver", "Knife",];
const rooms = ["Billiard Room", "Kitchen"];

const gameData = {suspectNames, weapons, rooms};

Object.keys(gameData).forEach(section => {
  tracker[section] = {};

  gameData[section].forEach(item => {
    tracker[section][item] = 
      createEmptyArray(4,0);
  });
});

console.log(tracker);
  
{
  "suspectNames": {
    "Mr. Green": [0,0,0,0],
    "Mrs. Peacock": [0,0,0,0],
    "Prof. Plum": [0,0,0,0]
  },
  "weapons": {
    "Revolver": [0,0,0,0],
    "Knife": [0,0,0,0]
  },
  "rooms": {
    "Billiard Room": [0,0,0,0],
    "Kitchen": [0,0,0,0]
  }
}

Input

Output

Organization

EXERCISE

Part 1 DONE

Further Reading

Warm Up Exercise

 

Functions:

Intro & Review

Functions - The Basics

_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = () => { 











};







_.partition(); // → [[1, 3], [2, 4]]
const _ = {};

_.partition = () => { 











};








Function Definition

const _ = {};

_.partition = () => { 











};







  

Function Body

const _ = {};

_.partition = function() { 











};







  

Function Body

const _ = {};

_.partition = function() { 











};






_.partition();
  

function invocation

const _ = {};

_.partition = function(list, predicate) { 











};






_.partition();
  

parameters

const _ = {};

_.partition = function(list, predicate) { 











};






_.partition([1,2,3]);
  

parameters

arguments

const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  console.log(_); // ??









};

console.log(result); // ??




_.partition([1,2,3]);
  

local scope

global scope

const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  console.log(_); // { ... }









};

console.log(result); // ??




_.partition([1,2,3]);
  

local scope

global scope

const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  console.log(_); // { ... }









};

console.log(result); // ??




_.partition([1,2,3]);
  

local scope

global scope

const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];










};

 
const isOdd = function(n) {
  return n % 2;
}

_.partition([1,2,3]);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];










};

 
const isOdd = function(n) {
  return n % 2;
}
isOdd(2); // ??
isOdd(3); // ??

_.partition([1,2,3]);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];










};

 
const isOdd = function(n) {
  return n % 2;
}
isOdd(2); // 0
isOdd(3); // 1

_.partition([1,2,3]);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];










};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  
  isOdd();








};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  
  console.log(predicate); // ??








};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  
  console.log(predicate); 
  // => ƒ (n) {
  //   return n % 2;
  // }





};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  
  console.log(predicate === isOdd); // ??
  
  
  





};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  
  console.log(predicate === isOdd); // true
  
  
  





};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
    
  result.forEach((item, index, list) => {
    console.log(item); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];
  const item = "Oh, hello there.";

  result.forEach((item, index, list) => {
    console.log(item); // ??
    
    
    
  
  });


};

_.partition([1, 2, 3, 4], n => n % 2);
// → [[1, 3], [2, 4]]
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  result.forEach((item, index, list) => {
    console.log(item); // [], []
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.count('I\'ve been called...'); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.count('I\'ve been called...'); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.log(item); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.log(item); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.log(list); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = function(list, predicate) { 
  const result = [[],[]];
  const item = "Oh, hello there.";
  
  list.forEach((item, index, list) => {
    console.log(list); // ??
    
    
    
  
  });


};

 
const isOdd = function(n) {
  return n % 2;
}



_.partition([1,2,3,4], isOdd);
  
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(arguments); // ??
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(arguments);
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((...args) => {
    console.log(args); // ??
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((...args) => {
    console.log(args);
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((...args) => {
    console.log(...args); // ??
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((...args) => {
    console.log(...args); 
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(...args => {
    console.log(...args); // ??
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(...args => {
    console.log(...args);
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(predicate); // ??
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]

predicate vs iteratee

const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(predicate);
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(predicate());
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    console.log(predicate());
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    predicate(item); // ?
    
    
    
  
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    if(predicate(item)) {
      // ??
    } else {
      // ??
    }
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    if(predicate(item)) {
      // add to left
    } else {
      // add to right
    }
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    if(predicate(item)) {
      result[0].push(item);
    } else {
      result[1].push(item); 
    }
  });


};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    if(predicate(item)) {
      result[0].push(item);
    } else {
      result[1].push(item); 
    }
  });

  return result;
};
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]

Intro & Review

EXERCISE

Callbacks + Higher Order Functions

FUNCTIONS

1. HOFs take a function as an input.

element.addEventListener("change", () => {
  console.log("Our evidence is updated");
});

2. HOFs return a function as the output

const newClue = (name) => {
  const length = name.length;

  return (weapon) => {
    const clue = length + weapon.length;
    return !!(clue % 1);
  };

};
const ifElse = (condition, isTrue, isFalse) => {
  return condition ? isTrue() : isFalse();
};

ifElse(isGuilty,
 () => { console.log('GUILTY!!');},
 () => { console.log('NOT GUILTY');}
);
const ifElse = (condition, isTrue, isFalse) => {
  return condition ? isTrue() : isFalse();
};

const logTrue = () => { console.log('GUILTY!!');}
const logFalse =  () => { console.log('NOT GUILTY');}

ifElse(true, logTrue, logFalse);
const ifElse = (condition, isTrue, isFalse, p) => {
  //How do we pass arguments?
  return condition ? isTrue(p) : isFalse(p);
};

ifElse(true, fn1, fn2, 'HI');
const ifElse = (condition, isTrue, isFalse, ...args) => {
  console.log(args); //['HI', 'BYE', 'HOLA']

  return condition ? 
    isTrue(...args) : // isTrue('HI', 'BYE', 'HOLA')
    isFalse(...args);
  
};

ifElse(true, fn1, fn2, 'HI', 'BYE', 'HOLA');
const ifElse = (condition, isTrue, isFalse) => {
  const args = [].slice.call(arguments, 3);
  
  return condition ?
    isTrue.apply(this, args) :
    isFalse.apply(this, args);
};

const logTrue = (msgs) => { console.log(msgs); };
const logFalse = (msgs) => { console.log(msgs); };

ifElse(true, logTrue, logFalse, 'a', 'b');
const ifElse = (condition, isTrue, isFalse) => {
  const args = [].slice.call(arguments, 3);
  
  return condition ?
    isTrue.apply(this, args) :
    isFalse.apply(this, args);
};

const logTrue = (msgs) => { console.log(msgs); };
const logFalse = (msgs) => { console.log(msgs); };

ifElse(true, logTrue, logFalse, 'a', 'b');
const ifElse = function(condition, isTrue, isFalse) {
  const args = [].slice.call(arguments, 3);
  
  return condition ?
    isTrue.apply(this, args) :
    isFalse.apply(this, args);
};

const logTrue = (msgs) => { console.log(msgs); };
const logFalse = (msgs) => { console.log(msgs); };

ifElse(true, logTrue, logFalse, 'a', 'b');

Callbacks + HOF

EXERCISE

const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    predicate(item) ? 
      result[0].push(item) : 
      result[1].push(item); 
  });

  return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item, index, list) => {
    result[predicate(item) ? 0 : 1].push(item)
  });

 return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach((item) => {
    result[predicate(item) ? 0 : 1].push(item)
  });

 return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(item => {
    result[predicate(item) ? 0 : 1].push(item)
  });

 return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(item => result[predicate(item) ? 0 : 1].push(item));

  return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition => (collection, predicate) {
  const basePartition = (result, value) => (
    result[predicate(value) ? 0 : 1].push(value), result
  );

  return _.transform(collection, basePartition , [[], []]);
}
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]
const _ = {};

_.partition => (collection, predicate) {
  const basePartition = (result, value) => (
    result[predicate(value) ? 0 : 1].push(value), result
  );

  return _.reduce(collection, basePartition , [[], []]);
}
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]

FUNCTIONAL

STYLE

FUNCTIONS

IMMUTABLE

CURRIED

ITERATEE

DATA

ITERATEE

DATA

ITERATEE FIRST - DATA LAST

const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(item => result[predicate(item) ? 0 : 1].push(item));

  return result;
};



 
_.partition([1, 2, 3, 4], n => n % 2);

// → [[1, 3], [2, 4]]

ITERATEE FIRST - DATA LAST

const _ = {};

_.partition = (list, predicate) => { 
  const result = [[],[]];

  list.forEach(item => result[predicate(item) ? 0 : 1].push(item));

  return result;
};



 
_.partition( n => n % 2, [1, 2, 3, 4]);

// → [[1, 3], [2, 4]]

ITERATEE FIRST - DATA LAST

const _ = {};

_.partition = (predicate, list) => { 
  const result = [[],[]];

  list.forEach(item => result[predicate(item) ? 0 : 1].push(item));

  return result;
};



 
_.partition( n => n % 2, [1, 2, 3, 4]);

// → [[1, 3], [2, 4]]

IMMUTABLE

IMMUTABLE

_.defaults = (object, ...sources) => {


  sources.forEach(source => {
    for (const key in source) {
      const value = object[key];
      if (value === undefined) {
        object[key] = source[key];
      }
    }
  });
  return object;
};

IMMUTABLE

_.defaults = (object, ...sources) => {
  const clonedObject = { ...object };

  sources.forEach(source => {
    for (const key in source) {
      const value = clonedObject[key];
      if (value === undefined) {
        object[key] = source[key];
      }
    }
  });
  return clonedObject;
};

CURRIED

FUNCTIONAL

STYLE

EXERCISE

Currying

FUNCTIONS

const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);














  
const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

curriedGuess("Mrs. Peacock")("library")("candlestick");
// => "I think it was Mrs. Peacock in the library with a candlestick"

console.log(typeof curriedGuess); //?












  
const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

curriedGuess("Mrs. Peacock")("library")("candlestick");
// => "I think it was Mrs. Peacock in the library with a candlestick"

console.log(typeof curriedGuess); // function












  
const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess("Miss Scarlett", "billiards room");





const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess("Miss Scarlett", "billiards room");

console.log(typeof guessLater); // => ??



const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess("Miss Scarlett", "billiards room");

console.log(typeof guessLater); // => function



const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess("Miss Scarlett", "billiards room");

guessLater("candlestick");
// => ??





const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess("Miss Scarlett", "billiards room");

guessLater("candlestick");
// => "I think it was Miss Scarlett in the billiards room with a candlestick"





const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

const guessLater = curriedGuess(_, _,"candlestick")

guessLater("Colonel Mustard", "kitchen");
// => "I think it was Colonel Mustard in the kitchen with a candlestick"




const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

curriedGuess("Mrs. Peacock")("library")("candlestick");

curriedGuess("Miss Scarlett", "billiards room")("candlestick");

const guessLater = curriedGuess(_, _,"candlestick")

guessLater("Colonel Mustard", "kitchen");


// => "I think it was Mrs. Peacock in the library with a candlestick"
// => "I think it was Miss Scarlett in the billiards room with a candlestick"
// => "I think it was Colonel Mustard in the kitchen with a candlestick"

  • arity
  • func.length

IMMUTABLE

CURRIED

ITERATEE

DATA

const clue = () => {
  const msg = "Help! I think I found a clue!";
 
  const logger = () => {
    console.log(msg);
  };

  setTimeout(logger, 1000);
  console.log("what happens first? this log or the halp above?");
};

clue();
// => ??
// => ??







 



  
const clue = () => {
  const msg = "Help! I think I found a clue!";

  const logger = () => {
    console.log(msg);
  };

  setTimeout(logger, 1000);
  console.log("What happens first? this log or the halp above?");
};

clue();
// => "What happens first? this log or the halp above?"
// => "Help! I think I found a clue!"







 



  
const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');




 
 



const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');






console.log(typeof funcClueCounter1); // => ??



 
 



const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');






console.log(typeof funcClueCounter1); // => function



 
 



const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

 



console.log(typeof funcClueCounter1); // => function
const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

 



console.log(typeof funcClueCounter1); // => function
console.log(funcClueCounter1 === funcClueCounter2); // => ??
const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

 



console.log(typeof funcClueCounter1); // => function
console.log(funcClueCounter1 === funcClueCounter2); // => false
const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

funcClueCounter1(), funcClueCounter1(), funcClueCounter1();
// => ??






const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

funcClueCounter1(), funcClueCounter1(), funcClueCounter1();
// => "Clue # 1"
// => "Clue # 2"
// => "Clue # 3"




const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

funcClueCounter1(), funcClueCounter1(), funcClueCounter1();
// => "Clue # 1"
// => "Clue # 2"
// => "Clue # 3"

funcClueCounter2();
// => ??

const clueCounter = (msg) => {

  let count = 0;
  const logger = () => {
    console.log(`${msg} ${++count}`);
  };

  return logger;
};

const funcClueCounter1 = clueCounter('Clue #');
const funcClueCounter2 = clueCounter('Another clue #');

funcClueCounter1(), funcClueCounter1(), funcClueCounter1();
// => "Clue # 1"
// => "Clue # 2"
// => "Clue # 3"

funcClueCounter2();
// => "Another clue # 1"

const cC = (msg) => {

  let count = 0;
  const lg = () => {
    console.log(`${msg} ${++count}`);
  };

  return lg;
};

const cc1 = cC('Clue #');
const cc2 = cC('Another clue #');

cc1(), cc1(), cc1();

cc2();

fn { ... }

cC

lg() { ... }

cc1

"Clue #"

msg

lg() { ... }

lg

0

count

lg() { ... }

cc2

"Anot.."

msg

lg() { ... }

lg

0

count

const cC = (msg) => {

  let count = 0;
  const lg = () => {
    console.log(`${msg} ${++count}`);
  };

  return lg;
};

const cc1 = cC('Clue #');
const cc2 = cC('Another clue #');

cc1(), cc1(), cc1();

cc2();

lg() { ... }

cc1

"Clue #"

msg

lg() { ... }

lg

0

count

1

2

3

lg() { ... }

cc2

"Anot.."

msg

lg() { ... }

lg

0

count

1

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};


 


  
const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};


 



  

fn { ... }

countClues

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};

const c = countClues();



   

fn { ... }

countClues

0

n

{ count: fn,

reset: fn }

c

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};

const c = countClues();
console.log(c);
{
  count: () => n++,
  reset: () => n = 0
}

fn { ... }

countClues

0

n

{ count: fn,

reset: fn }

c

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();


  

fn { ... }

countClues

0

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();

c.count();
-> ??

  

fn { ... }

countClues

0

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

const countClues = () => {
  let n = 0;

  return {
    count: () => n++,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();

c.count();
-> 0

  

fn { ... }

countClues

1

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

const countClues = () => {
  let n = 0;

  return {
    count: () => ++n,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();

c.count();
-> 1

  

fn { ... }

countClues

1

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

const countClues = () => {
  let n = 0;

  return {
    count: () => ++n,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();

c.count(), c.count();
d.count();

fn { ... }

countClues

2

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

-> ??

  

1

1

const countClues = () => {
  let n = 0;

  return {
    count: () => ++n,
    reset: () => n = 0
  };
};

const c = countClues();
const d = countClues();

c.count(), c.count();
d.count();

fn { ... }

countClues

2

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

-> 1

  

1

1

Currying

EXERCISE

const guess = function(suspect, room, weapon) {
  console.log(`I think it was ${suspect} in the ${room} with a ${weapon}`);
};

const curriedGuess = _.curry(guess);

curriedGuess("Mrs. Peacock")("library")("candlestick");

CONCLUSION

Further Reading

async + await

generator functions (yield)

backticks (``)

ASSETS

fn { ... }

1

clueCn

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

fn { ... }

1

n

{ count: fn,

reset: fn }

c

d

0

n

{ count: fn,

reset: fn }

[ [], [] ]

result

{ ... }

_

n => n % 2

predicate

[1, 2, 3, 4]

list

??

index

??

item

[1, 2, 3, 4]

list

0

n

fn { ... }

countClues

0

n

??

index

{ count: fn,

reset: fn }

firstCounter

fn { ... }

countClues

0

n

{ count: fn,

reset: fn }

c

"name" :

"Mrs. Scarlett"

who

??

plea

"plea" :

"I would never!"

"name" :

"Mrs. Scarlett"

who

"0" :

"I was not in..."

"name" :

"Mrs. Scarlett"

who

"0" :

"I was not in..."

"plea" :

"I would never!"

DEBUG

const mrsScarlet = { ... };











 
const mrsScarlet = { ... };

const profPlum = { ... };








 
  
const mrsScarlet = { ... };

const profPlum = { ... };

const colMustard = { ... };







  
const mrsScarlet = { ... };

const profPlum = { ... };

const colMustard = { ... };

const mrsWhite = { ... };





  
const mrsScarlet = { ... };

const profPlum = { ... };

const colMustard = { ... };

const mrsWhite = { ... };

const mrGreen = { ... };



 
const mrsScarlet = { ... };

const profPlum = { ... };

const colMustard = { ... };

const mrsWhite = { ... };

const mrGreen = { ... };

const mrsPeacock = { ... };

  

Fundamentals for Functional Programming in JavaScript V3

By Bianca Gandolfo

Fundamentals for Functional Programming in JavaScript V3

Fundamentals for Functional Programming in JavaScript V3

  • 7,290