Skip to content

Homework #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
57 changes: 57 additions & 0 deletions src/HOF/problem1.js
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)]
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/__proto__/problem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
var parent = {x: 5, y: 6, z: 8};
var child = {x : 10};

child.__proto__ = parent;

module.exports = {parent, child}
5 changes: 3 additions & 2 deletions src/anonymous-functions/problem1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// fix all the errors

function c(g, h) {
var x = g(6);
var y = h(8);
return [x, y];
}

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;
module.exports = t;
2 changes: 1 addition & 1 deletion src/arrow-functions/problem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/arrow-functions/problem2.js
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
5 changes: 5 additions & 0 deletions src/bind/problem1.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/bind/problem2.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 12 additions & 2 deletions src/callbacks/problem1.js
Original file line number Diff line number Diff line change
@@ -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);
SetTimeout(shout("hello"), 1); */
6 changes: 6 additions & 0 deletions src/classes/problem1.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/constructor-functions/problem1.js
Original file line number Diff line number Diff line change
@@ -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};
26 changes: 21 additions & 5 deletions src/exceptions/problem1.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
22 changes: 20 additions & 2 deletions src/inheritance/problem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/inheritance/problem2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/return/problem1.js
Original file line number Diff line number Diff line change
@@ -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;


6 changes: 6 additions & 0 deletions src/this/problem1.js
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/variable-scoping/problem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down