Commit 3be5b7dc authored by Kristian Freeman's avatar Kristian Freeman

2017-08-18

parent 48866587
.DS_Store
/*
* Hints:
*
* Arrays are sets of data. They're defined using bracket-syntax, like this:
*
* var array = [1, 2, 3];
*
* An object is a data structure with key and value. A simple object looks like this:
*
* var instructor = {
* name: "Kristian"
* };
*
* Object values can be referenced by their key. Using the above object, we can get `name`:
*
* console.log(instructor.name)
*
* An array can contain objects. A simple example (formatted across multiple lines for ease
* of reading) might look like:
*
* var array = [
* {
* value: 1
* },
* {
* value: 2
* }
* ]
*
*/
// No changes are needed above this line.
/*
* Exercise goals:
* 1. Define an array (`cities`) of city objects.
* 2. A city object has a single key `name`, with a string value.
* 3. The tests will look at the `cities` variable for the presence of at least two objects, and
* log the `name` value of each.
*/
var cities = [
];
// No changes are needed below this line.
const assert = require("assert");
assert.notEqual(cities.length, 0, "The cities variable should have at least one object inside of it");
assert.notEqual(cities.length, 1, "Try defining more than one city object inside of the cities array");
cities.forEach(function(city) {
assert.notEqual(city.name, undefined, "A city object inside of cities must have a name value")
console.log(city.name);
});
console.log("Passed! :)");
const assert = require("assert");
// No changes are needed above this line.
// Define a cities array that contains the cities
// Phoenix, Los Angeles, and Seattle.
var cities;
var citiesString = cities.map(function(city) {
// What should map return here to allow the
// below "assert" check to be true?
return // ???
}).join(", ");
assert.equal(citiesString, "Phoenix, Los Angeles, Seattle")
// No changes are needed below this line.
assert.notEqual(cities.length, 0, "The cities variable should have at least one object inside of it");
assert.notEqual(cities.length, 1, "Try defining more than one city object inside of the cities array");
assert.equal(cities.find(c => c === "Phoenix", "Phoenix", "The cities array should include Phoenix");
console.log("Passed! :)");
class Car {
constructor() {
/*
* Add the following attributes and their default values:
* - peopleInCar, which stores the number of people in the
* car. This should default to 0.
* - seats, which stores the number of available seats in the
* car. This should default to 4.
* - gas, which stores the percentage of gas available. This
* defaults to 100.
* - mileage, which stores the number of miles driven. This
* defaults to 0.
*/
}
/*
* Define the drive function, which subtracts 1 from gas, and
* adds 1 to mileage.
*/
drive() {
}
addPerson() {
if (this.peopleInCar == this.seats) {
return false;
} else {
this.peopleInCar += 1;
return true;
}
}
removePerson() {
if (this.peopleInCar > 0) {
this.peopleInCar -= 1;
return true;
} else {
return false;
}
}
}
class SportsCar extends Car {
constructor() {
super()
/*
* Define gas to 50.
* Define seats to 2.
*/
}
/*
* Consider the following functions, addPerson and removePerson.
* If they simply call the "super" function, do they need to be
* defined?
*
* Try removing them and re-running the tests to see if it makes
* a difference.
*/
addPerson() {
super.addPerson();
}
removePerson() {
super.removePerson();
}
/*
* A sports car uses more gas than a normal car.
* Define this function so that it subtracts 5 gas units instead
* of 1.
*
* For a bonus assignment, consider adding additional logic, so that if
* the amount of gas available is 0, the car is unable to drive
* (return false).
*/
drive() {
super.drive();
}
}
class ElectricCar extends Car {
constructor() {
super()
/*
* An electric car uses a battery instead of gas.
* Define this.battery and default to 100.
*/
}
/*
* An electric car uses a battery instead of gas.
* Define this function so that it subtracts 1 battery unit instead
* of a gas unit.
*/
drive() {
super.drive();
}
}
// No changes are needed below this line.
const assert = require("assert");
var standard = new Car();
assert.equal(standard.peopleInCar, 0, "A Car should have 0 peopleInCar.");
assert.equal(standard.seats, 4, "A Car should have 4 seats.");
assert.equal(standard.gas, 100, "A Car should start with 0 gas.");
assert.equal(standard.mileage, 0, "A Car should start with 0 mileage.");
standard.addPerson()
assert.equal(standard.peopleInCar, 1, "Calling addPerson() should add 1 to peopleInCar.");
standard.addPerson()
assert.equal(standard.peopleInCar, 2, "Calling addPerson() should add 1 to peopleInCar.");
standard.addPerson()
standard.addPerson()
assert.equal(standard.peopleInCar, 4, "Calling addPerson() should add 1 to peopleInCar.");
standard.addPerson()
assert.equal(standard.peopleInCar, 4, "Calling addPerson() should add 1 to peopleInCar.");
standard.removePerson()
standard.removePerson()
standard.removePerson()
assert.equal(standard.peopleInCar, 1, "Calling removePerson() should subtract 1 from peopleInCar.");
standard.removePerson()
assert.equal(standard.peopleInCar, 0, "Calling removePerson() should subtract 1 from peopleInCar.");
standard.removePerson()
assert.equal(standard.peopleInCar, 0, "Calling removePerson() when no one is the car should do nothing.");
standard.drive()
assert.equal(standard.gas, 99, "Calling drive() should remove 1 from gas.");
assert.equal(standard.mileage, 1, "Calling drive() should add 1 to mileage.");
var sports = new SportsCar();
assert.equal(sports.gas, 50, "The SportsCar class should start with 50 gas.");
sports.drive()
assert.notEqual(sports.gas, 44,
"The method drive() for SportsCar removed 6 gas, instead of 5. Note that since the parent class Car removes 1 gas during the drive() method, you should subtract 4 gas instead of 5.");
assert.equal(sports.gas, 45, "Calling drive() on SportsCar should remove 5 gas.");
var electric = new ElectricCar();
electric.drive()
assert.equal(electric.battery, 99, "Calling drive() on ElectricCar should remove 1 battery unit.");
// assert.notEqual(electric.gas, 99, "The electric car still has a gas value that is being used by `.drive`.");
console.log("Passed! :)");
const assert = require("assert");
var integers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// No changes are needed above this line.
/*
* Progressively filtering an array simply means
* calling `.filter` on a succession of arrays.
*
* For instance, we might define an array as:
* var array = [1, 2, 3];
*
* Filtering on that array and capturing the value would mean:
* var newArray = array.filter(function(num) {
* return num != 1;
* });
*
* To "progressively" filter down our sets, we might filter on
* newArray, thus "combining" filters:
*
* var newerArray = newArray.filter(function(num) {
* return num != 2;
* });
*/
var notZero;
var lessThanFive;
/*
* As with many programming languages, determing whether a number
* is even or odd in JavaScript can be done with the % operator.
* This operator calculates the "remainder" of a division:
*
* 2 % 2 == 0, because there is no left over values from 2 / 2.
* 3 % 2 == 1, because there is a 1 left over values from 3 / 2.
*
* Therefore, an odd number can be checked using `x % 2 == 1`
*/
var oddNumbers;
var greaterThanOne;
/*
* What do you expect the final value to be? Can you define it as a
* number below? If you aren't sure, try running the tests while
* logging (console.log) `greaterThanOne`.
*/
var finalValue;
// No changes are needed below this line.
assert.equal(integers.length, 10, "The integers array shouldn't be changed from its original implementation");
assert.equal(notZero.length, 9, "The notZero array should include every number that isn't equal to zero");
assert.equal(lessThanFive.length, 4, "The lessThanFive array should include every number less than five, and should also be filtered from notZero");
assert.equal(oddNumbers.length, 2, "The oddNumbers array should include every odd number in the set, and should be filtered from lessThanFive");
assert.equal(greaterThanOne.length, 1, "The greaterThanOne array should include every number greater than one, and should be filtered from oddNumbers");
assert.equal(finalValue, 3, "The final value should just be 3");
console.log("Passed! :)");
default:
node control-flow.js
node hello-world.js
var didSomethingElse = false;
var doSomethingElse = () => { didSomethingElse = true };
var ateFood = false;
var eatFood = () => { ateFood = true };
// No changes are needed above this line.
/*
* If/else allows multiple "paths" to be taken in code.
*
* var thirsty = false;
* if (thirsty) {
* console.log("I am thirsty");
* } else {
* console.log("I am not thirsty");
* }
*
* In the below example, the variable `hungry` is set to true.
* Using an if/else statement, ensure the following things happen:
* 1. If hungry, call the function eatFood: `eatFood()`
* 2. If hungry, set the value `hungry` to `false`.
* 3. If not hungry, call the function doSomethingElse: `doSomethingElse()`.
*/
var hungry = true;
/*
* What value should replace `false` in the `if` section of this code?
*/
if (false) {
// What TWO things should happen if the `if` expression is true?
} else {
// What ONE thing should happen if the `if` expression is false?
}
// No changes are needed below this line.
const assert = require("assert");
assert.equal(ateFood, true, "The function `eatFood` should have been called in the if block of the code.");
assert.equal(hungry, false, "hungry should be false.");
assert.equal(didSomethingElse, false, "The function `didSomethingElse` shouldn't be called: make sure that it exists in the else block of the code.");
console.log("Passed! :)");
var helloFunction = function(name) {
return "Hello " + name;
};
// No changes are needed above this line.
/*
* Variables can be set using the "var" keyword in JavaScript.
* Variables can be simply declared, such as `var me`, which initializes
* the variable `me` with no value.
*
* Variables can also be set to a value using the `=` syntax, such as:
*
* var age = 99;
*
* In doing so, the `age` variable now has a value of 99.
*
*/
var me;
// No changes are needed below this line.
const assert = require("assert");
assert.notEqual(me, undefined, "The me variable should be set to a string of your name, such as:\n\nvar me = 'NAME';\n");
console.log(helloFunction(me));
console.log("Passed! :)");
<html>
<head>
<!-- These lines can be ignored ...-->
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
<!-- These lines can be ignored ...-->
</head>
<body>
<div>
<input id="one" /> +
<input id="two" /> =
<span id="equals"></span>
</div>
<!-- These lines can be ignored ...-->
<div id="mocha"></div>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script>
try {
var sumToSpan = function() {
var elem = document.getElementById("equals");
if (firstValue && secondValue) {
var newSum = Number(firstValue) + Number(secondValue);
if (!isNaN(newSum)) { elem.innerText = newSum }
} else {
elem.innerText = ""
}
}
<!-- These lines can be ignored ...-->
// These values do _not_ need to be set: they'll be
// used later in event handlers.
var firstValue;
var secondValue;
/*
* Use document.getElementById to assign firstInput
* to the input with id "one".
*/
var firstInput;
firstInput.addEventListener('input', function() {
firstValue = firstInput.value;
sumToSpan();
})
/*
* Use document.getElementById to assign secondInput
* to the input with id "two".
*/
var secondInput;
/*
* Use secondInput.addEventListener with the "input" event to
* to set the _value_ of secondInput to secondValue.
*/
/*
* Use document.getElementById to assign equalsField
* to the span with id "equals".
*/
var equalsField;
/*
* You might expect a third event handler here, considering
* we're working with three elements.
*
* Surprise! Instead of defining a third event handler,
* we're going to simply call a pre-defined function on the
* previous event handler.
* Adding `sumToSpan()` to each of the two input handlers
* will call sumToSpan, which takes the numeric values of
* both inputs and sets the span "#equals" to that value.
*/
<!-- no changes are needed below this line -->
} catch (e) {
// Your code might not be ready yet - we do try/catch here so that the tests can still run.
}
describe("DOM element tests", function() {
it("updates the value of the first input", function() {
var _one = document.getElementById('one');
_one.value = 1;
expect(firstInput.value).to.be(_one.value);
_one.value = null;
})
it("updates the value of the second input", function() {
var _two = document.getElementById('two');
_two.value = 2;
expect(secondInput.value).to.be(_two.value);
_two.value = null;
})
it("correctly updates the third input", function() {
var _one = document.getElementById('one');
_one.value = 3;
var _two = document.getElementById('two');
_two.value = 4;
var inputEvt = new Event('input');
_one.dispatchEvent(inputEvt);
_two.dispatchEvent(inputEvt);
var _three = document.getElementById('equals');
expect(_three.innerText).to.be("7");
_one.value = null;
_two.value = null;
_one.dispatchEvent(inputEvt);
_two.dispatchEvent(inputEvt);
})
})
</script>
<!-- These lines can be ignored ...-->
<script>
mocha.checkLeaks();
mocha.run();
</script>
<!-- These lines can be ignored ...-->
</body>
</html>
default:
node print-yourself.js
/*
* Hints:
*
* Functions are pieces of JavaScript code that can be defined and later executed.
* Functions are defined as such:
*
* function() {
* }
*
* Functions can accept arguments, which can be used inside the function body:
*
* function(anArg) {
* console.log(anArg);
* }
*
* Functions can be named and referred to later by that name:
*
* function namedFunc() {
* }
*
* A function can be executed using the () syntax:
*
* namedFunc();
*
* If a function has arguments, they can be provided in the () syntax:
*
* function namedFunc(arg) {
* }
*
* namedFunc(1);
*
* Functions often "return" values, in many cases based on the arguments they receive:
*
* function double(number) {
* return number * 2;
* }
*
* double(3); // 6
*
*/
// No changes are needed above this line.
/*
* Exercise goals:
* 1. Define a `me` variable with your first name.
* 2. Define a named function called `printMe` with a `name` argument.
* 3. In the function body of `printMe`, return the string "I am <name argument>".
*/
var me;
// No changes are needed below this line.
const assert = require("assert");
assert.notEqual(me, undefined, "The me variable should be set to a string of your first name, such as:\n\nvar me = 'NAME';\n");
assert.notEqual(printMe, undefined, "The function printMe should be defined");
var funcResponse = printMe(me);
assert.notEqual(funcResponse, "I am" + me,
"Adding strings together often requires a space: your function returns 'I am<name> instead of 'I am <name>'. Try adding a space at the end of 'I am'."
);
assert.equal(funcResponse, "I am " + me, "The function printMe should return a string in the format 'I am <me>'");
console.log(printMe(me));
console.log("Passed! :)");
/*
* Hints:
*
* JavaScript "scope" is the "view" that the function or currently executing code has.
* This includes what other variables and functions are available to be referenced in the code.
* We often refer to the current context as `this` -- the actual value of `this` can be a source
* of confusion for many JavaScript developers.
*
* For instance, in the below example, calling the `findName` function, which looks for the `name`
* value on the context `this`, can either refer to a top-level (global) variable, _or_ the key on
* an object, depending on what context the function is called in.
*
* var name = "JavaScript";
* var me = { name: "Kristian" };
*
* function findName() {
* console.log(this.name);
* }
*
* findName(); // "JavaScript"
* findName.call(me); // "Kristian"
*
* The `.bind` function can permanently change the scope of a function to a specific context.
* Below, `.bind` sets the context of `findName` to always refer to the `me` object:
* Note that we re-assign the value of `findName` to the response of `findName.bind`.
*
* var name = "JavaScript";
* var me = { name: "Kristian" };
*
* function findName() {
* console.log(this.name);
* }
*
* findName = findName.bind(me);
* findName() // "Kristian"
*
*/
// This line can be ignored! It's used to set up a context inside of the Node/JS intepreter.
(function() {
this.name = "JavaScript";
this.age = 22;
function printNameAndAge() {
return this.name + " is " + this.age;
}
// No changes are needed above this line.
// Exercise goals have been in-lined throughout the code:
// Goal #1: Define a `me` object that contains the keys `name` and `age`.
var me;
/*
* Goal #2:
*
* This variable should be updated either:
* 1. Calling `printNameAndAge` with the context `me`
* 2. Binding `printNameAndAge` to the context `me`.
* Note that if you bind the function, you'll need to also call it _after_
* binding it, by adding an additional set of parentheses `()` at the end.
* 3. (bonus: get the tests to pass with both!)
*/
var correctlyBound = printNameAndAge();
// No changes are needed below this line.
const assert = require("assert");
assert.notEqual(typeof correctlyBound, 'function', "correctlyBound is currently a function -- this means that you haven't correctly bound it to a context. Ensure that the variable definition ends with open and closed parentheses in some format.");
assert.notEqual(correctlyBound, "JavaScript is 22", "The context of function `printNameAndAge` is currently top-level, and returning the string 'JavaScript is 22'. Instead, either call the function with the context of your 'me' object, or permanently bind it to 'me'.");
console.log(correctlyBound);
console.log("Passed! :)");
// This line can also be ignored.
})()