diff --git a/paperproblems/HOF/problem1.txt b/paperproblems/HOF/problem1.txt index 5985535..cb175a1 100644 --- a/paperproblems/HOF/problem1.txt +++ b/paperproblems/HOF/problem1.txt @@ -7,6 +7,8 @@ Specifically: - What does it do? +Filter is a function that takes two parameters. The first is an array, and the second is a function. The filter function goes through every element of the array and returns all elements in a new object that pass the second function's test. + var evenNumbers = [2, 4, 6, 8, 10]; var oddNumbers = [3, 5, 7, 9]; diff --git a/paperproblems/HOF/problem2.txt b/paperproblems/HOF/problem2.txt index 7215f54..47160f4 100644 --- a/paperproblems/HOF/problem2.txt +++ b/paperproblems/HOF/problem2.txt @@ -6,6 +6,9 @@ Specifically: - What are the types of the parameters - What does it do? + +Map is a function that takes two parakeets. The first is an array and the second is function. Map will pass each element in array to the function and return the results in a new array. + var someNumbers = [1, 2, 3, 4]; var someStrings = ["bob", "ERIC"]; diff --git a/paperproblems/HOF/problem3.txt b/paperproblems/HOF/problem3.txt index 46f2ead..279db63 100644 --- a/paperproblems/HOF/problem3.txt +++ b/paperproblems/HOF/problem3.txt @@ -8,4 +8,6 @@ function f(g) { return g(3,5); } -console.log(f(k)); \ No newline at end of file +console.log(f(k)); + +15 \ No newline at end of file diff --git a/paperproblems/HOF/problem4.txt b/paperproblems/HOF/problem4.txt index 6441398..8daa940 100644 --- a/paperproblems/HOF/problem4.txt +++ b/paperproblems/HOF/problem4.txt @@ -12,4 +12,8 @@ function f(g, h) { return g(h, 1, 4); } -console.log(f(k, m)); \ No newline at end of file + + +console.log(f(k, m)); + +Nan \ No newline at end of file diff --git a/paperproblems/__proto__/problem1.txt b/paperproblems/__proto__/problem1.txt index fe263f1..3b0680f 100644 --- a/paperproblems/__proto__/problem1.txt +++ b/paperproblems/__proto__/problem1.txt @@ -1,4 +1,4 @@ -Draw the object diagram for this program + Draw the object diagram for this program var x1 = {}; var x2 = {}; diff --git a/paperproblems/__proto__/problem2.txt b/paperproblems/__proto__/problem2.txt index 699ecd8..8168bac 100644 --- a/paperproblems/__proto__/problem2.txt +++ b/paperproblems/__proto__/problem2.txt @@ -9,9 +9,14 @@ x3.__proto__ = x2; x4.__proto__ = x3; x1.age = 20; -x3.age = 18; +x3.age = 18; console.log(x1.age); console.log(x2.age); console.log(x3.age); -console.log(x4.age); \ No newline at end of file +console.log(x4.age); + +20 +20 +18 +18 \ No newline at end of file diff --git a/paperproblems/__proto__/problem3.txt b/paperproblems/__proto__/problem3.txt index aad4fcb..9e70ec1 100644 --- a/paperproblems/__proto__/problem3.txt +++ b/paperproblems/__proto__/problem3.txt @@ -11,4 +11,6 @@ You get the following exception: a) Explain what this exception means. -b) Why does this exception exist? What's wrong with a cyclic __proto__ value? \ No newline at end of file +b) Why does this exception exist? What's wrong with a cyclic __proto__ value? + +This is a cyclic __proto__ value because the proto of the object which a points to, is being pointed at the proto of b. B is pointing back proto of a, creating a loop that will never end. If you are looking for something in proto of a and it has been pointed at the proto of b b, it then look in the proto of b, if there is nothing there, it would normally then go to the global proto and look there. If it couldn't find it there it would return an error. However if the proto of b is also pointing BACK at a, then it will continue looking forever, never reaching the global proto. \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem1.txt b/paperproblems/anonymous-functions/problem1.txt index 08e09e7..b44515e 100644 --- a/paperproblems/anonymous-functions/problem1.txt +++ b/paperproblems/anonymous-functions/problem1.txt @@ -5,4 +5,13 @@ function greet(x) { console.log("hello " + x); } -greet(bob); \ No newline at end of file +greet("bob"); + + +var greet = function(x){console.log("hello " +x);} + +(function(x) {console.log("hello" + x);})(bob); + + + + diff --git a/paperproblems/anonymous-functions/problem2.txt b/paperproblems/anonymous-functions/problem2.txt index 7cfb344..4ba3732 100644 --- a/paperproblems/anonymous-functions/problem2.txt +++ b/paperproblems/anonymous-functions/problem2.txt @@ -12,3 +12,11 @@ function call(f) { } call(greet); + + +var greet = function(x){console.log("hello " + x);} + +var call = function(f){f("bob")} + + +(function(f){f("bob");})(function(x){console.log("hello " + x);}) diff --git a/paperproblems/anonymous-functions/problem3.txt b/paperproblems/anonymous-functions/problem3.txt index 5d8a0fc..6cf3e25 100644 --- a/paperproblems/anonymous-functions/problem3.txt +++ b/paperproblems/anonymous-functions/problem3.txt @@ -10,3 +10,10 @@ function call(f) { } call(greet); + + +var greet = function (x,y){console.log("hello" + x + " " + y); + +Var call = function (f){f("bob", "dole")}; + +(function(f){f("bob", "dole");})(function(x,y){console.log("hello" + x + " " +y);}) diff --git a/paperproblems/anonymous-functions/problem4.txt b/paperproblems/anonymous-functions/problem4.txt index 689d724..45b6257 100644 --- a/paperproblems/anonymous-functions/problem4.txt +++ b/paperproblems/anonymous-functions/problem4.txt @@ -10,4 +10,11 @@ function twice(f) { f("mary"); } -twice(greet); \ No newline at end of file +twice(greet); + +Var twice = function (f){f("bob");f("Mary");} + +Var greet = function(x) {console.log("hello" + x);} + +(function(f){f("bob");f("Mary");})(function(x) {console.log("hello " + x);}) + diff --git a/paperproblems/array-functions/problem1.txt b/paperproblems/array-functions/problem1.txt index 8e97911..c5cfaaa 100644 --- a/paperproblems/array-functions/problem1.txt +++ b/paperproblems/array-functions/problem1.txt @@ -13,4 +13,6 @@ var dogs = animals.filter( function(animal) {return animal.species === 'dog';}); -console.log(dogs.length); \ No newline at end of file +console.log(dogs.length); + +2 \ No newline at end of file diff --git a/paperproblems/array-functions/problem2.txt b/paperproblems/array-functions/problem2.txt index e9050e5..623ae2b 100644 --- a/paperproblems/array-functions/problem2.txt +++ b/paperproblems/array-functions/problem2.txt @@ -13,4 +13,7 @@ var letterN = animals.filter(function(animal) { return animal.name !== undefined && animal.name[0] === 'N' }); -console.log(letterN[0].name); \ No newline at end of file +console.log(letterN[0].name); + + +Nacho \ No newline at end of file diff --git a/paperproblems/array-functions/problem3.txt b/paperproblems/array-functions/problem3.txt index e1182ab..2ea54be 100644 --- a/paperproblems/array-functions/problem3.txt +++ b/paperproblems/array-functions/problem3.txt @@ -10,4 +10,8 @@ var animals = [ var animalNames = animals.map( function(animal) {return animal.name}); -console.log(animalNames.join()); \ No newline at end of file +console.log(animalNames.join()); + +"Nacho,Ramses,Flufftail,Popcorn,Neckbeard," + +// empty string takes a place in the array, but does not appear in the string exactly, but you know it's there because there is a comma after Neckbeard indicating that neckbeard is not the last element in the array. if the last name were undefined, it would look the same as it does here. \ No newline at end of file diff --git a/paperproblems/array-functions/problem4.txt b/paperproblems/array-functions/problem4.txt index 8b933aa..86f8e03 100644 --- a/paperproblems/array-functions/problem4.txt +++ b/paperproblems/array-functions/problem4.txt @@ -11,8 +11,11 @@ function isDog(animal) { return animal.species === 'dog'; } -animals.some(isDog) +animals.some(isDog) animals.every(isDog) +true +false + diff --git a/paperproblems/arrow-functions/problem1.txt b/paperproblems/arrow-functions/problem1.txt index 7338e61..b82752d 100644 --- a/paperproblems/arrow-functions/problem1.txt +++ b/paperproblems/arrow-functions/problem1.txt @@ -4,16 +4,30 @@ For each of the following expressions: a) x => x + 1 +(no. Input numbers, output numbers) + b) x, y => x * y +Syntax error : if more than one parameter, parentheses required on parameters + c) x => { x * 2 } +(no. inputs number, output number ) + d) (x, z) => {console.log(z); return x * z} +(no. inputs number, output number) + e) x => console.log(z); return x * z +(Syntax error. if more than one line of code, curly braces are required) + f) (x) => x * 2 +(No, input number output number) + e) () => console.log("hello") +no.input? Output = string + When you're done, check all your answers in the developer console. \ No newline at end of file diff --git a/paperproblems/callbacks/problem1.txt b/paperproblems/callbacks/problem1.txt index 63df256..5072973 100644 --- a/paperproblems/callbacks/problem1.txt +++ b/paperproblems/callbacks/problem1.txt @@ -12,4 +12,6 @@ function h() { setTimeout(f, 1000); } -setTimeout(h, 200); \ No newline at end of file +setTimeout(h, 200); + +After 1700ms console.log hello \ No newline at end of file diff --git a/paperproblems/callbacks/problem2.txt b/paperproblems/callbacks/problem2.txt index 1ee06dd..e6c5d5e 100644 --- a/paperproblems/callbacks/problem2.txt +++ b/paperproblems/callbacks/problem2.txt @@ -8,4 +8,6 @@ function h() { setInterval(g, 1000); } -setInterval(h, 1000); \ No newline at end of file +setInterval(h, 1000); + + diff --git a/paperproblems/classes/problem2.txt b/paperproblems/classes/problem2.txt index 60891b1..7f7826b 100644 --- a/paperproblems/classes/problem2.txt +++ b/paperproblems/classes/problem2.txt @@ -12,11 +12,14 @@ class Person { } toString() { return "My name is " + this.name + - "and I'm " + this.age + " and I make " + this.salary; + " and I'm " + this.age + " and I make " + this.salary; } } var bob = new Person(25, "Bob", 40000); bob.increaseSalary(-500); -console.log(bob.toString()); \ No newline at end of file +console.log(bob.toString()); + +"Yay!" +"My name is Bob and I'm 25 and I make 39500" \ No newline at end of file diff --git a/paperproblems/conditional-operator/problem1.txt b/paperproblems/conditional-operator/problem1.txt index fabd9e8..2aac649 100644 --- a/paperproblems/conditional-operator/problem1.txt +++ b/paperproblems/conditional-operator/problem1.txt @@ -4,4 +4,6 @@ function f(x) { return x < 5 ? 3 : x > 8 ? 4 : x == 6 ? 12 : 9; } -console.log(f(5)); \ No newline at end of file +console.log(f(5)); + +9 \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem1.txt b/paperproblems/constructor-functions/problem1.txt index bd77a77..ba29933 100644 --- a/paperproblems/constructor-functions/problem1.txt +++ b/paperproblems/constructor-functions/problem1.txt @@ -1,7 +1,7 @@ Draw the object diagram for the following program. Hints: - - There are two variables: makePerson and bob - - makePerson has a property called prototype + - There are two variables: Person and bob + - Person has a property called prototype - bob has properties constructor and __proto__ function Person(name, age) { diff --git a/paperproblems/constructor-functions/problem2.txt b/paperproblems/constructor-functions/problem2.txt index 4cb7dd9..8af5e5d 100644 --- a/paperproblems/constructor-functions/problem2.txt +++ b/paperproblems/constructor-functions/problem2.txt @@ -12,4 +12,6 @@ var bob = new Person("Bob", 30); Person.prototype.leave = function() {return this.name + " is leaving";} -bob.leave(); \ No newline at end of file +bob.leave(); + +Nothing - need a console.log to print "bob is leaving" \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem3.txt b/paperproblems/constructor-functions/problem3.txt index 2c875ee..8a20df5 100644 --- a/paperproblems/constructor-functions/problem3.txt +++ b/paperproblems/constructor-functions/problem3.txt @@ -16,4 +16,7 @@ Person.prototype.salary = Person.prototype.salary + 300; Person.__proto__.salary = Person.__proto__.salary + 400; bob.salary = bob.salary + 50; -console.log(bob.salary); \ No newline at end of file +console.log(bob.salary); + + +40450 \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem4.txt b/paperproblems/constructor-functions/problem4.txt index 10d57df..deedc9a 100644 --- a/paperproblems/constructor-functions/problem4.txt +++ b/paperproblems/constructor-functions/problem4.txt @@ -12,4 +12,6 @@ var bob = Person("Bob", 30); bob.name = bob.name + " Dole"; -bob.greet(); \ No newline at end of file +bob.greet(); + +error - need New in order to work, otherwise greet is not linked to bob. \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem5.txt b/paperproblems/constructor-functions/problem5.txt index 70770f4..2f1782c 100644 --- a/paperproblems/constructor-functions/problem5.txt +++ b/paperproblems/constructor-functions/problem5.txt @@ -1,6 +1,5 @@ Rewrite this program so that it does't use the new keyword. The program must be equivalent in every other way. -(You would never do this in production. This is for learning purposes) function Person(name, age) { this.name = name; @@ -11,4 +10,20 @@ function Person(name, age) { } var bob = new Person("Bob", 30); -var sue = new Person("Sue", 24); + +// + + + +function Person(name, age){ + var ret= { + name: name, + age: age, + greet: function (){console.log ("hello my name is " + this.name)} +}ret.__proto__=Person.prototype; +Return ret; +} + + +var bob = Person("bob", 30); + diff --git a/paperproblems/exceptions/problem1.txt b/paperproblems/exceptions/problem1.txt index b39b619..5f006b8 100644 --- a/paperproblems/exceptions/problem1.txt +++ b/paperproblems/exceptions/problem1.txt @@ -13,3 +13,5 @@ function g() { } g(); + +500 \ No newline at end of file diff --git a/paperproblems/exceptions/problem2.txt b/paperproblems/exceptions/problem2.txt index 1108a2c..ebb04b0 100644 --- a/paperproblems/exceptions/problem2.txt +++ b/paperproblems/exceptions/problem2.txt @@ -23,3 +23,6 @@ function g() { } g(); + +500 +"some error" \ No newline at end of file diff --git a/paperproblems/expansion/problem1.txt b/paperproblems/expansion/problem1.txt deleted file mode 100644 index 0f38755..0000000 --- a/paperproblems/expansion/problem1.txt +++ /dev/null @@ -1,11 +0,0 @@ -// Expand each of the following -// #1 -console.log(({foo: 6}).foo + [1,2,3].length) -// #2 -(new Array()).concat(5).concat(3).length -// #3 -while([].filter(y => y > 1).length > 3) {} -// #4 -[].concat((() => 4)()) -// #5 -({foo: [4]}).foo[0] + [5,6,10][2] \ No newline at end of file diff --git a/paperproblems/inheritance/problem2.txt b/paperproblems/inheritance/problem2.txt index f9d6898..f31508c 100644 --- a/paperproblems/inheritance/problem2.txt +++ b/paperproblems/inheritance/problem2.txt @@ -11,3 +11,19 @@ class Shape { this.area() + " and perimeter " + this.perimeter(); } } + +class Rectangle extends Shape { + constructor(width, height){ + super ('rectangle'); + this.width = width; + this.height = height; + } + area(){ + return this.height * this.width + } + perimeter(){ + return (this.height*2)+(this.width*2) + } +} +var rec = new Rectangle (3, 4); +console.log(rec.toString()); diff --git a/paperproblems/return/problem1.txt b/paperproblems/return/problem1.txt index fed99b8..d43047f 100644 --- a/paperproblems/return/problem1.txt +++ b/paperproblems/return/problem1.txt @@ -7,3 +7,6 @@ function f() { console.log(f()); + +5 + diff --git a/paperproblems/this/problem1.txt b/paperproblems/this/problem1.txt index 7212ac6..e7e248a 100644 --- a/paperproblems/this/problem1.txt +++ b/paperproblems/this/problem1.txt @@ -9,3 +9,8 @@ function foo() { var obj = {bar: foo, baz: 8}; obj.bar(); +8 + + + + diff --git a/paperproblems/this/problem2.txt b/paperproblems/this/problem2.txt index 1144503..186295f 100644 --- a/paperproblems/this/problem2.txt +++ b/paperproblems/this/problem2.txt @@ -10,3 +10,5 @@ var obj = { var g = obj.bar; g(); +undefined + diff --git a/paperproblems/this/problem3.txt b/paperproblems/this/problem3.txt index dcfda4d..5f13ae6 100644 --- a/paperproblems/this/problem3.txt +++ b/paperproblems/this/problem3.txt @@ -15,3 +15,6 @@ var obj2 = { obj2.bar = obj.bar; obj2.bar(); + + +12 \ No newline at end of file diff --git a/paperproblems/this/problem4.txt b/paperproblems/this/problem4.txt index d7a9ebd..3cb062d 100644 --- a/paperproblems/this/problem4.txt +++ b/paperproblems/this/problem4.txt @@ -11,4 +11,7 @@ function f(g) { console.log(g(4)); } -f(obj.bar); \ No newline at end of file +f(obj.bar); + +NaN +undefined. \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem1.txt b/paperproblems/variable-scoping/problem1.txt index 281bdd6..98b9a50 100644 --- a/paperproblems/variable-scoping/problem1.txt +++ b/paperproblems/variable-scoping/problem1.txt @@ -9,3 +9,7 @@ function f() { f(); f(); + + +2, +4 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem2.txt b/paperproblems/variable-scoping/problem2.txt index eb70c9d..08c3811 100644 --- a/paperproblems/variable-scoping/problem2.txt +++ b/paperproblems/variable-scoping/problem2.txt @@ -7,4 +7,7 @@ function f() { } f(); -f(); \ No newline at end of file +f(); + +2 +2 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem3.txt b/paperproblems/variable-scoping/problem3.txt index ba8ada5..8802c9e 100644 --- a/paperproblems/variable-scoping/problem3.txt +++ b/paperproblems/variable-scoping/problem3.txt @@ -11,4 +11,8 @@ function f() { f(); f(); -console.log(y); \ No newline at end of file +console.log(y); + +7 +9 +3 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem4.txt b/paperproblems/variable-scoping/problem4.txt index 2294b2e..9a3d699 100644 --- a/paperproblems/variable-scoping/problem4.txt +++ b/paperproblems/variable-scoping/problem4.txt @@ -14,4 +14,6 @@ function f() { } } -} \ No newline at end of file +} + +P,k,h,z,y,f,x \ No newline at end of file diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..78f7c64 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,5 +1,15 @@ + function callNoException(f, arg) { + try { + f(arg); + }catch (err){ + return null + } + return f(arg); +} + + // if f(arg) throws an exception, return null // otherwise return what f(arg) returned // Example: @@ -9,9 +19,22 @@ function callNoException(f, arg) { // } // callNoException(throwsZero, 0) returns null // callNoException(throwsZero, 12) returns 12 + +funciton g() { + var ret = f(arg); + if (ret ===null) throw new Error ("oh no"); + return ret; } -function callNoNull(f, arg) { + + function callNoNull(f, arg) { + + if (f(arg) === null){ + throw new Error ("function returns null"); + }else { + return f(arg) +} +} // if f(arg) returns null, throw an exception // otherwise return what f(arg) returned // Example: @@ -22,10 +45,31 @@ function callNoNull(f, arg) { // callNoNull(nullZero, 0) throws an exception // callNoNull(nullZero, 12) returns 12 + + -} function exceptionalize(f) { + function f(arg){ + if(arg==0) return null; + return arg; + } + function g(arg){ + if (f(arg)=== null) throw new Error('is null'); + } + try { + g(arg); + }catch (err){ + return err + } + return f(arg); +} + + + + + + // returns a new function // this function takes 1 input, called arg // if f(arg) is null, this new function throws an exception @@ -40,9 +84,19 @@ function exceptionalize(f) { // g(0) throws an exception // g(12) returns 12 -} function nullify(f) { + + function g(arg) + try { + return f(arg); + }catch (err) { + return null; + } + } + return g; + + // returns a new function // this function takes 1 input, called arg // if f(arg) throws an exception, this new function returns null @@ -56,9 +110,15 @@ function nullify(f) { // g(0) returns null // g(12) throws an exception -} + function map(lst, f) { + newArr = lst.map(function(element){return f(element)}) + return newArr; +} + + + // 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)] @@ -69,9 +129,13 @@ function map(lst, f) { // // function toUpperCase(str) { return str.toUpperCase(); } // map(["bob", "susie"], toUpperCase) returns ["BOB", "SUSIE"] -} +// function f (x){return x % 2 == 0}; function filter(lst, f) { + lst_ = lst.filter(function f (x){return x % 2 !== 0}); + return lst_; + + } // 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 @@ -83,17 +147,21 @@ function filter(lst, f) { // Example: // function isEven(x) {return x % 2 == 0;} // filter([1, 2, 3, 4, 5], isEven) returns [2,4]; -} + function every(lst, f) { + var boo = lst.every(function f(x){return x % 2 == 0}); + return boo; + +} // 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 + // every (lst, f) returns a true if f returns true for every element of lst // Example // every([2,4,12], x => x % 2 == 0) returns true // every([2,3,12], x => x % 2 == 0) returns false -} + module.exports = { 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 c9cdf24..3a230c5 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -5,8 +5,12 @@ function c(g, h) { return [x, y]; } + + function t() { - return c(function (x) {return y + 1}, function (y) {return x * 2}); + return c(function g(x) {return x + 1}, function h(y) {return y * 2}); } -module.exports = t; + + +module.exports = t; \ No newline at end of file diff --git a/src/array-functions/problem1.js b/src/array-functions/problem1.js index 06bc426..bf8f05f 100644 --- a/src/array-functions/problem1.js +++ b/src/array-functions/problem1.js @@ -1,20 +1,34 @@ + + function removeEvens(lst) { + oddArr = lst.filter(x => x % 2 !==0) + return oddArr; + + // lst is an array of numbers // Returns a new list with all the even numbers of lst removed } function keepLong(lst) { + + longLst = lst.filter(x => x.length >5) + return longLst; // lst is an array of strings // Returns a new list with all the elements of lst that are length greater than 5 } function greet(lst) { + helloArr = lst.map(x => "Hello " + x) + return helloArr; // lst is an array of strings // Adds "Hello " to every element of greet // For example: greet(["bob", "eric"]) returns ["Hello bob", "Hello eric"] } function greetLong(lst) { + + longArr = lst.filter(x => x.length >3).map(x => "Hello " + x); + return longArr; // lst is an array of strings // Only greet people who's names have length at least 4. // Otherwise ignore them completely. @@ -22,6 +36,12 @@ function greetLong(lst) { } function allLong(lst) { + return lst.every(x => x.length >4); + + + + + // lst is an array of strings // Returns true if every element of lst is of length at least 5. Otherwise returns false. } 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..012adcd 100644 --- a/src/arrow-functions/problem2.js +++ b/src/arrow-functions/problem2.js @@ -1,9 +1,14 @@ // 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..2b1d1aa 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -2,6 +2,7 @@ var bob = {name: "Bob"}; function greet() { return "I'm " + this.name; } +green = greet.bind(bob); // bind greet to bob -module.exports = greet; \ No newline at end of file +module.exports = green; \ No newline at end of file diff --git a/src/bind/problem2.js b/src/bind/problem2.js index 079a343..fd97b8b 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); +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..fcc1c4e 100644 --- a/src/callbacks/problem1.js +++ b/src/callbacks/problem1.js @@ -1,7 +1,7 @@ // Fix all the errors. It should print hello after 1 second - -function shout(x) { - console.log(x.toUppercase()); +var x = "hello"; +function shout() { + console.log(x.toUpperCase()); } -SetTimeout(shout("hello"), 1); \ No newline at end of file +setTimeout(shout, 1000); \ No newline at end of file diff --git a/src/classes/problem1.js b/src/classes/problem1.js index 8e1bcc9..03ed8c0 100644 --- a/src/classes/problem1.js +++ b/src/classes/problem1.js @@ -1,5 +1,14 @@ class Dog { - // Dog has a constructor with three arguments (in this order): age, name and breed + constructor(age, name, breed){ + this.age = age; + this.name = name; + this.breed = breed; + } + bark(){ + return "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/conditional-operator/problem1.js b/src/conditional-operator/problem1.js index af6e31d..899ecc9 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,23 +1,15 @@ -//Remove the if statements from these functions. +//Remove the statements from these functions. //Replace them with the conditional operator + function desirability(x) { - if(x == 'Brad Pitt') { - return "very desirable"; - } else { - return "not so desirable"; - } -} + return x === "Brad Pitt"? "very desirable": "not so desirable"} function broadenHorizon(x) { - if(x == 'Brad Pitt') { - return "very desirable"; - } else if(x == 'Angelina Jolie') { - return "also desirable"; - } else { - return "not desirable"; - } -} + return x === "Brad Pitt"? "very desirable": x === "Angelina Jolie"? + "also desirable": "not desirable"} + + module.exports = {desirability, broadenHorizon} \ No newline at end of file diff --git a/src/conditional-operator/problem2.js b/src/conditional-operator/problem2.js index 2dd85bf..66e7067 100644 --- a/src/conditional-operator/problem2.js +++ b/src/conditional-operator/problem2.js @@ -1,12 +1,16 @@ -// Remove the conditional operator from these functions -// Replace them with if statements + function iLike(x) { - return x == 'chinese food' ? true : false; + if (x === "chinese food"){return true} + return false; + } function iLikeLessPicky(x) { - return x == 'chinese food' ? true : x == 'french food' ? true : false; + if (x === 'chinese food'){return true} + else if (x === "french food"){return true} + return false; + } module.exports = {iLike, iLikeLessPicky}; \ No newline at end of file diff --git a/src/constructor-functions/problem1.js b/src/constructor-functions/problem1.js index b39887c..0f92fbb 100644 --- a/src/constructor-functions/problem1.js +++ b/src/constructor-functions/problem1.js @@ -1,2 +1,14 @@ // Add a function to all arrays called isNotEmpty // isNotEmpty returns true is the array is empty, false otherwise + + +Array.prototype.isNotEmpty = isNotEmpty + + +function isNotEmpty(){ + if (this.length <1){return false} + return true; +} + + + diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..ba6bf66 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,20 +1,38 @@ function first(arr) { // Throw an exception if the array has no elements // Otherwise return the first element -} - + if (1 > arr.length){ throw new Error("array has no elements");} + else {return arr[0]; + } + } function detective(i) { - function suspect(i) { + function suspect(i) { if(i * 7 % 3 == 0) throw new Error("Bad i!"); + } + try { + suspect(i); + }catch (err){ + return 'something fishy' + } + return "everything ok" } + + + + + + // 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. -} + function assignFlight(name) { var flightNumber = ((name.length * 7) % 20) + "0"; var terrorSuspects = ["bob", "eric", "susie"]; + if (terrorSuspects.includes(name)){ + throw new Error ("ALERT!") + }else {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..44c1e16 100644 --- a/src/inheritance/problem1.js +++ b/src/inheritance/problem1.js @@ -4,7 +4,14 @@ class Shape { } } -class Rectangle { +class Rectangle extends Shape { + constructor(width, height){ + super (); + this.height = height; + this.width = width; + } + area(){return this.height * this.width}; + perimeter(){return (this.height*2)+(this.width*2)} // A rectangle is a shape // Every rectangle has a width and a height @@ -13,7 +20,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 @@ -23,4 +35,5 @@ class Square { } + module.exports = {Shape, Rectangle, Square}; \ No newline at end of file diff --git a/src/inheritance/problem2.js b/src/inheritance/problem2.js index f74dc6b..e5d9f98 100644 --- a/src/inheritance/problem2.js +++ b/src/inheritance/problem2.js @@ -2,21 +2,21 @@ 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; diff --git a/src/return/problem1.js b/src/return/problem1.js index c8f7b32..13e3314 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,17 @@ -// 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..feaa48e 100644 --- a/src/this/problem1.js +++ b/src/this/problem1.js @@ -1,6 +1,17 @@ 'use strict'; function whatsMyAgeAgain() { - // returns this.age unless this is not defined. If this is not defined, return 18 + if (this === undefined){ + return 18; + }else{ + return this.age; + } + + // returns this.age unless this is not defined. + // If this is not defined, + // return 18 } + + + module.exports = whatsMyAgeAgain; \ No newline at end of file diff --git a/src/variable-scoping/problem1.js b/src/variable-scoping/problem1.js index da8f429..2c487c1 100644 --- a/src/variable-scoping/problem1.js +++ b/src/variable-scoping/problem1.js @@ -3,8 +3,8 @@ // The first time it is called it returns 1 // Every call thereafter returns a number one greater than the last -function f() { - var x = 0; +var x = 0; +function f() { x = x + 1; return x; }