diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..65228cd Binary files /dev/null and b/.DS_Store differ 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/.DS_Store b/paperproblems/.DS_Store new file mode 100644 index 0000000..2c8e725 Binary files /dev/null and b/paperproblems/.DS_Store differ diff --git a/paperproblems/HOF/problem1.txt b/paperproblems/HOF/problem1.txt index 5985535..523714e 100644 --- a/paperproblems/HOF/problem1.txt +++ b/paperproblems/HOF/problem1.txt @@ -3,10 +3,11 @@ You don't know what it does, but there are clues at the bottom of this file Use these clues to describe what filter is. Specifically: - How many parameters does it have +two - What are the types of the parameters +array, function - What does it do? - - +it takes an array and applies a function which assesses whether or not an element of an array is divisible by itself, depending on if the function returns a boolean of true the element is either passed to a new array using.push or ignored from this new array. var evenNumbers = [2, 4, 6, 8, 10]; var oddNumbers = [3, 5, 7, 9]; diff --git a/src/HOF/problem1.js b/src/HOF/problem1.js index 0ed585e..f63a2a7 100644 --- a/src/HOF/problem1.js +++ b/src/HOF/problem1.js @@ -1,5 +1,15 @@ -function callNoException(f, arg) { + function callNoException(f, arg) { + + try { + f(arg); + } + catch(err) { + return null; + } + return arg; + + } // if f(arg) throws an exception, return null // otherwise return what f(arg) returned // Example: @@ -9,9 +19,28 @@ function callNoException(f, arg) { // } // callNoException(throwsZero, 0) returns null // callNoException(throwsZero, 12) returns 12 -} -function callNoNull(f, arg) { + + function callNoNull(f, arg) { + + + + try { f(arg) + if (f(arg) === null) { + throw "it's null"; + } + + + return arg + + } + catch(err){ throw new Error ("null alert!"); + + } +} + + + // if f(arg) returns null, throw an exception // otherwise return what f(arg) returned // Example: @@ -22,10 +51,40 @@ function callNoNull(f, arg) { // callNoNull(nullZero, 0) throws an exception // callNoNull(nullZero, 12) returns 12 + var exceptionalize = function(f) { + return function g(arg) { + arg = f + + if (arg !== null ); + return arg; + + + try { + + g(f); + + if (arg === null){ + + + throw new Error ("error!"); + } + + + + } -} -function exceptionalize(f) { + catch(err){ throw new Error ("bazinga!"); + + } + return arg + } + + return arg + + + } + // returns a new function // this function takes 1 input, called arg // if f(arg) is null, this new function throws an exception @@ -40,9 +99,40 @@ function exceptionalize(f) { // g(0) throws an exception // g(12) returns 12 -} -function nullify(f) { + + var nullify = function(f) { + return function newFunction(arg) { + var numNum = "" + try { + newFunction(arg); + if (arg == 0) throw new Error ("whoops"); + + numNum = arg; + return numNum + } + + catch(err) { + return null + } + return numNum; + + } + return function g(arg) { + try { + g(numNum) + + + return arg + } + catch (err) { + return null; + done() + } + } + + } + // returns a new function // this function takes 1 input, called arg // if f(arg) throws an exception, this new function returns null @@ -56,9 +146,13 @@ function nullify(f) { // g(0) returns null // g(12) throws an exception -} + function map(lst, f) { + lst = [a, b]; + f = {} +lst.map(function(lst){ return f.toUppercase()} + // lst is an array and f is a function // map returns an array with the same number of elements as lst // if lst = [a1, a2, a3, a4, a5] then map(lst, f) returns [f(a1), f(a2), f(a3), f(a4), f(a5)] @@ -69,7 +163,7 @@ function map(lst, f) { // // 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 diff --git a/src/__proto__/problem1.js b/src/__proto__/problem1.js index 820a78d..21857b0 100644 --- a/src/__proto__/problem1.js +++ b/src/__proto__/problem1.js @@ -2,5 +2,5 @@ var parent = {x: 5, y: 6, z: 8}; var child = {x : 10}; - +child.__proto__ = parent; module.exports = {parent, child} \ No newline at end of file diff --git a/src/exceptions/problem1.js b/src/exceptions/problem1.js index f54c8f8..96f321f 100644 --- a/src/exceptions/problem1.js +++ b/src/exceptions/problem1.js @@ -1,22 +1,51 @@ + + function first(arr) { // Throw an exception if the array has no elements // Otherwise return the first element + if (arr[0] === undefined) { + throw "error"; + } else { return arr[0]; + + } + } - +// console.log(first(arr1)); function detective(i) { function suspect(i) { - if(i * 7 % 3 == 0) throw new Error("Bad i!"); + if(i * 7 % 3 === 0) {throw new Error("Bad i!")}; + } + + try { + suspect(i); } + catch(err) { + return "something fishy"; + } + + return "everything ok" +} + + // detective checks to see if the suspect throws an exception on input i. // Returns "everything ok" if the suspect doesn't. // Returns "something fishy" if the suspect does. -} + function assignFlight(name) { var flightNumber = ((name.length * 7) % 20) + "0"; var terrorSuspects = ["bob", "eric", "susie"]; // if the name is a terror suspect, throw an exception // Otherwise, return the flight number + if (name == "bob" || name == "eric" || name == "susie") {throw new Error ("terrorist!"); + } + try { + assignFlight(name); + } + catch(err) { + return "terrorist!"; + } + return flightNumber } module.exports = {first, detective, assignFlight} \ No newline at end of file diff --git a/src/exceptions/problem1a.js b/src/exceptions/problem1a.js new file mode 100644 index 0000000..044d7cf --- /dev/null +++ b/src/exceptions/problem1a.js @@ -0,0 +1,46 @@ + + +function first(arr) { + // Throw an exception if the array has no elements + // Otherwise return the first element + if (arr[0] === undefined) { + throw "error"; + } else { return arr[0]; + + } + +} +// console.log(first(arr1)); +function detective(i) { + function suspect(i) { + if(i * 7 % 3 == 0) {throw new Error("Bad i!"); + + } else { throw new Error("everything ok") +} +} + try { + suspect(i); + } + catch(err) { + return console.log("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"]; + // 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..b6df18c 100644 --- a/src/return/problem1.js +++ b/src/return/problem1.js @@ -1,17 +1,16 @@ -// Remove as many characters from this function without changing its meaning. -// In other words, make this function as succinct as possible -// Also, remove these comments + function f(x) { if(x > 10) { return "hello"; } else if(x > 5) { return "goodbye"; - } else { - return undefined; - } + } + } -module.exports = f; // Don't delete this line but remove this comment. + + +module.exports = f; diff --git a/src/return/problem1a.js b/src/return/problem1a.js new file mode 100644 index 0000000..27c3d74 --- /dev/null +++ b/src/return/problem1a.js @@ -0,0 +1,17 @@ + + +function f(x) { + if(x > 10) { + return "hello"; + } else if(x > 5) { + return "goodbye"; + } else { + return undefined; + } +} + + + +module.exports = f; + +