diff --git a/paperproblems/array-functions/problem1.txt b/paperproblems/array-functions/problem1.txt index 8e97911..a79f195 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..2378ffc 100644 --- a/paperproblems/array-functions/problem2.txt +++ b/paperproblems/array-functions/problem2.txt @@ -13,4 +13,6 @@ 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..430f697 100644 --- a/paperproblems/array-functions/problem3.txt +++ b/paperproblems/array-functions/problem3.txt @@ -10,4 +10,6 @@ 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, \ No newline at end of file diff --git a/paperproblems/array-functions/problem4.txt b/paperproblems/array-functions/problem4.txt index 8b933aa..ae47d6a 100644 --- a/paperproblems/array-functions/problem4.txt +++ b/paperproblems/array-functions/problem4.txt @@ -11,8 +11,8 @@ function isDog(animal) { return animal.species === 'dog'; } -animals.some(isDog) -animals.every(isDog) +animals.some(isDog) //true +animals.every(isDog) //false diff --git a/paperproblems/arrow-functions/problem1.txt b/paperproblems/arrow-functions/problem1.txt index 7338e61..1604e4f 100644 --- a/paperproblems/arrow-functions/problem1.txt +++ b/paperproblems/arrow-functions/problem1.txt @@ -2,18 +2,18 @@ For each of the following expressions: - Does it have a syntax error? - If it doesn't have a syntax error, what are the probable input and output types of the function? -a) x => x + 1 +a) x => x + 1 //no syntax error, input type: number, output type: number -b) x, y => x * y +b) x, y => x * y //need () for the arguments, input type: number, output type: number -c) x => { x * 2 } +c) x => { x * 2 } //need no {} OR a return before x*2, input type: number, output type: number -d) (x, z) => {console.log(z); return x * z} +d) (x, z) => {console.log(z); return x * z} //no syntax error, input type: number, output type: number -e) x => console.log(z); return x * z +e) x => console.log(z); return x * z //need {} andz must be defined, input type: number, output type: number -f) (x) => x * 2 +f) (x) => x * 2 //ok but you remove () for the parameter, input type: number, output type: number -e) () => console.log("hello") +e) () => console.log("hello") //no syntax error, input type: none, output type: 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..07631a5 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); + +//This programme log "Hello!" after 1700 ms \ No newline at end of file diff --git a/paperproblems/callbacks/problem2.txt b/paperproblems/callbacks/problem2.txt index 1ee06dd..8e51fa2 100644 --- a/paperproblems/callbacks/problem2.txt +++ b/paperproblems/callbacks/problem2.txt @@ -8,4 +8,9 @@ function h() { setInterval(g, 1000); } -setInterval(h, 1000); \ No newline at end of file +setInterval(h, 1000); + +//This program calls h() every second +//h() calls g() every second +//g() logs "Hello!" +//There is an "Hello!" more at each call \ No newline at end of file diff --git a/paperproblems/classes/problem1.txt b/paperproblems/classes/problem1.txt index 9fbc41e..2a0b610 100644 --- a/paperproblems/classes/problem1.txt +++ b/paperproblems/classes/problem1.txt @@ -11,4 +11,4 @@ class Dog { } } -var someDog = new Dog("schnitzel", 2, "male"); +var someDog = new Dog("schnitzel", 2, "male"); \ No newline at end of file diff --git a/paperproblems/classes/problem2.txt b/paperproblems/classes/problem2.txt index 60891b1..8a2ca81 100644 --- a/paperproblems/classes/problem2.txt +++ b/paperproblems/classes/problem2.txt @@ -12,11 +12,15 @@ 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()); + +//output: +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..09083b8 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)); + +//output : 9 \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem2.txt b/paperproblems/constructor-functions/problem2.txt index 4cb7dd9..bbe1ef5 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(); + +//output "Bob is leaving" because the property leave is correctly added to the prototype of the function Person \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem3.txt b/paperproblems/constructor-functions/problem3.txt index 2c875ee..e4127f4 100644 --- a/paperproblems/constructor-functions/problem3.txt +++ b/paperproblems/constructor-functions/problem3.txt @@ -16,4 +16,6 @@ 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); + +// output : 40450 because line 16 needs a "prototype" instead of "__proto__" \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem4.txt b/paperproblems/constructor-functions/problem4.txt index 10d57df..720fc18 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(); + +//output: error because the var bob need to use the constructor to define all its properties : var bob = new Person("Bob", 30) \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem5.txt b/paperproblems/constructor-functions/problem5.txt index 70770f4..f4642c0 100644 --- a/paperproblems/constructor-functions/problem5.txt +++ b/paperproblems/constructor-functions/problem5.txt @@ -11,4 +11,13 @@ function Person(name, age) { } var bob = new Person("Bob", 30); + + +function person(name, age) { + return {fname: name, age: age, greet: function(){console.log("Hello my name is " + name)}} +} + +var bob = person('Bob', 30) + var sue = new Person("Sue", 24); + diff --git a/paperproblems/inheritance/problem1.txt b/paperproblems/inheritance/problem1.txt index 3de989f..1cf3156 100644 --- a/paperproblems/inheritance/problem1.txt +++ b/paperproblems/inheritance/problem1.txt @@ -24,4 +24,6 @@ class Square extends Shape { } var s = new Square(5); -console.log(s.toString()); \ No newline at end of file +console.log(s.toString()); + +//output : square with area 25 and perimeter 20 \ No newline at end of file diff --git a/paperproblems/inheritance/problem2.txt b/paperproblems/inheritance/problem2.txt index f9d6898..b378c8d 100644 --- a/paperproblems/inheritance/problem2.txt +++ b/paperproblems/inheritance/problem2.txt @@ -11,3 +11,20 @@ class Shape { this.area() + " and perimeter " + this.perimeter(); } } + +class Rectangle extends Shape { + constructor(height, width) { + super("rectangle"); + this.height = height; + this.width = width; + } + area() { + return this.height * this.width; + } + perimeter() { + return this.height * 2 + this.width * 2; + } +} + +var s = new Rectangle(5,3); +console.log(s.toString()); \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem1.txt b/paperproblems/variable-scoping/problem1.txt index 281bdd6..17e7be8 100644 --- a/paperproblems/variable-scoping/problem1.txt +++ b/paperproblems/variable-scoping/problem1.txt @@ -9,3 +9,7 @@ function f() { f(); f(); + +//output: +2 +4 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem2.txt b/paperproblems/variable-scoping/problem2.txt index eb70c9d..94676fc 100644 --- a/paperproblems/variable-scoping/problem2.txt +++ b/paperproblems/variable-scoping/problem2.txt @@ -7,4 +7,8 @@ function f() { } f(); -f(); \ No newline at end of file +f(); + +output: +2 +2 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem3.txt b/paperproblems/variable-scoping/problem3.txt index ba8ada5..18da6dc 100644 --- a/paperproblems/variable-scoping/problem3.txt +++ b/paperproblems/variable-scoping/problem3.txt @@ -11,4 +11,10 @@ function f() { f(); f(); -console.log(y); \ No newline at end of file +console.log(y); + +//output: + +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..ff56505 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 +} + +//variables in scope: f, y, g, z, h and p \ No newline at end of file diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..9790105 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,18 +1,25 @@ - function callNoException(f, arg) { - // if f(arg) throws an exception, return null - // otherwise return what f(arg) returned - // Example: - // function throwsZero(x){ - // if(x==0) throw new Error("woops"); - // return x; - // } - // callNoException(throwsZero, 0) returns null - // callNoException(throwsZero, 12) returns 12 + try { + f(arg); + return f(arg); + } + catch(err) { + return null; + } } -function callNoNull(f, arg) { - // if f(arg) returns null, throw an exception +// if f(arg) throws an exception, return null +// otherwise return what f(arg) returned +// Example: +// function throwsZero(x){ +// if(x==0) throw new Error("woops"); +// return x; +// } +// callNoException(throwsZero, 0) returns null +// callNoException(throwsZero, 12) returns 12 + +// +// if f(arg) returns null, throw an exception // otherwise return what f(arg) returned // Example: // function nullZero(x) { @@ -21,11 +28,66 @@ function callNoNull(f, arg) { // } // callNoNull(nullZero, 0) throws an exception // callNoNull(nullZero, 12) returns 12 +function callNoNull(f, arg) { + var ret = f(arg); + if(ret === null) throw new Error("oh no!"); + return ret +} + + + +/* si callNoNull retourne une fonction : +function callNoNull(f, arg) { + function g() { + var ret = f(arg); + if(ret === null) throw new Error("oh no!"); + return ret + } + return g; +} +*/ + +/* +function callNoNull(f, arg) { + if(f(arg) === null) throw "arg is null"; + return f(arg); + } +function nullZero(x) { + if(x==0) return null; + return x; +} +try { + callNoNull(nullZero, 12) + } catch(err) { + console.log(err) } +*/ + +// +function exceptionalize(f) { + return function(arg) { + if (f(arg) == null) {throw 'arg is null';} + else return f(arg); + } +} + +/*function exceptionalize(f) { + return f(); +} + +function nullZero(x) { + if(x==0) return null; + return x; +} + +function g(arg) { + if(nullZero(arg) === "null") throw "arg is null"; + return g(arg) +} +*/ -function exceptionalize(f) { // returns a new function // this function takes 1 input, called arg // if f(arg) is null, this new function throws an exception @@ -40,8 +102,8 @@ function exceptionalize(f) { // g(0) throws an exception // g(12) returns 12 -} +// function nullify(f) { // returns a new function // this function takes 1 input, called arg @@ -55,23 +117,42 @@ function nullify(f) { // nullify(throwsZero) returns a function g such that // g(0) returns null // g(12) throws an exception - + function g(arg) { + try { + return f(arg); + } catch (err) { + return null; + } + } + return g; } + +// function map(lst, f) { - // 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)] - // map returns a new array created by applying f - // to the elements of the original array - // - // Example - // - // function toUpperCase(str) { return str.toUpperCase(); } - // map(["bob", "susie"], toUpperCase) returns ["BOB", "SUSIE"] + var ret = []; + for (index in lst) { + ret.push(f(lst[index])); + } + return ret; + } + + function toUpperCase(str) { return str.toUpperCase(); } + map(["bob", "susie"], toUpperCase) + + + // + + function filter(lst, f) { + var arr = []; + for(var i = 0; i < lst.length; i++) { + if(f(lst[i]) == true) { + arr.push(lst[i]); + } + } + return arr; } -function filter(lst, f) { // 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 @@ -79,11 +160,10 @@ function filter(lst, f) { // if lst_ = filter(lst, f) and x is an element of lst_ it means that: // x is an element of lst // f(x) is true - // + // Example: // function isEven(x) {return x % 2 == 0;} // filter([1, 2, 3, 4, 5], isEven) returns [2,4]; -} function every(lst, f) { // lst is an array and f is a function @@ -92,7 +172,14 @@ function every(lst, f) { // Example // every([2,4,12], x => x % 2 == 0) returns true - // every([2,3,12], x => x % 2 == 0) returns false + // every([2,3,12], x => x % 2 == 0) returns false + for(var i = 0; i < lst.length; i++) { + if(f(lst[i]) === false) { + return false; + } + + } + return true; } @@ -104,4 +191,4 @@ module.exports = { map, filter, every -}; +}; \ No newline at end of file diff --git a/src/__proto__/problem1.js b/src/__proto__/problem1.js index 820a78d..540455e 100644 --- a/src/__proto__/problem1.js +++ b/src/__proto__/problem1.js @@ -1,6 +1,6 @@ // Make parent the __proto__ of child var parent = {x: 5, y: 6, z: 8}; -var child = {x : 10}; +var child = {x : 10, __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..4e06179 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -6,7 +6,9 @@ function c(g, h) { } 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; diff --git a/src/array-functions/problem1.js b/src/array-functions/problem1.js index 06bc426..103eb67 100644 --- a/src/array-functions/problem1.js +++ b/src/array-functions/problem1.js @@ -1,20 +1,38 @@ function removeEvens(lst) { + var arr = []; + for (var i = 0; i < lst.length; i++) { + if(lst[i] % 2 === 1) arr.push(lst[i]) + } + return arr; // lst is an array of numbers // Returns a new list with all the even numbers of lst removed } function keepLong(lst) { + var arr = []; + for (var i = 0; i < lst.length; i++) { + if(lst[i].length > 5) arr.push(lst[i]) + } + return arr; // 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) { + var x = lst.map(function(i) {return "Hello " + i}); + return x; + } // 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) { + var arr = []; + for (var i = 0; i < lst.length; i++) { + if(lst[i].length > 3) arr.push(lst[i]) + } + var x = arr.map(function(i) {return "Hello " + i}); + return x // lst is an array of strings // Only greet people who's names have length at least 4. // Otherwise ignore them completely. @@ -22,6 +40,9 @@ function greetLong(lst) { } function allLong(lst) { + for (i of lst) { + return i.length >= 5 ? true : false; + } // 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..91f9741 100644 --- a/src/arrow-functions/problem2.js +++ b/src/arrow-functions/problem2.js @@ -1,9 +1,16 @@ // 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 = 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..91594a8 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -1,7 +1,7 @@ var bob = {name: "Bob"}; -function greet() { +var greet = function() { return "I'm " + this.name; } // bind greet to bob - +greet = greet.bind(bob); module.exports = greet; \ No newline at end of file diff --git a/src/bind/problem2.js b/src/bind/problem2.js index 079a343..15d7f8f 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; +var greet = function() { + 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..9f04106 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()); + console.log(x.toLowerCase()); } -SetTimeout(shout("hello"), 1); \ No newline at end of file +setTimeout(shout, 1000, "hello"); \ No newline at end of file diff --git a/src/classes/problem1.js b/src/classes/problem1.js index 8e1bcc9..1924c49 100644 --- a/src/classes/problem1.js +++ b/src/classes/problem1.js @@ -1,4 +1,12 @@ class Dog { + 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..ed2f727 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,23 +1,11 @@ -//Remove the if 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 +module.exports = {desirability, broadenHorizon} diff --git a/src/conditional-operator/problem2.js b/src/conditional-operator/problem2.js index 2dd85bf..a238e42 100644 --- a/src/conditional-operator/problem2.js +++ b/src/conditional-operator/problem2.js @@ -2,11 +2,14 @@ // Replace them with if statements function iLike(x) { - return x == 'chinese food' ? true : false; + if(x == 'chinese food') return true + else 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 + else 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..8d74d34 100644 --- a/src/constructor-functions/problem1.js +++ b/src/constructor-functions/problem1.js @@ -1,2 +1,8 @@ // Add a function to all arrays called isNotEmpty // isNotEmpty returns true is the array is empty, false otherwise + +var x = new Array(); +Array.prototype.isNotEmpty = function() { + if (this.length !== 0) return true; + else return false; +} diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..5b631fd 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,22 +1,46 @@ function first(arr) { + if(Array.isArray(arr) === false) { + throw new Error("it's not an array") + } else if(arr.length === 0) { + throw new Error("empty array"); + } else return arr[0] + } + try { + first([1,2]) + } catch(err) { + console.log(err) + } // Throw an exception if the array has no elements // Otherwise return the first element -} function detective(i) { function suspect(i) { if(i * 7 % 3 == 0) throw new Error("Bad i!"); } + if(suspect(i) !== "Bad i!") return "everything is ok" + } + +try { + detective(); + return "everything is ok" + } catch(err) { + return "something fishy" +} // 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"]; + for (var i = 0; i < terrorSuspects.length; i++) { + if(name === terrorSuspects[0] || name === terrorSuspects[1] || name === terrorSuspects[2]) { + throw new Error("He is suspect") + } else return flightNumber + } // if the name is a terror suspect, throw an exception // Otherwise, return the flight number } -module.exports = {first, detective, assignFlight} \ No newline at end of file +module.exports = {first, detective, assignFlight} diff --git a/src/inheritance/problem1.js b/src/inheritance/problem1.js index 71e66b5..869e0ba 100644 --- a/src/inheritance/problem1.js +++ b/src/inheritance/problem1.js @@ -4,8 +4,20 @@ class Shape { } } -class Rectangle { - +class Rectangle extends Shape { + constructor(width, heigth) { + super(); + this.width = width; + this.heigth = heigth; + } + area() { + if (this.width !== this.height) return this.width * this.heigth + else return this.size * this.size; + } + perimeter() { + if (this.width !== this.height) return this.width * 2 + this.heigth * 2 + else return this.size * 4; + } // A rectangle is a shape // Every rectangle has a width and a height // Implement the constructor @@ -13,7 +25,12 @@ class Rectangle { // The constructor has two arguments: width and height } -class Square { +class Square extends Rectangle{ + constructor(size) { + super(); + this.size = 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 +40,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..3e040c0 100644 --- a/src/inheritance/problem2.js +++ b/src/inheritance/problem2.js @@ -2,25 +2,28 @@ 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; } } +var s = new Square(5) +console.log(s.toString()) + module.exports = {Shape, Square}; \ No newline at end of file diff --git a/src/return/problem1.js b/src/return/problem1.js index c8f7b32..084e59f 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -3,13 +3,7 @@ // Also, remove these comments function f(x) { - if(x > 10) { - return "hello"; - } else if(x > 5) { - return "goodbye"; - } else { - return undefined; - } + return x > 10 ? "hello" : x > 5 ? "goodbye" : undefined; } module.exports = f; // Don't delete this line but remove this comment. diff --git a/src/syntax-errors/problem1.js b/src/syntax-errors/problem1.js index a798e0c..d8c08f9 100644 --- a/src/syntax-errors/problem1.js +++ b/src/syntax-errors/problem1.js @@ -1,7 +1,7 @@ for(var i = 0; i < 10; i++) { for(var j = 0; j < 10; j++) { for(var k = 0; k < 10; k++) { - if(i < k) + if(i < k) { if( k < j) { console.log(i + j + k) } diff --git a/src/syntax-errors/problem2.js b/src/syntax-errors/problem2.js index 63643d3..9ae4d99 100644 --- a/src/syntax-errors/problem2.js +++ b/src/syntax-errors/problem2.js @@ -14,7 +14,7 @@ var myObj = { }, beers: function() { - function g(){}} + function g(){} // lots and lots of code }, diff --git a/src/this/problem1.js b/src/this/problem1.js index b7b2740..e852681 100644 --- a/src/this/problem1.js +++ b/src/this/problem1.js @@ -1,6 +1,7 @@ '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; } - 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..a0af8d1 100644 --- a/src/variable-scoping/problem1.js +++ b/src/variable-scoping/problem1.js @@ -3,8 +3,9 @@ // 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; }