diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..6198e8e 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,5 +1,12 @@ function callNoException(f, arg) { + + try { + return f(arg); + } + catch(err) { + return null; + } // if f(arg) throws an exception, return null // otherwise return what f(arg) returned // Example: @@ -12,6 +19,10 @@ function callNoException(f, arg) { } function callNoNull(f, arg) { + if (f(arg) == null) { + throw new Error("f(arg) is not null"); + } + return f(arg); // if f(arg) returns null, throw an exception // otherwise return what f(arg) returned // Example: @@ -26,6 +37,15 @@ function callNoNull(f, arg) { } function exceptionalize(f) { + + var k = function(arg){ + if (f(arg) == null) { + throw new Error("f(arg) is null") + } + return f(arg); + } + return k; + // returns a new function // this function takes 1 input, called arg // if f(arg) is null, this new function throws an exception @@ -43,6 +63,16 @@ function exceptionalize(f) { } function nullify(f) { + + var k = function(arg) { + try { + return f(arg); + } + catch(err) { + return null; + } + } + return k; // returns a new function // this function takes 1 input, called arg // if f(arg) throws an exception, this new function returns null @@ -59,6 +89,14 @@ function nullify(f) { } function map(lst, f) { + + var arr = []; + + for (var i = 0; i < lst.length; i++) { + arr.push(f(lst[i])); + } + + return arr; // lst is an array and f is a function // map returns an array with the same number of elements as lst // if lst = [a1, a2, a3, a4, a5] then map(lst, f) returns [f(a1), f(a2), f(a3), f(a4), f(a5)] @@ -72,6 +110,15 @@ function map(lst, f) { } function filter(lst, f) { + var arr = []; + + for (var i = 0; i < lst.length; i++) { + if (f(lst[i])) { + arr.push(lst[i]) + } + } + + return arr; // lst is an array and f is a function // f takes one argument and returns a boolean (true or false) // filter(lst, f) returns a list with all the elements of lst that does not satisfy f removed @@ -86,6 +133,16 @@ function filter(lst, f) { } function every(lst, f) { + + var out = true; + + for (var i = 0; i < lst.length; i++) { + if (f(lst[i]) == false) { + var out = false; + } + } + + return out; // lst is an array and f is a function // f takes 1 arguments and returns a boolean // filter(lst, f) returns a true if f returns true for every element of lst diff --git a/src/__proto__/problem1.js b/src/__proto__/problem1.js index 820a78d..2a3dbdd 100644 --- a/src/__proto__/problem1.js +++ b/src/__proto__/problem1.js @@ -3,4 +3,6 @@ var parent = {x: 5, y: 6, z: 8}; var child = {x : 10}; +child.__proto__ = parent; + module.exports = {parent, child} \ No newline at end of file diff --git a/src/anonymous-functions/problem1.js b/src/anonymous-functions/problem1.js index ab1384c..f50a40f 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -1,4 +1,5 @@ // fix all the errors + function c(g, h) { var x = g(6); var y = h(8); @@ -6,7 +7,7 @@ function c(g, h) { } function t() { - return c(function g(x) {return y + 1}, function h(y) {return x * 2}); + return c(function(x) {return x + 1}, function(y) {return y * 2}); } -module.exports = t; \ No newline at end of file +module.exports = t; diff --git a/src/arrow-functions/problem1.js b/src/arrow-functions/problem1.js index 5c0873a..a50775a 100644 --- a/src/arrow-functions/problem1.js +++ b/src/arrow-functions/problem1.js @@ -6,7 +6,7 @@ function c(g, h) { } function t() { - return c( x => return y + 2, (x,y) => return x + y); + return c( x => x + 2, (x,y) => x + y); } module.exports = t diff --git a/src/arrow-functions/problem2.js b/src/arrow-functions/problem2.js index 1ae3fdb..ca52600 100644 --- a/src/arrow-functions/problem2.js +++ b/src/arrow-functions/problem2.js @@ -1,9 +1,17 @@ // Convert all the arrow functions to normal anonymous functions // There should be no arrows by the end -var x = x => x + 1; -var y = (x, y) => x + y; -var z = x => {var y = (x * 7) % 2; return y * 2}; +var x = function(x) { + return x + 1; +} + +var y = function(x, y) { + return x + y; +} + +var z = function(x){ + var y = (x * 7) % 2; return y * 2 +} module.exports = {x, y, z}; diff --git a/src/bind/problem1.js b/src/bind/problem1.js index da0b2d6..8db83d3 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -1,7 +1,12 @@ var bob = {name: "Bob"}; + function greet() { return "I'm " + this.name; } + + var greet = greet.bind(bob); + + // bind greet to bob module.exports = greet; \ No newline at end of file diff --git a/src/bind/problem2.js b/src/bind/problem2.js index 079a343..aa26f93 100644 --- a/src/bind/problem2.js +++ b/src/bind/problem2.js @@ -1,9 +1,9 @@ // Fix all the errors in this program var dog = {breed: "schnitzel"}; function greet() { - return "I'm a " + this.bred; + return "I'm a " + this.breed; } -greet.bind(dog); +var greet = greet.bind(dog); module.exports = greet; \ No newline at end of file diff --git a/src/callbacks/problem1.js b/src/callbacks/problem1.js index 0e8eb76..d6a6bbf 100644 --- a/src/callbacks/problem1.js +++ b/src/callbacks/problem1.js @@ -1,7 +1,17 @@ // Fix all the errors. It should print hello after 1 second -function shout(x) { +function shoutLoudly(x) { + console.log(x.toUpperCase()) +} + +function shout() { + return shoutLoudly("hello"); +} + +setTimeout(shout, 1000); + +/* function shout(x) { console.log(x.toUppercase()); } -SetTimeout(shout("hello"), 1); \ No newline at end of file +SetTimeout(shout("hello"), 1); */ \ No newline at end of file diff --git a/src/classes/problem1.js b/src/classes/problem1.js index 8e1bcc9..5466a38 100644 --- a/src/classes/problem1.js +++ b/src/classes/problem1.js @@ -1,4 +1,10 @@ class Dog { + constructor(age, name, breed) { + this.age = age; + this.name = name; + this.breed = breed; + this.bark = () => "woof"; + } // Dog has a constructor with three arguments (in this order): age, name and breed // Dog has three attributes: age, name and breed // Dog has a method bark, which returns a string diff --git a/src/constructor-functions/problem1.js b/src/constructor-functions/problem1.js index b39887c..6f567e0 100644 --- a/src/constructor-functions/problem1.js +++ b/src/constructor-functions/problem1.js @@ -1,2 +1,4 @@ // Add a function to all arrays called isNotEmpty // isNotEmpty returns true is the array is empty, false otherwise + +Array.prototype.isNotEmpty = function(){return this.length != 0}; \ No newline at end of file diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..cd4f62e 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,20 +1,36 @@ function first(arr) { - // Throw an exception if the array has no elements - // Otherwise return the first element + if (arr.length == 0) { + throw new Error("No elements"); + } else { + return arr[0]; + } + } function detective(i) { function suspect(i) { if(i * 7 % 3 == 0) throw new Error("Bad i!"); } - // detective checks to see if the suspect throws an exception on input i. - // Returns "everything ok" if the suspect doesn't. - // Returns "something fishy" if the suspect does. + + try { + suspect(i) + return "everything ok"; + } + catch(e) { + return "something fishy"; + } } function assignFlight(name) { var flightNumber = ((name.length * 7) % 20) + "0"; var terrorSuspects = ["bob", "eric", "susie"]; + + for (var i = 0; i < terrorSuspects.length; i++) { + if (name == terrorSuspects[i]) { + throw new Error("Terrorist!"); + } + return flightNumber; + } // if the name is a terror suspect, throw an exception // Otherwise, return the flight number } diff --git a/src/inheritance/problem1.js b/src/inheritance/problem1.js index 71e66b5..8075582 100644 --- a/src/inheritance/problem1.js +++ b/src/inheritance/problem1.js @@ -4,7 +4,20 @@ class Shape { } } -class Rectangle { +class Rectangle extends Shape { + constructor(width, height) { + super(); + this.width = width; + this.height = height; + } + + area() { + return this.width * this.height; + } + + perimeter() { + return (this.width * 2) + (this.height * 2); + } // A rectangle is a shape // Every rectangle has a width and a height @@ -13,7 +26,12 @@ class Rectangle { // The constructor has two arguments: width and height } -class Square { +class Square extends Rectangle{ + constructor(size) { + super(); + this.width = size; + this.height = size; + } // A square is a rectangle // Every square has a width and a height // The height and width of a square are always the same diff --git a/src/inheritance/problem2.js b/src/inheritance/problem2.js index f74dc6b..c3707d2 100644 --- a/src/inheritance/problem2.js +++ b/src/inheritance/problem2.js @@ -2,24 +2,24 @@ class Shape { constructor(shapeName) { - this.shapName = shapeName; + this.shapeName = shapeName; } toString() { return this.shapeName + " with area " + - this.area() + " and perimeter " + this.permeter(); + this.area() + " and perimeter " + this.perimeter(); } } -class Square { +class Square extends Shape { constructor(size) { - supr("square"); + super("square"); this.size = size; } area() { - return this.size * this.siz; + return this.size * this.size; } perimeter() { - return this.size * 4; + return this.size * 4; } } diff --git a/src/return/problem1.js b/src/return/problem1.js index c8f7b32..b8a3c2f 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,11 @@ -// Remove as many characters from this function without changing its meaning. -// In other words, make this function as succinct as possible -// Also, remove these comments - function f(x) { if(x > 10) { return "hello"; } else if(x > 5) { return "goodbye"; - } else { - return undefined; } } -module.exports = f; // Don't delete this line but remove this comment. +module.exports = f; diff --git a/src/this/problem1.js b/src/this/problem1.js index b7b2740..4abce05 100644 --- a/src/this/problem1.js +++ b/src/this/problem1.js @@ -1,5 +1,11 @@ 'use strict'; function whatsMyAgeAgain() { + try { + return this.age; + } + catch(e) { + return 18; + } // returns this.age unless this is not defined. If this is not defined, return 18 } diff --git a/src/variable-scoping/problem1.js b/src/variable-scoping/problem1.js index da8f429..489af17 100644 --- a/src/variable-scoping/problem1.js +++ b/src/variable-scoping/problem1.js @@ -2,9 +2,9 @@ // It should return a different number every time it is called // The first time it is called it returns 1 // Every call thereafter returns a number one greater than the last +var x = 0; function f() { - var x = 0; x = x + 1; return x; }