Skip to content

Instantly share code, notes, and snippets.

@nicholas-johnson
Last active October 27, 2017 13:47
Show Gist options
  • Save nicholas-johnson/0bc015e13fdc4f1e6a734309643bba96 to your computer and use it in GitHub Desktop.
Save nicholas-johnson/0bc015e13fdc4f1e6a734309643bba96 to your computer and use it in GitHub Desktop.

Core JavaScript Questions

JS: Time - 15 minutes

Part 1 - Cat Factory

I have a startup providing custom cats to celebrities: www.petfactory.com. The core of my startup is this wondrous function that I purchased from an agency:

var catFactory = function() {
  var cat = {};
  cat.name = "Moof";
  cat.age = 12;
  return cat;
}

var moof = catFactory();
console.log(moof)

Check it out here: https://repl.it/NUjE/0

  1. I have a problem that all the cats are the same. I have a request for a kitten named Daisy. Kindly parameterise the function so that I can create this custom cat and satisfy my client.

Part 2 - Rat factory

The startup is doing well, and we expanding into rats. We went back to the agency with our 10m seed funding, and six months later they sent us this wonder of modern technology:

var rat = {};

var ratFactory = function(name, age) {
  rat.name = name;
  rat.age = age;
  return rat;
}

var hatchet = ratFactory('hatchet', 4);
var chopsticks = ratFactory('chopsticks', 15);

console.log(hatchet)

Check it out here: https://repl.it/NUj8/1

  1. Unfortunately it’s not working. Without running it, can you tell me what the problem is, and what will happening when I run it?
  2. Can you fix it by moving one line into the right place?

Part 3 - Slug factory

It’s a new craze! Custom slugs for rich people. Slugs are a little more complicated than other animals, so understandably, our agency charged quite a bit more. Luckily we have a new VC who still hasn’t worked out that we have literally no idea what we are doing.

var slugFactory = function() {
  var slug = {
    colour: "caliginous grey",
    age:0
  }
  
  var makeRandomName = () => {
    var slug = {}
    var names = ['mikey', 'paul', 'stu', 'danboy'];
    slug.name = [0,0].map(() => names[Math.floor(Math.random()*names.length)])
  }
  
  slug.name = makeRandomName();
  return slug;
}

var nero = slugFactory();
console.log(nero)

Check it out here: https://repl.it/NUjs/1

  1. Our scientists have determined that this code does not work. Without running it, can you tell me what the output is?
  2. Can you make the name be defined, thereby saving the entire company?
  3. Bonus points for mentioning that Nero was obviously a caterpillar.

Part 4 - Pet assembly

We are scaling our startup. We now have hundreds of pets all in progress at the same time. Our agency has suggested that the best way to handle this would be with a class.

class PetFactory {
  constructor() {
    this.pets = []
  }
  makeCat(count) {
    this.pets.push(new Cat())
  }
  addSkillToAll(fnName, fn) {
    // ^^^^^ problem here! ^^^^^
  }
}

class Cat {
  constructor() {
    this.miows = true;
  }
}

var petFactory = new PetFactory()

petFactory.makeCat();

Check it here: https://repl.it/NUkC/1

This is all jolly fun. We can rustle up a new Cat in less time than it takes to say “cats are cool”, but we have some problems.

  1. makeCat() is nice, but slow. I want to be able to create 50 delicious cats with just one method. Kindly give me a makeCats(50) method that will add 50 cats to the pets array.

Part 5 - Catskills

In the above code, the addSkillToAll() method doesn’t work. It’s supposed to receive a string and a function, then add that function to all the pets in the pets array. I want to be able to call:

petFactory.makeCats(5000)
petFactory.addSkillToAll("jump", () => {console.log('woot')})
petFactory.pets[22].jump()

Can you implement the addSkillToAll method.

Use this repl: https://repl.it/NUkZ/2

hint: map?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment