diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f09c238 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,326 @@ +{ + "name": "javascript-fundamentals", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", + "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "dev": true, + "requires": { + "balanced-match": "1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", + "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=", + "dev": true + }, + "chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", + "dev": true, + "requires": { + "assertion-error": "1.1.0", + "deep-eql": "0.1.3", + "type-detect": "1.0.0" + } + }, + "chai-spies": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/chai-spies/-/chai-spies-0.7.1.tgz", + "integrity": "sha1-ND2Z9RJEIS6LF+ZLk5lv97LCqbE=", + "dev": true + }, + "commander": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.9.0.tgz", + "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", + "dev": true, + "requires": { + "graceful-readlink": "1.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "debug": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz", + "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-eql": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", + "dev": true, + "requires": { + "type-detect": "0.1.1" + }, + "dependencies": { + "type-detect": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz", + "integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=", + "dev": true + } + } + }, + "diff": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.2.0.tgz", + "integrity": "sha1-yc45Okt8vQsFinJck98pkCeGj/k=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "glob": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz", + "integrity": "sha1-gFIR3wT6rxxjo2ADBs31reULLsg=", + "dev": true, + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "graceful-readlink": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", + "integrity": "sha1-TK+tdrxi8C+gObL5Tpo906ORpyU=", + "dev": true + }, + "growl": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz", + "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=", + "dev": true + }, + "has-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-1.0.0.tgz", + "integrity": "sha1-nZ55MWXOAXoA8AQYxD+UKnsdEfo=", + "dev": true + }, + "he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "json3": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/json3/-/json3-3.3.2.tgz", + "integrity": "sha1-PAQ0dD35Pi9cQq7nsZvLSDV19OE=", + "dev": true + }, + "lodash._baseassign": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz", + "integrity": "sha1-jDigmVAPIVrQnlnxci/QxSv+Ck4=", + "dev": true, + "requires": { + "lodash._basecopy": "3.0.1", + "lodash.keys": "3.1.2" + } + }, + "lodash._basecopy": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz", + "integrity": "sha1-jaDmqHbPNEwK2KVIghEd08XHyjY=", + "dev": true + }, + "lodash._basecreate": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash._basecreate/-/lodash._basecreate-3.0.3.tgz", + "integrity": "sha1-G8ZhYU2qf8MRt9A78WgGoCE8+CE=", + "dev": true + }, + "lodash._getnative": { + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz", + "integrity": "sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=", + "dev": true + }, + "lodash._isiterateecall": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/lodash._isiterateecall/-/lodash._isiterateecall-3.0.9.tgz", + "integrity": "sha1-UgOte6Ql+uhCRg5pbbnPPmqsBXw=", + "dev": true + }, + "lodash.create": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/lodash.create/-/lodash.create-3.1.1.tgz", + "integrity": "sha1-1/KEnw29p+BGgruM1yqwIkYd6+c=", + "dev": true, + "requires": { + "lodash._baseassign": "3.2.0", + "lodash._basecreate": "3.0.3", + "lodash._isiterateecall": "3.0.9" + } + }, + "lodash.isarguments": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", + "integrity": "sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo=", + "dev": true + }, + "lodash.isarray": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/lodash.isarray/-/lodash.isarray-3.0.4.tgz", + "integrity": "sha1-eeTriMNqgSKvhvhEqpvNhRtfu1U=", + "dev": true + }, + "lodash.keys": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/lodash.keys/-/lodash.keys-3.1.2.tgz", + "integrity": "sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo=", + "dev": true, + "requires": { + "lodash._getnative": "3.9.1", + "lodash.isarguments": "3.1.0", + "lodash.isarray": "3.0.4" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "1.1.8" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-3.5.3.tgz", + "integrity": "sha512-/6na001MJWEtYxHOV1WLfsmR4YIynkUEhBwzsb+fk2qmQ3iqsi258l/Q2MWHJMImAcNpZ8DEdYAK72NHoIQ9Eg==", + "dev": true, + "requires": { + "browser-stdout": "1.3.0", + "commander": "2.9.0", + "debug": "2.6.8", + "diff": "3.2.0", + "escape-string-regexp": "1.0.5", + "glob": "7.1.1", + "growl": "1.9.2", + "he": "1.1.1", + "json3": "3.3.2", + "lodash.create": "3.1.1", + "mkdirp": "0.5.1", + "supports-color": "3.1.2" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "supports-color": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-3.1.2.tgz", + "integrity": "sha1-cqJiiU2dQIuVbKBf83su2KbiotU=", + "dev": true, + "requires": { + "has-flag": "1.0.0" + } + }, + "type-detect": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz", + "integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=", + "dev": true + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/paperproblems/anonymous-functions/problem1.txt b/paperproblems/anonymous-functions/problem1.txt index 08e09e7..42684e0 100644 --- a/paperproblems/anonymous-functions/problem1.txt +++ b/paperproblems/anonymous-functions/problem1.txt @@ -5,4 +5,7 @@ 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);} +greet = "bob"; \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem2.txt b/paperproblems/anonymous-functions/problem2.txt index 7cfb344..d1e02aa 100644 --- a/paperproblems/anonymous-functions/problem2.txt +++ b/paperproblems/anonymous-functions/problem2.txt @@ -12,3 +12,5 @@ function call(f) { } call(greet); + +(function(f){f("bob");})(function(x){console.log("hello " + x);}) \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem3.txt b/paperproblems/anonymous-functions/problem3.txt index 5d8a0fc..9c03e04 100644 --- a/paperproblems/anonymous-functions/problem3.txt +++ b/paperproblems/anonymous-functions/problem3.txt @@ -10,3 +10,5 @@ function call(f) { } call(greet); + +(function(f){f("bob","dole");})(function(x,y){console.log("hello " + x + " " + y);}) \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem4.txt b/paperproblems/anonymous-functions/problem4.txt index 689d724..7d22028 100644 --- a/paperproblems/anonymous-functions/problem4.txt +++ b/paperproblems/anonymous-functions/problem4.txt @@ -10,4 +10,6 @@ function twice(f) { f("mary"); } -twice(greet); \ No newline at end of file +twice(greet); + +(function(f){f("bob");f("mary")})(function(x){console.log("hello " + x);}) \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem5.txt b/paperproblems/anonymous-functions/problem5.txt index 2d69a4d..a6494fb 100644 --- a/paperproblems/anonymous-functions/problem5.txt +++ b/paperproblems/anonymous-functions/problem5.txt @@ -15,5 +15,7 @@ function soften(f) { } var softAskOnADate = soften(askOnADate); -console.log(softAskOnADate("Eric")); -console.log(softAskOnADate("Bob")); +console.log(softAskOnADate("Eric")); --> maybe +console.log(softAskOnADate("Bob"));--> I do! + + \ No newline at end of file diff --git a/paperproblems/arrow-functions/problem1.txt b/paperproblems/arrow-functions/problem1.txt index 7338e61..6614b26 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 -- inputs x and adds 1 -b) x, y => x * y +b) x, y => x * y // x is not defined -c) x => { x * 2 } +c) x => { x * 2 } //no syntax error -- inputs x and multiplies by 2 -d) (x, z) => {console.log(z); return x * z} +d) (x, z) => {console.log(z); return x * z} //no syntax error -- prints z and then returns z multiplied by x -e) x => console.log(z); return x * z +e) x => console.log(z); return x * z // return is outside of the function -f) (x) => x * 2 +f) (x) => x * 2 // will return x multiplied by 2 -e) () => console.log("hello") +e) () => console.log("hello") // prints out hello When you're done, check all your answers in the developer console. \ No newline at end of file diff --git a/paperproblems/bind/problem1.txt b/paperproblems/bind/problem1.txt index e57fc23..b79274a 100644 --- a/paperproblems/bind/problem1.txt +++ b/paperproblems/bind/problem1.txt @@ -9,4 +9,6 @@ var obj = { obj.bar = obj.bar.bind({baz: 4}); -obj.bar(); \ No newline at end of file +obj.bar(); + +--> 4 \ No newline at end of file diff --git a/paperproblems/bind/problem2.txt b/paperproblems/bind/problem2.txt index 6c9d16d..2cb82e4 100644 --- a/paperproblems/bind/problem2.txt +++ b/paperproblems/bind/problem2.txt @@ -15,4 +15,6 @@ eric.introduction = bob.introduction; eric.introduction = eric.introduction.bind({name: "Steve"}); -eric.introduction(); \ No newline at end of file +eric.introduction(); + +--> Hi I'm Bob \ No newline at end of file diff --git a/paperproblems/callbacks/problem1.txt b/paperproblems/callbacks/problem1.txt index 63df256..df84965 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); + +//wait 1.7 seconds and print Hello! \ No newline at end of file diff --git a/paperproblems/callbacks/problem2.txt b/paperproblems/callbacks/problem2.txt index 1ee06dd..7d4155a 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); + +//print hello every 2 seconds infinitely \ No newline at end of file diff --git a/paperproblems/classes/problem2.txt b/paperproblems/classes/problem2.txt index 60891b1..975e036 100644 --- a/paperproblems/classes/problem2.txt +++ b/paperproblems/classes/problem2.txt @@ -18,5 +18,5 @@ class Person { var bob = new Person(25, "Bob", 40000); -bob.increaseSalary(-500); -console.log(bob.toString()); \ No newline at end of file +bob.increaseSalary(-500); // Yay! +console.log(bob.toString());// My name is Bob and im 25 and i make 39500 \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem2.txt b/paperproblems/constructor-functions/problem2.txt index 4cb7dd9..680ad6a 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(); + +//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..30fcd06 100644 --- a/paperproblems/constructor-functions/problem3.txt +++ b/paperproblems/constructor-functions/problem3.txt @@ -10,10 +10,10 @@ function Person(name, age) { var bob = new Person("Bob", 30); -bob.constructor.prototype.salary = 40000; -bob.__proto__.salary = bob.__proto__.salary + 100; -Person.prototype.salary = Person.prototype.salary + 300; -Person.__proto__.salary = Person.__proto__.salary + 400; -bob.salary = bob.salary + 50; +bob.constructor.prototype.salary = 40000; +bob.__proto__.salary = bob.__proto__.salary + 100; // adds 100 to 40 000 = 40 100 +Person.prototype.salary = Person.prototype.salary + 300; // adds 300 to 40 100 = 40 400 +Person.__proto__.salary = Person.__proto__.salary + 400; // cannot __proto__ to a object so skips this +bob.salary = bob.salary + 50; // adds 50 to 40 400 -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..cbd1071 100644 --- a/paperproblems/constructor-functions/problem4.txt +++ b/paperproblems/constructor-functions/problem4.txt @@ -12,4 +12,4 @@ var bob = Person("Bob", 30); bob.name = bob.name + " Dole"; -bob.greet(); \ No newline at end of file +bob.greet(); // the program will crash because we need to use the keyword new to create an object; \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem5.txt b/paperproblems/constructor-functions/problem5.txt index f47e2b8..2764a7a 100644 --- a/paperproblems/constructor-functions/problem5.txt +++ b/paperproblems/constructor-functions/problem5.txt @@ -1,12 +1,15 @@ Rewrite this program so that it does't use the new keyword. The program must be equivalent in every other way. -function Person(name, age) { - this.name = name; - this.age = age; - this.greet = function() { - console.log("Hello my name is " + this.name); +function Person(fname, myage) { + return {name: fname, + age: myage, + greet: function() { + console.log("Hello my name is " + this.name) + } } } -var bob = Person("Bob", 30); \ No newline at end of file +var bob = Person("Bob", 30); +bob.__proto__ = Person.prototype; +bob.greet(); \ No newline at end of file diff --git a/paperproblems/inheritance/problem1.txt b/paperproblems/inheritance/problem1.txt index 3de989f..66d0fcb 100644 --- a/paperproblems/inheritance/problem1.txt +++ b/paperproblems/inheritance/problem1.txt @@ -24,4 +24,4 @@ class Square extends Shape { } var s = new Square(5); -console.log(s.toString()); \ No newline at end of file +console.log(s.toString()); //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..b4197c1 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(width, height){ + super("rectangle"); + this.width = width; + this.height = height; + } + area() { + return this.height * this.width; + } + perimeter() { + return (this.width + this.height) * 2; + } +} + +var r = new Rectangle(100, 200); +console.log(r.toString()); diff --git a/paperproblems/this/problem1.txt b/paperproblems/this/problem1.txt index 7212ac6..078b838 100644 --- a/paperproblems/this/problem1.txt +++ b/paperproblems/this/problem1.txt @@ -9,3 +9,4 @@ function foo() { var obj = {bar: foo, baz: 8}; obj.bar(); +--> 8 \ No newline at end of file diff --git a/paperproblems/this/problem2.txt b/paperproblems/this/problem2.txt index 1144503..1dcdca0 100644 --- a/paperproblems/this/problem2.txt +++ b/paperproblems/this/problem2.txt @@ -7,6 +7,7 @@ var obj = { baz: 12 } -var g = obj.bar; +var g = obj.bar(); g(); +--> wont be able to console.log baz because this.baz returns undefined. diff --git a/paperproblems/this/problem3.txt b/paperproblems/this/problem3.txt index dcfda4d..eb02b20 100644 --- a/paperproblems/this/problem3.txt +++ b/paperproblems/this/problem3.txt @@ -15,3 +15,4 @@ 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..02139c7 100644 --- a/paperproblems/this/problem4.txt +++ b/paperproblems/this/problem4.txt @@ -11,4 +11,6 @@ function f(g) { console.log(g(4)); } -f(obj.bar); \ No newline at end of file +f(obj.bar); + +--> undefined \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem1.txt b/paperproblems/variable-scoping/problem1.txt index 281bdd6..44d55c0 100644 --- a/paperproblems/variable-scoping/problem1.txt +++ b/paperproblems/variable-scoping/problem1.txt @@ -7,5 +7,8 @@ function f() { console.log(x); } +f(); f(); -f(); +--> 2 +--> 4 + diff --git a/paperproblems/variable-scoping/problem2.txt b/paperproblems/variable-scoping/problem2.txt index eb70c9d..f4d725e 100644 --- a/paperproblems/variable-scoping/problem2.txt +++ b/paperproblems/variable-scoping/problem2.txt @@ -7,4 +7,6 @@ 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..955dffe 100644 --- a/paperproblems/variable-scoping/problem3.txt +++ b/paperproblems/variable-scoping/problem3.txt @@ -9,6 +9,6 @@ function f() { console.log(x + y); } -f(); -f(); -console.log(y); \ No newline at end of file +f(); //prints 7 +f(); //prints 9 +console.log(y); //prints 3 \ No newline at end of file diff --git a/paperproblems/variable-scoping/problem4.txt b/paperproblems/variable-scoping/problem4.txt index 2294b2e..5f8515d 100644 --- a/paperproblems/variable-scoping/problem4.txt +++ b/paperproblems/variable-scoping/problem4.txt @@ -11,7 +11,7 @@ function f() { var b = "boom"; } // Which variables are in scope on this line? + f, g , h, k , y , p , b , z, x } - } } \ No newline at end of file diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..a465120 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -9,6 +9,14 @@ function callNoException(f, arg) { // } // callNoException(throwsZero, 0) returns null // callNoException(throwsZero, 12) returns 12 + + try { + f(arg); + + } catch (err) { + return null; + } + return arg; } function callNoNull(f, arg) { @@ -21,8 +29,9 @@ function callNoNull(f, arg) { // } // callNoNull(nullZero, 0) throws an exception // callNoNull(nullZero, 12) returns 12 - - + + if (f(arg) == null) throw new Error("Returns null") + return arg; } function exceptionalize(f) { @@ -39,7 +48,11 @@ function exceptionalize(f) { // exceptionalize(nullZero) returns a function g such that // g(0) throws an exception // g(12) returns 12 - + return function (arg) { + if (f(arg) === null) throw new Error("Error"); + return arg; + } + return f(arg); } function nullify(f) { @@ -55,7 +68,15 @@ function nullify(f) { // nullify(throwsZero) returns a function g such that // g(0) returns null // g(12) throws an exception - + return function (arg) { + try { + f(arg) + + } catch (err) { + return null; + } + return f(arg); + } } function map(lst, f) { @@ -69,6 +90,11 @@ function map(lst, f) { // // function toUpperCase(str) { return str.toUpperCase(); } // map(["bob", "susie"], toUpperCase) returns ["BOB", "SUSIE"] + var newArray = []; + for (var i = 0; i < lst.length; i++) { + newArray.push(f(lst[i])); + } + return newArray; } function filter(lst, f) { @@ -82,26 +108,43 @@ function filter(lst, f) { // // Example: // function isEven(x) {return x % 2 == 0;} - // filter([1, 2, 3, 4, 5], isEven) returns [2,4]; + // filter([1, 2, 3, 4, 5], isEven) returns [2,4]; + var newArray = []; + + for (var i = 0; i < lst.length; i++) { + if (f(lst[i]) == true) { + newArray.push(lst[i]); + } + } + return newArray; } function every(lst, f) { // 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 - + // 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 return y + 2, (x,y) => return x + y); + return c( x => y + 2, (x,y) => x + y); } module.exports = t diff --git a/src/arrow-functions/problem2.js b/src/arrow-functions/problem2.js index 1ae3fdb..fb80bf4 100644 --- a/src/arrow-functions/problem2.js +++ b/src/arrow-functions/problem2.js @@ -1,10 +1,9 @@ // 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}; - -module.exports = {x, y, z}; +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..d8db3cc 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -3,5 +3,5 @@ function greet() { 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..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..c482a0a 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.toUpperCase()); } -SetTimeout(shout("hello"), 1); \ No newline at end of file +setTimeout(function (){shout("hello")}, 1000); \ No newline at end of file diff --git a/src/classes/problem1.js b/src/classes/problem1.js index 8e1bcc9..d832f40 100644 --- a/src/classes/problem1.js +++ b/src/classes/problem1.js @@ -1,7 +1,15 @@ class Dog { // 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 + // Dog has a method bark, which returns a string + constructor(age,name,breed){ + this.age = age; + this.name = name; + this.breed = breed; + } + bark (){ + return "I'm "+ this.name +" I am a " + this.breed + " and I am " + this.age +"."; + } } module.exports = Dog; \ No newline at end of file diff --git a/src/constructor-functions/problem1.js b/src/constructor-functions/problem1.js index b39887c..018c241 100644 --- a/src/constructor-functions/problem1.js +++ b/src/constructor-functions/problem1.js @@ -1,2 +1,7 @@ // 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 () { return this.length !== 0; } +console.log(x.isNotEmpty()) +x.push(0); +console.log(x.isNotEmpty()) \ No newline at end of file diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..bff30fa 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,22 +1,45 @@ function first(arr) { // Throw an exception if the array has no elements // Otherwise return the first element + if (arr.length === 0) { + throw new Error("The array is empty"); + + } + return arr[0]; + } function detective(i) { function suspect(i) { - if(i * 7 % 3 == 0) throw new Error("Bad i!"); + if (i * 7 % 3 == 0) throw new Error("Bad i!"); + + } + try{ + suspect(i); + }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. + return "everything ok"; } function assignFlight(name) { var flightNumber = ((name.length * 7) % 20) + "0"; var terrorSuspects = ["bob", "eric", "susie"]; + var found = false; // if the name is a terror suspect, throw an exception // Otherwise, return the flight number + + for (var i = 0; i <= terrorSuspects.length; i++) { + found = false; + if (name === terrorSuspects[i]) { + found = true; + //console.log(found); + throw new Error(name + " is a terror suspect!"); + } + + + } + if (!found) return flightNumber; } -module.exports = {first, detective, assignFlight} \ No newline at end of file +module.exports = { first, detective, assignFlight } \ No newline at end of file diff --git a/src/inheritance/problem1.js b/src/inheritance/problem1.js index 71e66b5..70310cd 100644 --- a/src/inheritance/problem1.js +++ b/src/inheritance/problem1.js @@ -4,22 +4,38 @@ class Shape { } } -class Rectangle { +class Rectangle extends Shape { // A rectangle is a shape // Every rectangle has a width and a height // Implement the constructor // Implement the area and perimeter methods // The constructor has two arguments: width and height + constructor(width, height){ + super(); + this.width = width; + this.height = height; + } + area() { + return this.height * this.width; + } + perimeter() { + return (this.width + this.height) * 2; + } } -class Square { +class Square extends Rectangle{ // A square is a rectangle // Every square has a width and a height // The height and width of a square are always the same // Implement the constructor // Do not implement the area and perimeter methods. They should be inherited from Rectangle // The constructor has one argument + constructor(size){ + super(); + this.width = size; + this.height = size; + } } diff --git a/src/inheritance/problem2.js b/src/inheritance/problem2.js index f74dc6b..c0ef88a 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..9f3282a 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,7 @@ -// 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; - } + if (x > 10) return "hello"; + if (x > 5) return "goodbye"; } - -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..585bf27 100644 --- a/src/this/problem1.js +++ b/src/this/problem1.js @@ -1,6 +1,8 @@ 'use strict'; function whatsMyAgeAgain() { // returns this.age unless this is not defined. If this is not defined, return 18 + if(typeof this === 'undefined') return 18; + 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..993742c 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; }