In this practice, you will practice debugging more context problems.
Clone the starter from the Download link at the bottom of this page.
Run npm install to install any dependencies.
Implement the following in the problems/01-car-drive.js file.
- Create a
Carclass. - A newly instantiated instance should have its
speedproperty initialized to0. - Add an instance method called
drive(newSpeed)that takes in anewSpeedand sets it to the instance'sspeedproperty. It should also return the presentspeedof the instance.
Test your implementation by running the test specs in the test/01-car-drive-spec.js file. Run the specs with the following command:
npm test test/01-car-drive-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
let car = new Car();
car.drive(0); // => returns 0
console.log(car); // => { speed: 0 }
car.drive(10); // => returns 10
console.log(car); // => { speed: 10 }
car.drive(50); // => returns 50
console.log(car); // -> { speed: 50 }
car.drive(100); // => returns 100
console.log(car); // -> { speed: 100 }Implement the following in the problems/02-calculator.js file.
- Create a
Calculatorclass. - A newly instantiated instance should have its
totalproperty initialized to0. - Add the following instance methods which should all return the reassigned
totalproperty of the instance: a.add(num)- add thenumarg to thetotalb.subtract(num)- subtract thenumarg from thetotalc.divide(num)- divide thetotalby thenumarg d.multiply(num)- multiply thetotalby thenumarg
Test your implementation by running the test specs in the test/02-calculator-spec.js file. Run the specs with the following command:
npm test test/02-calculator-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
let calculator = new Calculator();
console.log(calculator.add(50)); // => 50
console.log(calculator.subtract(35)); // => 15
console.log(calculator.multiply(10)); // => 150
console.log(calculator.divide(5)); // => 30
console.log(calculator.total) // => 30Implement the following in the problems/03-make-dog.js file.
- Create a
Dogclass. - The
constructorfunction should take in anameargument and set thenameproperty of a newly instantiated instance to thenameargument. - Add a static method called
makeJet()that will return a new instance of theDogclass with anameproperty of "Jet". - Add two instance methods:
a.
changeName(newName)- set thenameto thenewNameb.speak(word)- returns${name} says ${word}
Test your implementation by running the test specs in the test/03-make-dog-spec.js file. Run the specs with the following command:
npm test test/03-make-dog-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
let dog1 = Dog.makeJet(); // returns an object
console.log(dog1.name); // Jet
console.log(dog1.speak("hello")); // Jet says hello
console.log(dog1.changeName("Freyja")); // Freyja
console.log(dog1.name); // Freyja
console.log(dog1.speak("hello")); // Freyja says hello
let dog2 = Dog.makeJet();
console.log(dog2.name); // JetImplement the following in the problems/04-change-context.js file.
Write a function named changeContext(func, obj) that will accept a
function func and an object obj. The changeContext function should return
the result of the function func invoked with the object obj as its context.
Test your implementation by running the test specs in the test/04-change-context-spec.js file. Run the specs with the following command:
npm test test/04-change-context-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Example:
class Person {
constructor(name) {
this.name = name;
}
}
function extractName() {
return this.name;
}
const kristen = new Person('Kristen');
console.log(changeContext(extractName, kristen)); // => KristenImplement the following in the problems/05-bind-to-an-arg.js file.
Write a function named bindToAnArg(func, arg) that will accept a
function func and any argument arg. The bindToAnArg function should return
the passed-in function func modified to always be invoked with the passed-in
argument arg.
Test your implementation by running the test specs in the test/05-bind-to-an-arg-spec.js file. Run the specs with the following command:
npm test test/05-bind-to-an-arg-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
function add(num1, num2) {
return num1 + num2;
}
const addTwo = bindToAnArg(add, 2);
const addThree = bindToAnArg(add, 3);
const twoPlusSix = addTwo(6);
const twoPlusSeven = addTwo(7);
const threePlusSeven = addThree(7);
const threePlusEight = addThree(8);
console.log({
twoPlusSix, // => 8
twoPlusSeven, // => 9
threePlusSeven, // => 10
threePlusEight // => 11
});function iSpy(thing) {
return "I spy a " + thing;
}
let spyTree = bindToAnArg(iSpy, "tree");
console.log(spyTree()); // => I spy a tree
console.log(spyTree("car")); // => I spy a tree
let spyCar = bindToAnArg(iSpy, "car");
console.log(spyCar()); // => I spy a car
console.log(spyCar("potato")); // => I spy a carImplement the following in the problems/06-fancy-calculator.js file.
- Import the
Calculatorclass. - Create a
FancyCalculatorclass with theCalculatorclass as its parent class. - Add the following instance methods which should all return the reassigned
totalproperty of the instance: a.setTotal(newTotal)- sets thetotalto thenewTotalb.modulo(num)- sets thetotalto the remainder of dividing bynumc.squared()- multiplies thetotalby thetotal
Test your implementation by running the test specs in the test/06-fancy-calculator-spec.js file. Run the specs with the following command:
npm test test/06-fancy-calculator-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
let fancyCalculator = new FancyCalculator();
console.log(fancyCalculator.setTotal(5)); // => 5
console.log(fancyCalculator.squared()); // => 25
console.log(fancyCalculator.modulo(4)); // => 1
console.log(fancyCalculator.total) // => 1
// can use instance methods on the Calculator class
console.log(fancyCalculator.add(9)) // => 10Implement the following in the problems/07-all-the-args.js file.
Write a function named allTheArgs(func, ...args) that will accept a
function func and any number of arguments. Then return the string
"You bowed to" concatenated to the array of args. The allTheArgs function
should return the passed-in function func modified to always be invoked with
the passed-in arguments args. See code block below for further details.
Test your implementation by running the test specs in the test/07-all-the-args-spec.js file. Run the specs with the following command:
npm test test/07-all-the-args-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
function sum(...nums) {
return nums.reduce((acc, num) => acc + num);
}
const onePlusTwo = allTheArgs(sum, 1, 2);
const onePlusTwoPlusThree = onePlusTwo(3);
const onePlusTwoPlusFour = onePlusTwo(4);
console.log(onePlusTwoPlusThree); // => 6
console.log(onePlusTwoPlusFour); // => 7const bow = (...names) => {
return "You bowed to " + names.join(" and ");
};
console.log(bow("Sandy")) // prints "You bowed to Sandy"
let bowSandy = allTheArgs(bow, "Sandy");
console.log(bowSandy()); // prints "You bowed to Sandy"
console.log(bowSandy("Joe", "Nico")); // prints "You bowed to Sandy and Joe and Nico"Implement the following in the problems/08-call-me-later.js file.
- Create a
CallCenterclass. - The
constructorfunction should take in anameargument and set thenameproperty of a newly instantiated instance to thenameargument. - Add two instance methods:
a.
sayHello()- printsHello this is ${name}b.callMeLater(delay)- takes in adelayin milliseconds that will call thesayHello()method on the instance after thedelay. The context of thesayHello()method when invoked needs to be theCallCenterinstance that the method was called on.
You CANNOT use bind, call, or apply for this phase!
Test your implementation by running the test specs in the test/08-call-me-later-spec.js file. Run the specs with the following command:
npm test test/08-call-me-later-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Example 1:
let judy = new CallCenter("Judy");
judy.sayHello(); // prints "Hello this is Judy"
judy.callMeLater(1000); // waits one second then prints "Hello this is Judy"Example 2:
let melan = new CallCenter("Melan");
melan.sayHello(); // prints "Hello this is Melan"
melan.callMeLater(1000); // waits one second then prints "Hello this is Melan"Implement the following in the problems/09-call-on-target.js file.
Write a function named callOnTarget(func, obj1, obj2) that will accept a
function func and two objects, obj1 and obj2. callOnTarget should return
the result of the function func invoked with obj1 as its context and obj2
as the first argument.
Test your implementation by running the test specs in the test/09-call-on-target-spec.js file. Run the specs with the following command:
npm test test/09-call-on-target-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
const cat = {
name: "Breakfast"
};
const mouse = {
name: "Jerry"
};
function greet(other) {
return "I'm " + this.name + ". Nice to meet you, " + other.name;
}
console.log(callOnTarget(greet, cat, mouse));
// "I'm Breakfast. Nice to meet you, Jerry"
console.log(callOnTarget(greet, mouse, cat));
// "I'm Jerry. Nice to meet you, Breakfast"
const dog = {
name: "Noodles",
chase: function(animal) {
return "Woof, my name is " + this.name + " and I'm chasing " + animal.name;
}
};
console.log(callOnTarget(dog.chase, cat, dog));
// "Woof, my name is Breakfast and I'm chasing Noodles"Implement the following in the problems/10-party-planner.js file.
- Create a
PartyPlannerclass. - A newly instantiated instance should have its
guestListproperty initialized to an empty array. - Add two instance methods:
a.
addToGuestList(name)- add thenameto theguestListb.throwParty()- return a different string depending on the length of theguestList:- If there are no guests, return "Gotta add people to the guest list"
- If there are guests in the
guestList, return the guests' names. For example, if there are two guests, return "Welcome to the party ${name1} and ${name2}".
Test your implementation by running the test specs in the test/10-party-planner-spec.js file. Run the specs with the following command:
npm test test/10-party-planner-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Example 1:
const party = new PartyPlanner();
console.log(party.throwParty()); // prints "Gotta add people to the guest list"
party.addToGuestList("James");
console.log(party.throwParty()); // prints "Welcome to the party James"
party.addToGuestList("Alvin");
console.log(party.throwParty()); // prints "Welcome to the party James and Alvin"Example 2:
const party2 = new PartyPlanner();
console.log(party2.throwParty()); // prints "Gotta add people to the guest list"
party2.addToGuestList("Lucy");
console.log(party2.throwParty()); // prints "Welcome to the party Lucy"Implement the following in the problems/11-bind-set-timeout.js file.
Write a function named boundFuncTimer(obj, func, delay) that will accept am
object obj, a function func, and delay which is a number representing
milliseconds. The boundFuncTimer should invoke the function func with obj
as its context after a delay.
Test your implementation by running the test specs in the test/11-bind-set-timeout-spec.js file. Run the specs with the following command:
npm test test/11-bind-set-time-out-spec.jsIn addition to Mocha, you should test your code manually using Node.js. You can use the examples below at the bottom of the file run the file in Node.js.
Examples:
class Animal {
constructor(age) {
this.age = age;
}
growOlder() {
this.age++;
console.log(this.age);
}
}
const dog = new Animal(1);
const cat = new Animal(5);
boundFuncTimer(dog, dog.growOlder, 5000); // in 5 seconds prints: 2
boundFuncTimer(cat, dog.growOlder, 7000); // in 7 seconds prints: 6