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..7648032 100644 --- a/paperproblems/anonymous-functions/problem1.txt +++ b/paperproblems/anonymous-functions/problem1.txt @@ -1,8 +1,4 @@ Rewrite this program so that it doesn't have a single function definition In other words, replace all the function definitions with anonymous functions -function greet(x) { - console.log("hello " + x); -} - -greet(bob); \ No newline at end of file +(function (x) {console.log("hello " + x);})('bob'); \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem3.txt b/paperproblems/anonymous-functions/problem3.txt index 5d8a0fc..d52687c 100644 --- a/paperproblems/anonymous-functions/problem3.txt +++ b/paperproblems/anonymous-functions/problem3.txt @@ -1,12 +1,7 @@ Rewrite this program so that it doesn't have a single function definition In other words, replace all the function definitions with anonymous functions -function greet(x, y) { - console.log("hello " + x + " " + y); -} -function call(f) { - f("Bob", "Dole"); -} -call(greet); +(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..d17a784 100644 --- a/paperproblems/anonymous-functions/problem4.txt +++ b/paperproblems/anonymous-functions/problem4.txt @@ -1,13 +1,7 @@ Rewrite this program so that it doesn't have a single function definition In other words, replace all the function definitions with anonymous functions -function greet(x) { - console.log("hello " + x); -} - -function twice(f) { +(function (f) { f("bob"); f("mary"); -} - -twice(greet); \ No newline at end of file +})(function (x) {console.log("hello " + x);}) \ No newline at end of file diff --git a/paperproblems/constructor-functions/problem5.txt b/paperproblems/constructor-functions/problem5.txt index f47e2b8..44c571c 100644 --- a/paperproblems/constructor-functions/problem5.txt +++ b/paperproblems/constructor-functions/problem5.txt @@ -1,7 +1,18 @@ 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) { +function Person(myname, myage) { + return {name : myname, + age : myage, + greet : function() { + console.log("Hello my name is " + this.name); + }} +} + +var bob = Person("Bob", 30) +bob.__proto__ = Person.prototype + +//function Person(name, age) { this.name = name; this.age = age; this.greet = function() { @@ -9,4 +20,4 @@ function Person(name, age) { } } -var bob = Person("Bob", 30); \ No newline at end of file +var bob = new Person("Bob", 30);// \ No newline at end of file diff --git a/paperproblems/inheritance/problem2.txt b/paperproblems/inheritance/problem2.txt index f9d6898..600c5a4 100644 --- a/paperproblems/inheritance/problem2.txt +++ b/paperproblems/inheritance/problem2.txt @@ -11,3 +11,16 @@ 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.width * this.height;} + perimeter() { return (this.width * 2) + (this.height * 2);} +} + +var square = new Rectangle (4, 4) +square.toString(); \ No newline at end of file diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..5b37e53 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,99 +1,53 @@ 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 - // otherwise return what f(arg) returned - // Example: - // function nullZero(x) { - // if(x==0) return null; - // return x; - // } - // callNoNull(nullZero, 0) throws an exception - // callNoNull(nullZero, 12) returns 12 - - + if (f(arg) == null) {throw 'this is null';} + else return f(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 - // otherwise it returns what f(arg) returned - // difference with callNoNull: callNoNull doesn't return a function - // Example: - // function nullZero(x) { - // if(x==0) return null; - // return x; - // } - // 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 'f(arg) is null';} + else return f(arg); + } } function nullify(f) { - // returns a new function - // this function takes 1 input, called arg - // if f(arg) throws an exception, this new function returns null - // otherwise it returns what f(arg) returned - // Example - // function throwsZero(x){ - // if(x==0) throw new Error("woops"); - // return x; - // } - // 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) { - // 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 newArr = []; + for (var i=0; i x % 2 == 0) returns true - // every([2,3,12], x => x % 2 == 0) returns false -} + for (var i=0; i a%2 !== 0) // lst is an array of numbers // Returns a new list with all the even numbers of lst removed } function keepLong(lst) { + return lst.filter((a) => a.length > 5) // 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) { + return lst.map((a) => 'Hello '+a) // 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) { + return lst.filter((a) => a.length >= 4).map((a) => 'Hello '+a) // lst is an array of strings // Only greet people who's names have length at least 4. // Otherwise ignore them completely. @@ -22,6 +26,7 @@ function greetLong(lst) { } function allLong(lst) { + return lst.every((a) => a.length >= 5) // 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..e7fb024 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 => 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..a050183 100644 --- a/src/arrow-functions/problem2.js +++ b/src/arrow-functions/problem2.js @@ -1,9 +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}; +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..30b96ab 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -2,6 +2,8 @@ var bob = {name: "Bob"}; function greet() { return "I'm " + this.name; } + +greet = greet.bind(bob); // bind greet to bob module.exports = greet; \ No newline at end of file diff --git a/src/bind/problem2.js b/src/bind/problem2.js index 079a343..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..4f753a2 100644 --- a/src/callbacks/problem1.js +++ b/src/callbacks/problem1.js @@ -1,7 +1,11 @@ // 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 +function f() { + shout('hello'); +} + +setTimeout(f, 1000); \ No newline at end of file diff --git a/src/classes/problem1.js b/src/classes/problem1.js index 8e1bcc9..7563e12 100644 --- a/src/classes/problem1.js +++ b/src/classes/problem1.js @@ -1,4 +1,10 @@ class Dog { + constructor (age, name, breed) { + this.age = age; + this.name = name; + this.breed = breed; + } + 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..8aac792 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,23 +1,10 @@ -//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 diff --git a/src/conditional-operator/problem2.js b/src/conditional-operator/problem2.js index 2dd85bf..c0d8744 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..d312d1c 100644 --- a/src/constructor-functions/problem1.js +++ b/src/constructor-functions/problem1.js @@ -1,2 +1,6 @@ // Add a function to all arrays called isNotEmpty // isNotEmpty returns true is the array is empty, false otherwise + +Array.prototype.isNotEmpty = function () { + if (this.length === 0) {return false;} + else return true;} \ No newline at end of file diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..9f3fd99 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,22 +1,24 @@ function first(arr) { - // Throw an exception if the array has no elements - // Otherwise return the first element + if (arr.length === 0) {throw "this array is empty";} + 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);} + catch(err) { + return('something fishy'); + } + return 'everything ok'; } function assignFlight(name) { var flightNumber = ((name.length * 7) % 20) + "0"; var terrorSuspects = ["bob", "eric", "susie"]; - // if the name is a terror suspect, throw an exception - // Otherwise, return the flight number + if(name == terrorSuspects[0] || name == terrorSuspects[1] || name == terrorSuspects[2]) {throw "suspect alert!";} + else return flightNumber; } 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..89cce37 100644 --- a/src/inheritance/problem1.js +++ b/src/inheritance/problem1.js @@ -4,8 +4,14 @@ 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 // Implement the constructor @@ -13,7 +19,10 @@ class Rectangle { // The constructor has two arguments: width and height } -class Square { +class Square extends Rectangle { + constructor(size) { + super(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 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..3d4cb7d 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,8 @@ -// 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..e38c64b 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;} + 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..c6eb1e0 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 -function f() { - var x = 0; +var x = 0; + +function f() { x = x + 1; return x; }