From 93cc447f26f17f95a3a16647223f92a19dbc114b Mon Sep 17 00:00:00 2001 From: mamouel Date: Mon, 15 Jan 2018 19:48:02 -0500 Subject: [PATCH 01/13] ex src HOF, exceptions, __proto__, return, bind, this and anonymous function --- HOF avec commentaires/problem1.js | 194 +++++++++++ package-lock.json | 326 ++++++++++++++++++ .../anonymous-functions/problem1.txt | 7 +- .../anonymous-functions/problem2.txt | 12 + .../anonymous-functions/problem3.txt | 11 + .../anonymous-functions/problem4.txt | 11 + paperproblems/exceptions/problem2.txt | 2 +- paperproblems/return/problem1.txt | 3 +- src/HOF/problem1.js | 134 +++---- src/__proto__/problem1.js | 4 +- src/anonymous-functions/problem1.js | 2 +- src/bind/problem1.js | 4 +- src/bind/problem2.js | 6 +- src/exceptions/problem1.js | 36 +- src/return/problem1.js | 14 +- src/this/problem1.js | 2 + 16 files changed, 664 insertions(+), 104 deletions(-) create mode 100644 HOF avec commentaires/problem1.js create mode 100644 package-lock.json diff --git a/HOF avec commentaires/problem1.js b/HOF avec commentaires/problem1.js new file mode 100644 index 0000000..f402861 --- /dev/null +++ b/HOF avec commentaires/problem1.js @@ -0,0 +1,194 @@ +function callNoException(f, arg) { + try { + f(arg); + return f(arg); + } + catch(err) { + return null; + } +} + +// 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) { + // if(x==0) return null; + // return x; + // } + // 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) +} +*/ + + // 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 + + +// +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 + function g(arg) { + try { + return f(arg); + } catch (err) { + return null; + } + } + return g; +} + + +// +function map(lst, f) { + 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; +} + + // 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 + // filter(lst, f) has fewer elements than lst + // 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 + // 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 + for(var i = 0; i < lst.length; i++) { + if(f(lst[i]) === false) { + return false; + } + + } + return true; +} + + +module.exports = { + callNoException, + callNoNull, + exceptionalize, + nullify, + map, + filter, + every +}; 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..ba98cbc 100644 --- a/paperproblems/anonymous-functions/problem1.txt +++ b/paperproblems/anonymous-functions/problem1.txt @@ -5,4 +5,9 @@ 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..f3470a8 100644 --- a/paperproblems/anonymous-functions/problem2.txt +++ b/paperproblems/anonymous-functions/problem2.txt @@ -12,3 +12,15 @@ function call(f) { } call(greet); + + +var greet = function(x) { + console.log("hello " + x); +}; + +var call = function(f) { + f("bob"); +}; + +call(greet) + diff --git a/paperproblems/anonymous-functions/problem3.txt b/paperproblems/anonymous-functions/problem3.txt index 5d8a0fc..d61bf73 100644 --- a/paperproblems/anonymous-functions/problem3.txt +++ b/paperproblems/anonymous-functions/problem3.txt @@ -10,3 +10,14 @@ function call(f) { } call(greet); + + +var greet = function(x, y) { + console.log("hello " + x + " " + y); +}; + +var call = function(f) { + f("Bob", "Dole"); +}; + +call(greet); \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem4.txt b/paperproblems/anonymous-functions/problem4.txt index 689d724..d079b56 100644 --- a/paperproblems/anonymous-functions/problem4.txt +++ b/paperproblems/anonymous-functions/problem4.txt @@ -10,4 +10,15 @@ function twice(f) { f("mary"); } +twice(greet); + +var greet = function(x) { + console.log("hello " + x); +}; + +var twice = function(f) { + f("bob"); + f("mary"); +}; + twice(greet); \ No newline at end of file diff --git a/paperproblems/exceptions/problem2.txt b/paperproblems/exceptions/problem2.txt index 1108a2c..36095c3 100644 --- a/paperproblems/exceptions/problem2.txt +++ b/paperproblems/exceptions/problem2.txt @@ -22,4 +22,4 @@ function g() { } } -g(); +g(); \ No newline at end of file diff --git a/paperproblems/return/problem1.txt b/paperproblems/return/problem1.txt index fed99b8..81f1f27 100644 --- a/paperproblems/return/problem1.txt +++ b/paperproblems/return/problem1.txt @@ -5,5 +5,4 @@ function f() { return "hello"; } -console.log(f()); - +console.log(f()); \ No newline at end of file diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..8a7aae0 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,98 +1,76 @@ - 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 - - + var ret = f(arg); + if(ret === null) throw new Error("oh no!"); + return ret } -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 +function exceptionalize(f) { + return function(arg) { + if (f(arg) == null) {throw '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 - + 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 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 - // filter(lst, f) has fewer elements than lst - // 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 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 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 + + for(var i = 0; i < lst.length; i++) { + if(f(lst[i]) === false) { + return false; + } + + } + return true; } diff --git a/src/__proto__/problem1.js b/src/__proto__/problem1.js index 820a78d..d84b72b 100644 --- a/src/__proto__/problem1.js +++ b/src/__proto__/problem1.js @@ -1,6 +1,8 @@ // 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 ab1384c..33a161b 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -6,7 +6,7 @@ function c(g, h) { } function t() { - return c(function g(x) {return y + 1}, function h(y) {return x * 2}); + return c(function g(x) {return x + 1}, function h(y) {return y * 2}); } module.exports = t; \ No newline at end of file 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/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..f773233 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,22 +1,50 @@ 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(i * 7 % 3 === 0) throw new Error("Bad i!"); + } + if(suspect(i) !== "Bad i!") return "everything is ok" +} + + +try { +detective(4); +} 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 diff --git a/src/return/problem1.js b/src/return/problem1.js index c8f7b32..37e6881 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,9 @@ -// 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; - } + return x > 10 ? "hello" : x > 5 ? "goodbye" : 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..2a86e15 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(this === undefined) return 18; + else return this.age; } module.exports = whatsMyAgeAgain; \ No newline at end of file From 18ea2072762a518b41ef2292da3c563f14b8030b Mon Sep 17 00:00:00 2001 From: mamouel Date: Mon, 15 Jan 2018 19:51:31 -0500 Subject: [PATCH 02/13] Revert "ex src HOF, exceptions, __proto__, return, bind, this and anonymous function" This reverts commit 93cc447f26f17f95a3a16647223f92a19dbc114b. --- HOF avec commentaires/problem1.js | 194 ----------- package-lock.json | 326 ------------------ .../anonymous-functions/problem1.txt | 7 +- .../anonymous-functions/problem2.txt | 12 - .../anonymous-functions/problem3.txt | 11 - .../anonymous-functions/problem4.txt | 11 - paperproblems/exceptions/problem2.txt | 2 +- paperproblems/return/problem1.txt | 3 +- src/HOF/problem1.js | 134 ++++--- src/__proto__/problem1.js | 4 +- src/anonymous-functions/problem1.js | 2 +- src/bind/problem1.js | 4 +- src/bind/problem2.js | 6 +- src/exceptions/problem1.js | 36 +- src/return/problem1.js | 14 +- src/this/problem1.js | 2 - 16 files changed, 104 insertions(+), 664 deletions(-) delete mode 100644 HOF avec commentaires/problem1.js delete mode 100644 package-lock.json diff --git a/HOF avec commentaires/problem1.js b/HOF avec commentaires/problem1.js deleted file mode 100644 index f402861..0000000 --- a/HOF avec commentaires/problem1.js +++ /dev/null @@ -1,194 +0,0 @@ -function callNoException(f, arg) { - try { - f(arg); - return f(arg); - } - catch(err) { - return null; - } -} - -// 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) { - // if(x==0) return null; - // return x; - // } - // 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) -} -*/ - - // 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 - - -// -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 - function g(arg) { - try { - return f(arg); - } catch (err) { - return null; - } - } - return g; -} - - -// -function map(lst, f) { - 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; -} - - // 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 - // filter(lst, f) has fewer elements than lst - // 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 - // 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 - for(var i = 0; i < lst.length; i++) { - if(f(lst[i]) === false) { - return false; - } - - } - return true; -} - - -module.exports = { - callNoException, - callNoNull, - exceptionalize, - nullify, - map, - filter, - every -}; diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index f09c238..0000000 --- a/package-lock.json +++ /dev/null @@ -1,326 +0,0 @@ -{ - "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 ba98cbc..08e09e7 100644 --- a/paperproblems/anonymous-functions/problem1.txt +++ b/paperproblems/anonymous-functions/problem1.txt @@ -5,9 +5,4 @@ function greet(x) { console.log("hello " + x); } -greet(bob); - -var greet = function(x) { - console.log("hello " + x); -} -greet("bob"); \ No newline at end of file +greet(bob); \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem2.txt b/paperproblems/anonymous-functions/problem2.txt index f3470a8..7cfb344 100644 --- a/paperproblems/anonymous-functions/problem2.txt +++ b/paperproblems/anonymous-functions/problem2.txt @@ -12,15 +12,3 @@ function call(f) { } call(greet); - - -var greet = function(x) { - console.log("hello " + x); -}; - -var call = function(f) { - f("bob"); -}; - -call(greet) - diff --git a/paperproblems/anonymous-functions/problem3.txt b/paperproblems/anonymous-functions/problem3.txt index d61bf73..5d8a0fc 100644 --- a/paperproblems/anonymous-functions/problem3.txt +++ b/paperproblems/anonymous-functions/problem3.txt @@ -10,14 +10,3 @@ function call(f) { } call(greet); - - -var greet = function(x, y) { - console.log("hello " + x + " " + y); -}; - -var call = function(f) { - f("Bob", "Dole"); -}; - -call(greet); \ No newline at end of file diff --git a/paperproblems/anonymous-functions/problem4.txt b/paperproblems/anonymous-functions/problem4.txt index d079b56..689d724 100644 --- a/paperproblems/anonymous-functions/problem4.txt +++ b/paperproblems/anonymous-functions/problem4.txt @@ -10,15 +10,4 @@ function twice(f) { f("mary"); } -twice(greet); - -var greet = function(x) { - console.log("hello " + x); -}; - -var twice = function(f) { - f("bob"); - f("mary"); -}; - twice(greet); \ No newline at end of file diff --git a/paperproblems/exceptions/problem2.txt b/paperproblems/exceptions/problem2.txt index 36095c3..1108a2c 100644 --- a/paperproblems/exceptions/problem2.txt +++ b/paperproblems/exceptions/problem2.txt @@ -22,4 +22,4 @@ function g() { } } -g(); \ No newline at end of file +g(); diff --git a/paperproblems/return/problem1.txt b/paperproblems/return/problem1.txt index 81f1f27..fed99b8 100644 --- a/paperproblems/return/problem1.txt +++ b/paperproblems/return/problem1.txt @@ -5,4 +5,5 @@ function f() { return "hello"; } -console.log(f()); \ No newline at end of file +console.log(f()); + diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 8a7aae0..0ed585e 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,76 +1,98 @@ + function callNoException(f, arg) { - try { - f(arg); - return f(arg); - } - catch(err) { - return null; - } + // 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 } - function callNoNull(f, arg) { - var ret = f(arg); - if(ret === null) throw new Error("oh no!"); - return ret + // 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 + + } +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 -function exceptionalize(f) { - return function(arg) { - if (f(arg) == null) {throw 'arg is null';} - else return f(arg); - } } - - -// 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 + // 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 + } - -// function map(lst, f) { - var ret = []; - for (index in lst) { - ret.push(f(lst[index])); - } - return ret; - } - - - // - - 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; + // 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"] } - +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 + // filter(lst, f) has fewer elements than lst + // 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) { - - for(var i = 0; i < lst.length; i++) { - if(f(lst[i]) === false) { - return false; - } - - } - return true; + // 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 } diff --git a/src/__proto__/problem1.js b/src/__proto__/problem1.js index d84b72b..820a78d 100644 --- a/src/__proto__/problem1.js +++ b/src/__proto__/problem1.js @@ -1,8 +1,6 @@ // Make parent the __proto__ of child var parent = {x: 5, y: 6, z: 8}; -var child = {x : 10, __proto__: parent}; - - +var child = {x : 10}; 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 33a161b..ab1384c 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -6,7 +6,7 @@ function c(g, h) { } function t() { - return c(function g(x) {return x + 1}, function h(y) {return y * 2}); + return c(function g(x) {return y + 1}, function h(y) {return x * 2}); } module.exports = t; \ No newline at end of file diff --git a/src/bind/problem1.js b/src/bind/problem1.js index 91594a8..da0b2d6 100644 --- a/src/bind/problem1.js +++ b/src/bind/problem1.js @@ -1,7 +1,7 @@ var bob = {name: "Bob"}; -var greet = function() { +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 15d7f8f..079a343 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"}; -var greet = function() { - return "I'm a " + this.breed; +function greet() { + return "I'm a " + this.bred; } -greet = greet.bind(dog); +greet.bind(dog); module.exports = greet; \ No newline at end of file diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f773233..f54c8f8 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,50 +1,22 @@ 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(4); -} catch(err) { - return "something fishy" -} - + 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. +} 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 diff --git a/src/return/problem1.js b/src/return/problem1.js index 37e6881..c8f7b32 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,9 +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) { - return x > 10 ? "hello" : x > 5 ? "goodbye" : undefined + if(x > 10) { + return "hello"; + } else if(x > 5) { + return "goodbye"; + } else { + return undefined; + } } -module.exports = f; +module.exports = f; // Don't delete this line but remove this comment. diff --git a/src/this/problem1.js b/src/this/problem1.js index 2a86e15..b7b2740 100644 --- a/src/this/problem1.js +++ b/src/this/problem1.js @@ -1,8 +1,6 @@ '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 From 4bf0e8860d941f0693b3b0d8cea1adc4f619fd13 Mon Sep 17 00:00:00 2001 From: mamouel Date: Tue, 16 Jan 2018 17:55:20 -0500 Subject: [PATCH 03/13] ex variable-scoping, constructor and arrow-functions --- paperproblems/arrow-functions/problem1.txt | 14 +- .../constructor-functions/problem2.txt | 4 +- .../constructor-functions/problem3.txt | 4 +- .../constructor-functions/problem4.txt | 4 +- .../constructor-functions/problem5.txt | 8 +- paperproblems/variable-scoping/problem1.txt | 4 + paperproblems/variable-scoping/problem2.txt | 6 +- paperproblems/variable-scoping/problem3.txt | 8 +- paperproblems/variable-scoping/problem4.txt | 4 +- src/HOF/problem1.js | 147 ++++++++++++++---- src/__proto__/problem1.js | 2 +- src/anonymous-functions/problem1.js | 2 +- src/arrow-functions/problem1.js | 2 +- src/arrow-functions/problem2.js | 13 +- src/bind/problem1.js | 4 +- src/bind/problem2.js | 6 +- src/constructor-functions/problem1.js | 6 + src/return/problem1.js | 8 +- src/this/problem1.js | 3 +- src/variable-scoping/problem1.js | 3 +- 20 files changed, 188 insertions(+), 64 deletions(-) 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/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 f47e2b8..7ecacb9 100644 --- a/paperproblems/constructor-functions/problem5.txt +++ b/paperproblems/constructor-functions/problem5.txt @@ -9,4 +9,10 @@ function Person(name, age) { } } -var bob = Person("Bob", 30); \ No newline at end of file +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) \ 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 ab1384c..33a161b 100644 --- a/src/anonymous-functions/problem1.js +++ b/src/anonymous-functions/problem1.js @@ -6,7 +6,7 @@ function c(g, h) { } function t() { - return c(function g(x) {return y + 1}, function h(y) {return x * 2}); + return c(function g(x) {return x + 1}, function h(y) {return y * 2}); } module.exports = t; \ No newline at end of file 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/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/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/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; } From 11a120dabbf288b167843bce0e5bf91e0f9b3aa5 Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 08:33:55 -0500 Subject: [PATCH 04/13] update --- paperproblems/callbacks/problem1.txt | 4 +++- paperproblems/callbacks/problem2.txt | 7 +++++- paperproblems/classes/problem1.txt | 2 +- paperproblems/classes/problem2.txt | 8 +++++-- .../conditional-operator/problem1.txt | 4 +++- paperproblems/inheritance/problem1.txt | 4 +++- paperproblems/inheritance/problem2.txt | 17 +++++++++++++ src/callbacks/problem1.js | 4 ++-- src/classes/problem1.js | 8 +++++++ src/conditional-operator/problem1.js | 20 +++++----------- src/conditional-operator/problem2.js | 7 ++++-- src/inheritance/problem1.js | 24 ++++++++++++++++--- src/inheritance/problem2.js | 13 ++++++---- 13 files changed, 89 insertions(+), 33 deletions(-) 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/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/src/callbacks/problem1.js b/src/callbacks/problem1.js index 0e8eb76..3d0b551 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(function() {shout("hello")}, 1000); \ 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..d3d2825 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,23 +1,15 @@ -//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"; - } + var y = x == 'Brad Pitt' ? "very desirable" : "not so desirable"; + return y; } + function broadenHorizon(x) { - if(x == 'Brad Pitt') { - return "very desirable"; - } else if(x == 'Angelina Jolie') { - return "also desirable"; - } else { - return "not desirable"; - } + var y = x == 'Brad Pitt'? "very desirable" : x == 'Angelina Jolie'? "also desirable" : "not desirable"; + return y; } 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..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/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 From 1d3e11d8e5a3ac1e799da85dbebda9f7f6774020 Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 08:50:49 -0500 Subject: [PATCH 05/13] update --- src/conditional-operator/problem1.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/conditional-operator/problem1.js b/src/conditional-operator/problem1.js index d3d2825..aeb68aa 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,15 +1,11 @@ - - - function desirability(x) { - var y = x == 'Brad Pitt' ? "very desirable" : "not so desirable"; - return y; + return x == 'Brad Pitt' ? "very desirable" : "not so desirable"; } function broadenHorizon(x) { - var y = x == 'Brad Pitt'? "very desirable" : x == 'Angelina Jolie'? "also desirable" : "not desirable"; - return y; + return x == 'Brad Pitt'? "very desirable" : x == 'Angelina Jolie'? "also desirable" : "not desirable"; + } module.exports = {desirability, broadenHorizon} \ No newline at end of file From d6ddad926136e5d2475c4b9a0c70ee32bfde0daa Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 08:53:13 -0500 Subject: [PATCH 06/13] update 2 --- src/conditional-operator/problem1.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conditional-operator/problem1.js b/src/conditional-operator/problem1.js index aeb68aa..0b5a879 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -1,3 +1,4 @@ + function desirability(x) { return x == 'Brad Pitt' ? "very desirable" : "not so desirable"; } From 3e8cd40c47dbdc0a27e774abad4256647c46a2bf Mon Sep 17 00:00:00 2001 From: Mamouel Date: Thu, 18 Jan 2018 08:55:18 -0500 Subject: [PATCH 07/13] Update problem1.js --- src/conditional-operator/problem1.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/conditional-operator/problem1.js b/src/conditional-operator/problem1.js index d3d2825..148f85b 100644 --- a/src/conditional-operator/problem1.js +++ b/src/conditional-operator/problem1.js @@ -2,14 +2,12 @@ function desirability(x) { - var y = x == 'Brad Pitt' ? "very desirable" : "not so desirable"; - return y; + return x == 'Brad Pitt' ? "very desirable" : "not so desirable"; } function broadenHorizon(x) { - var y = x == 'Brad Pitt'? "very desirable" : x == 'Angelina Jolie'? "also desirable" : "not desirable"; - return y; + 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} From d2e68ccb1d19c1c63a34b285f1329ca9ced97f28 Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 09:12:16 -0500 Subject: [PATCH 08/13] reverse cancelled --- src/exceptions/problem1.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..e6fff96 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,20 +1,44 @@ 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 } From b6743aea058b0a6a4f62c8d778363932f8727e11 Mon Sep 17 00:00:00 2001 From: Emmanuel DODIER Date: Thu, 18 Jan 2018 09:13:52 -0500 Subject: [PATCH 09/13] Update problem1.js --- src/exceptions/problem1.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) 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} From 5d33336bff5dea625ce5d21f9dfccaa5a73e756c Mon Sep 17 00:00:00 2001 From: Emmanuel DODIER Date: Thu, 18 Jan 2018 09:14:38 -0500 Subject: [PATCH 10/13] Update problem1.js --- src/exceptions/problem1.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index 5b631fd..1176dd2 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -5,11 +5,11 @@ function first(arr) { throw new Error("empty array"); } else return arr[0] } - try { - first([1,2]) - } catch(err) { - console.log(err) - } +try { + first([1,2]) +} catch(err) { + console.log(err) +} // Throw an exception if the array has no elements // Otherwise return the first element From 8811980f23ca4cdfdbbb2fd08a26bdc99cc37ded Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 10:50:25 -0500 Subject: [PATCH 11/13] =?UTF-8?q?callbacks=20am=C3=A9lior=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/callbacks/problem1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/callbacks/problem1.js b/src/callbacks/problem1.js index 3d0b551..9f04106 100644 --- a/src/callbacks/problem1.js +++ b/src/callbacks/problem1.js @@ -4,4 +4,4 @@ function shout(x) { console.log(x.toLowerCase()); } -setTimeout(function() {shout("hello")}, 1000); \ No newline at end of file +setTimeout(shout, 1000, "hello"); \ No newline at end of file From 173f1b173925a143c22ebdae6b40702ae867d89f Mon Sep 17 00:00:00 2001 From: mamouel Date: Thu, 18 Jan 2018 14:51:34 -0500 Subject: [PATCH 12/13] update 3 --- src/array-functions/problem1.js | 22 +++++++++++++++++++++- src/syntax-errors/problem1.js | 2 +- src/syntax-errors/problem2.js | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/array-functions/problem1.js b/src/array-functions/problem1.js index 06bc426..5af4384 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,8 @@ function greetLong(lst) { } function allLong(lst) { + for (i in lst) {lst[i].length > 4 ? 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/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 }, From dbe4f68e3ba96916827407e5aedcd04716dbe0c5 Mon Sep 17 00:00:00 2001 From: mamouel Date: Fri, 19 Jan 2018 10:03:00 -0500 Subject: [PATCH 13/13] last commit --- paperproblems/array-functions/problem1.txt | 4 +++- paperproblems/array-functions/problem2.txt | 4 +++- paperproblems/array-functions/problem3.txt | 4 +++- paperproblems/array-functions/problem4.txt | 4 ++-- src/array-functions/problem1.js | 5 +++-- 5 files changed, 14 insertions(+), 7 deletions(-) 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/src/array-functions/problem1.js b/src/array-functions/problem1.js index 5af4384..103eb67 100644 --- a/src/array-functions/problem1.js +++ b/src/array-functions/problem1.js @@ -40,8 +40,9 @@ function greetLong(lst) { } function allLong(lst) { - for (i in lst) {lst[i].length > 4 ? true : false}; - + 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. }