From 465f682fa2d3729e3cb7d27fcea369ee76171d14 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 31 Oct 2019 00:10:58 +0100 Subject: [PATCH 01/37] week-1-project/app.js file completed --- week-1-project/app.js | 103 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 15 deletions(-) diff --git a/week-1-project/app.js b/week-1-project/app.js index fd5f1e0..3e6a77b 100644 --- a/week-1-project/app.js +++ b/week-1-project/app.js @@ -1,43 +1,116 @@ const object = { numberyStrings: [], NaNyStrings: [], + // evenStringsArr: [], isNumberyString: function (param) { - // write me! + // return typeof param === 'string' && !isNaN(param); + return typeof param !== 'string' ? false :isNaN(param) ? false : true; }, addString: function (param) { - if (null) return false; // write this early return condition - - // write me! (using this.isNumberyString) + if (typeof param !== 'string') return false; // write this early return condition + + else if (isNaN(param)) {this.NaNyStrings.push(param);} + + else if (!isNaN(param)) {this.numberyStrings.push(param);} + return true; }, allStrings: function () { - // write me! + + if (this.NaNyStrings.length===0) return this.numberyStrings; + else if(this.numberyStrings.length===0) return this.NaNyStrings; + else return this.numberyStrings.concat(this.NaNyStrings); }, evenStrings: function () { - // write me! - }, + + if ((this.NaNyStrings.length!==0) && (this.numberyStrings.length===0)) + return this.numberyStrings; + else if ((this.NaNyStrings.length===0) && (this.numberyStrings.length!==0)){ + function checkEven(num) {return num%2 === 0}; + return this.numberyStrings.filter(checkEven); + } + else if ((this.NaNyStrings.length!==0) && (this.numberyStrings.length!==0)){ + function checkEven(num) {return num%2 === 0}; + return this.numberyStrings.filter(checkEven); + } + }, oddStrings: function () { - // write me! + if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + return this.numberyStrings; + else if ((this.NaNyStrings.length===0) && (this.numberyStrings.length!==0)){ + function checkOdd(num) {return num%2 !== 0}; + return this.numberyStrings.filter(checkOdd); + } + else if ((this.NaNyStrings.length!==0) && (this.numberyStrings.length!==0)){ + function checkOdd(num) {return num%2 !== 0}; + return this.numberyStrings.filter(checkOdd); + } }, negativeStrings: function () { - // write me! + if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + return this.numberyStrings; + else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ + function checkNegative(num) {return num < 0}; + return this.numberyStrings.filter(checkNegative); + } + else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ + function checkNegative(num) {return num < 0}; + return this.numberyStrings.filter(checkNegative); + } }, positiveStrings: function () { - // write me! + if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + return this.numberyStrings; + else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ + function checkPozitive(num) {return num > 0}; + return this.numberyStrings.filter(checkPozitive); + } + else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ + function checkPozitive(num) {return (num > 0 || num === "")}; + return this.numberyStrings.filter(checkPozitive); + } }, zeroStrings: function () { - // write me! + if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + return this.numberyStrings; + else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ + function checkZero(num) {return (num == 0)} + return this.numberyStrings.filter(checkZero); + }else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ + function checkZero(num) {return (num == 0)} + return this.numberyStrings.filter(checkZero); + } }, numberyAsNumbers: function () { - // write me! + return this.numberyStrings.map(str => Number(str)); // in one line + + // in multiple lines + + /* if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + return this.numberyStrings; + else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ + function returnNumberyStrings(num) { return Number(num);} + return this.numberyStrings.map(returnNumberyStrings);} + else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ + function returnNumberyStrings(num) { return Number(num);} + return this.numberyStrings.map(returnNumberyStrings);} */ }, NaNyAsNumbers: function () { - // write me! + return this.NaNyStrings.map( str => Number(str)); }, sumOfNumbery: function () { - // write me! + if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0 ) { + return 0; + } + function add(a, b) { return (a + b) }; + return this.numberyAsNumbers().reduce(add); }, + sumOfNaNy: function () { - // write me! + if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0) { + return NaN; + } + function add(a, b) { return (a + b) }; + return this.NaNyAsNumbers().reduce(add); }, }; From c8007ae087d0e03412ffe5f7444ca62eeb7d7927 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Mon, 4 Nov 2019 13:59:05 +0100 Subject: [PATCH 02/37] week1 and week2 project files updated --- week-1-project/app.js | 25 ++++--- .../practice-problems/array-methods.js | 53 ++++++++------- .../avoiding-side-effects.js | 9 ++- .../practice-problems/functions-to-objects.js | 15 ++-- .../practice-problems/primitive-types.js | 68 +++++++++---------- week-2-project/practice-problems/index.html | 4 +- week-2-project/practice-problems/objects.js | 20 +++--- 7 files changed, 107 insertions(+), 87 deletions(-) diff --git a/week-1-project/app.js b/week-1-project/app.js index 3e6a77b..5ac4db8 100644 --- a/week-1-project/app.js +++ b/week-1-project/app.js @@ -17,8 +17,8 @@ const object = { allStrings: function () { if (this.NaNyStrings.length===0) return this.numberyStrings; - else if(this.numberyStrings.length===0) return this.NaNyStrings; - else return this.numberyStrings.concat(this.NaNyStrings); + if(this.numberyStrings.length===0) return this.NaNyStrings; + return this.numberyStrings.concat(this.NaNyStrings); }, evenStrings: function () { @@ -70,15 +70,18 @@ const object = { } }, zeroStrings: function () { - if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) - return this.numberyStrings; - else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ - function checkZero(num) {return (num == 0)} - return this.numberyStrings.filter(checkZero); - }else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ - function checkZero(num) {return (num == 0)} - return this.numberyStrings.filter(checkZero); - } + + return this.numberyStrings.filter(num => num == 0); + + // if (this.NaNyStrings.length !== 0 && this.numberyStrings.length === 0) + // return this.numberyStrings; + // else if (this.NaNyStrings.length === 0 && this.numberyStrings.length !== 0){ + // function checkZero(num) {return (num == 0)} + // return this.numberyStrings.filter(checkZero); + // }else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){ + // function checkZero(num) {return (num == 0)} + // return this.numberyStrings.filter(checkZero); + // } }, numberyAsNumbers: function () { return this.numberyStrings.map(str => Number(str)); // in one line diff --git a/week-1-project/practice-problems/array-methods.js b/week-1-project/practice-problems/array-methods.js index a815910..7ddd536 100644 --- a/week-1-project/practice-problems/array-methods.js +++ b/week-1-project/practice-problems/array-methods.js @@ -20,7 +20,7 @@ try { { name: 'fourth', args: [['hello'], ['world']], expected: ['hello', 'world'] }, ]; function concatArrays(arr1, arr2) { - // write me! + return [...arr1, ...arr2]; } concatArrays.display = true; evaluate(concatArrays, concatArraysTests); @@ -35,12 +35,13 @@ try { { name: 'seventh', args: ['Infinity'], expected: false }, { name: 'eighth', args: ['infinity'], expected: true }, { name: 'ninth', args: ['NaN'], expected: true }, - { name: 'tenth', args: [NaN], expected: null }, - { name: 'eleventh', args: [true], expected: null }, - { name: 'twelfth', args: [undefined], expected: null }, - { name: 'thirteenth', args: [null], expected: null }, + { name: 'tenth', args: [NaN], expected: true }, + { name: 'eleventh', args: [true], expected: false }, + { name: 'twelfth', args: [undefined], expected: true }, + { name: 'thirteenth', args: [null], expected: false }, ]; function isNaNyString(arg) { + return isNaN(arg); // write me! // can you write this in one line? (isNaN will be helpful) } @@ -61,13 +62,15 @@ try { { name: 'first', args: [thingsToNumber1], expected: [1, 2] }, { name: 'second', args: [thingsToNumber2], expected: [1, 10] }, { name: 'third', args: [thingsToNumber3], expected: [2, 0] }, - { name: 'fourth', args: [thingsToNumber4], expected: null }, - { name: 'fifth', args: [[1, 2, 3]], expected: null }, + { name: 'fourth', args: [thingsToNumber4], expected: [1, 2] }, + { name: 'fifth', args: [[1, 2, 3]], expected: [1, 2, 3] }, { name: 'sixth', args: [oddsToNumber], expected: [1, 3, 5] }, { name: 'seventh', args: [evensToNumber], expected: [2, 4, 6] }, ]; function returnAsNumbers(arr) { // return an array of nonNanny strings cast to Number - // write me! + let nonNaNyString = arr.filter(item => !(isNaNyString(item))); + let numberArray = nonNaNyString.map(item => Number(item)); + return numberArray // early return condition: array contains no numbery strings // consider using a variation of your solution to isNaNyString (and .every) }; @@ -89,10 +92,9 @@ try { { name: 'seventh', args: [numbersToSum3], expected: 2 }, ]; function sumAll(arr) { - // write me! - // no early return, all the test cases are numbers! - // this solution will be very helpful for the next exercise - }; + function add(a, b) { return (a + b) }; + return arr.reduce(add); + }; sumAll.display = true; evaluate(sumAll, sumAllTests); @@ -109,22 +111,23 @@ try { { name: 'first', args: [sumNumberys1], expected: 3 }, { name: 'second', args: [sumNumberys2], expected: 11 }, { name: 'third', args: [sumNumberys3], expected: 2 }, - { name: 'fourth', args: [sumNumberys4], expected: null }, - { name: 'fifth', args: [[1, 2, 3]], expected: null }, + { name: 'fourth', args: [sumNumberys4], expected: 3 }, + { name: 'fifth', args: [[1, 2, 3]], expected: 6 }, { name: 'sixth', args: [['1', '2', '3']], expected: 6 }, { name: 'seventh', args: [oddsToSum], expected: 9 }, { name: 'eighth', args: [evensToSum], expected: 12 }, ]; function sumAllNumberys(arr) { - // write me! + let numberyArr = arr.filter(item => !isNaNyString(item)) + .map(item => Number(item)); + return sumAll(numberyArr); + // early return condition: array contains no numbery strings }; sumAllNumberys.display = true; evaluate(sumAllNumberys, sumAllNumberysTests); - - const findEvensArray1 = ['.', '1', '2', ':']; const findEvensArray2 = ['1', 'two', 'three', '10']; const findEvensArray3 = ['one', '2', '', 'NaN']; @@ -137,13 +140,15 @@ try { { name: 'first', args: [findEvensArray1], expected: ['2'] }, { name: 'second', args: [findEvensArray2], expected: ['10'] }, { name: 'third', args: [findEvensArray3], expected: ['2', ''] }, - { name: 'fourth', args: [findEvensArray4], expected: null }, - { name: 'fifth', args: [[1, 2, 3]], expected: null }, + { name: 'fourth', args: [findEvensArray4], expected: [2] }, + { name: 'fifth', args: [[1, 2, 3]], expected: [2] }, { name: 'sixth', args: [oddsToNotFind], expected: [] }, { name: 'seventh', args: [evensToFind], expected: ['2', '4', '6'] }, ]; function findAllEvens(arr) { - // write me! + let numberyArr = arr.filter(item => !isNaNyString(item)); + let evenArr = numberyArr.filter(item => item%2===0) + return evenArr; // early return condition: array contains no numbery strings }; findAllEvens.display = true; @@ -163,13 +168,15 @@ try { { name: 'first', args: [findOddsArray1], expected: ['1'] }, { name: 'second', args: [findOddsArray2], expected: ['1'] }, { name: 'third', args: [findOddsArray3], expected: [] }, - { name: 'fourth', args: [findOddsArray4], expected: null }, - { name: 'fifth', args: [[1, 2, 3]], expected: null }, + { name: 'fourth', args: [findOddsArray4], expected: [1] }, + { name: 'fifth', args: [[1, 2, 3]], expected: [1, 3] }, { name: 'sixth', args: [oddsToFind], expected: ['1', '3', '5'] }, { name: 'seventh', args: [evensToNotFind], expected: [] }, ]; function findAllOdds(arr) { - // write me! + let numberyArr = arr.filter(item => !isNaNyString(item)); + let oddArr = numberyArr.filter(item => item%2!==0) + return oddArr; // early return condition: array contains no numbery strings }; findAllOdds.display = true; diff --git a/week-1-project/practice-problems/avoiding-side-effects.js b/week-1-project/practice-problems/avoiding-side-effects.js index 823001d..b77f5ef 100644 --- a/week-1-project/practice-problems/avoiding-side-effects.js +++ b/week-1-project/practice-problems/avoiding-side-effects.js @@ -206,7 +206,14 @@ try { { name: 'case 6', args: [['p', null], 2], expected: [['p', 'p'], [null, null]] }, ]; function repeatItems(items, numRepeats) { - // write me! + let result = items.map(e => { + const arr1 = [] + for (let i=0; i< numRepeats; i++){ + arr1.push(e); + } + return arr1; + }); + return result; } repeatItems.display = true; evaluate(repeatItems, repeatItemsTests); diff --git a/week-1-project/practice-problems/functions-to-objects.js b/week-1-project/practice-problems/functions-to-objects.js index 7348a1c..67aa127 100644 --- a/week-1-project/practice-problems/functions-to-objects.js +++ b/week-1-project/practice-problems/functions-to-objects.js @@ -83,10 +83,10 @@ try { // methods can "return" values to their objects obj.a = 3, obj.b = 4; - obj.sum(); + obj.sum(3,4); console.assert(7 === obj.result, 'obj.result should be 7'); - obj.a = 5; - obj.sum(); + obj.a = 5; obj.b = 4; + obj.sum(5,4); console.assert(9 === obj.result, 'obj.result should be 9'); } @@ -111,13 +111,13 @@ try { this.a = a; this.b = b; }, - sumAandB: function () { + sumAandB: function (a, b) { return this.a + this.b; } }; obj.setAandB(3, 4); - const result2 = obj.sumAandB(); + const result2 = obj.sumAandB(3, 4); } evaluate(example_twoMethods); @@ -132,7 +132,10 @@ try { const obj = { array: [3], - mergeArrays: function (arrToMerge) { } + mergeArrays: function (arrToMerge) { + // return [...this.array, ...arrToMerge]; + return this.array.concat(arrToMerge); + } } diff --git a/week-1-project/practice-problems/primitive-types.js b/week-1-project/practice-problems/primitive-types.js index 360d7a0..05c2947 100644 --- a/week-1-project/practice-problems/primitive-types.js +++ b/week-1-project/practice-problems/primitive-types.js @@ -59,27 +59,27 @@ try { // the type of a value is very important to understanding how JS works const typeofTests = [ // boolean values - { name: 'boo, true', args: [true], expected: '' }, - { name: 'boo, false', args: [false], expected: '' }, - // null's type is 'null'. just remember, don't try yet to understand - { name: 'obj, true', args: [null], expected: '' }, + { name: 'boo, true', args: [true], expected: 'boolean' }, + { name: 'boo, false', args: [false], expected: 'boolean' }, + // null's type is 'object'. just remember, don't try yet to understand + { name: 'obj, true', args: [null], expected: 'object' }, // undefined. like with null, there is only one value with this type - { name: 'und, undefined', args: [undefined], expected: '' }, + { name: 'und, undefined', args: [undefined], expected: 'undefined' }, // strings are anything with quotes around it - { name: 'str, ', args: [''], expected: '' }, - { name: 'str, anything with quotes!', args: ['anything with quotes!'], expected: '' }, + { name: 'str, ', args: [''], expected: 'string' }, + { name: 'str, anything with quotes!', args: ['anything with quotes!'], expected: 'string' }, // numbers are a bit more strange and varied { name: 'num, 0.0', args: [0.0], expected: 'number' }, { name: 'num, NaN', args: [NaN], expected: 'number' }, { name: 'num, Infinity', args: [Infinity], expected: 'number' }, { name: 'num, 4', args: [4], expected: 'number' }, // write 6 more passing test cases with expected value 'number' - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, + { name: 'str, 4', args: ['4'], expected: 'string' }, + { name: 'boo, true', args: [true], expected: 'boolean' }, + { name: 'obj, arr', args: ['[arr'], expected: 'string' }, + { name: 'str, hello!', args: ['hello!'], expected: 'string' }, + { name: 'num', args: [NaN], expected: 'number' }, + { name: 'num', args: [Infinity], expected: 'number' }, ] function allValuesHaveAType(value) { return typeof value; @@ -99,7 +99,7 @@ try { ]; function typeofReturnsAString(value) { const typeofValue = typeof value; - return typeof typeofValue; + return typeofValue; } typeofReturnsAString.quizzing = true; evaluate(typeofReturnsAString, typeofReturnsAStringTests); @@ -125,16 +125,16 @@ try { // fix the expected values to pass the tests const strictEqualityTests = [ - { name: 'NaN', args: [NaN, NaN], expected: null }, - { name: 'first', args: [true, 'true'], expected: null }, - { name: 'second', args: [1, '1'], expected: null }, - { name: 'third', args: ['1', '1'], expected: null }, - { name: 'fourth', args: [1000, 1e3], expected: null }, - { name: 'fifth', args: [+0, -0], expected: null }, - { name: 'sixth', args: [1, 1.0], expected: null }, - { name: 'seventh', args: ['', ""], expected: null }, - { name: 'eighth', args: ["", ``], expected: null }, - { name: 'ninth', args: [' ', ' '], expected: null }, + { name: 'NaN', args: [NaN, NaN], expected: false }, + { name: 'first', args: [true, 'true'], expected: false }, + { name: 'second', args: [1, '1'], expected: false }, + { name: 'third', args: ['1', '1'], expected: true }, + { name: 'fourth', args: [1000, 1e3], expected: true }, + { name: 'fifth', args: [+0, -0], expected: true }, + { name: 'sixth', args: [1, 1.0], expected: true }, + { name: 'seventh', args: ['', ""], expected: true }, + { name: 'eighth', args: ["", ``], expected: true }, + { name: 'ninth', args: [' ', ' '], expected: false }, ]; function strictEquality(a, b) { // if type OR value are not the same, returns false @@ -147,16 +147,16 @@ try { const strictInequalityTests = [ - { name: 'NaN', args: [NaN, NaN], expected: null }, - { name: 'first', args: [true, 'true'], expected: null }, - { name: 'second', args: [1, '1'], expected: null }, - { name: 'third', args: ['1', '1'], expected: null }, - { name: 'fourth', args: [1000, 1e3], expected: null }, - { name: 'fifth', args: [+0, -0], expected: null }, - { name: 'sixth', args: [1, 1.0], expected: null }, - { name: 'seventh', args: ['', ""], expected: null }, - { name: 'eighth', args: ["", ``], expected: null }, - { name: 'ninth', args: [' ', ' '], expected: null }, + { name: 'NaN', args: [NaN, NaN], expected: true }, + { name: 'first', args: [true, 'true'], expected: true }, + { name: 'second', args: [1, '1'], expected: true }, + { name: 'third', args: ['1', '1'], expected: false }, + { name: 'fourth', args: [1000, 1e3], expected: false }, + { name: 'fifth', args: [+0, -0], expected: false }, + { name: 'sixth', args: [1, 1.0], expected: false }, + { name: 'seventh', args: ['', ""], expected: false }, + { name: 'eighth', args: ["", ``], expected: false }, + { name: 'ninth', args: [' ', ' '], expected: true }, ]; function strictInequality(a, b) { // if type OR value are not the same, returns true diff --git a/week-2-project/practice-problems/index.html b/week-2-project/practice-problems/index.html index 3b4bfc6..9d592fc 100644 --- a/week-2-project/practice-problems/index.html +++ b/week-2-project/practice-problems/index.html @@ -22,8 +22,8 @@ - - + +
diff --git a/week-2-project/practice-problems/objects.js b/week-2-project/practice-problems/objects.js index 003bc42..db0580e 100644 --- a/week-2-project/practice-problems/objects.js +++ b/week-2-project/practice-problems/objects.js @@ -114,34 +114,34 @@ try { function passTheAssertions1() { - ; // declare and assign a1 - ; // declare and assign a2 + const a1 = {}; // declare and assign a1 + const a2 = a1; // declare and assign a2 console.assert(a1 === a2, 'a1 should strictly equal a2'); - ; // declare and assign b1 - ; // declare and assign b2 + const b1 = {}; // declare and assign b1 + const b2 = {}; // declare and assign b2 console.assert(b1 !== b2, 'b1 should not strictly equal b2'); // --- - ; // write one line to pass the assertions + a1.x = 'hi!'; // write one line to pass the assertions console.assert(a1.x === a2.x, 'a1.x should strictly equal a2.x'); console.assert(a1.x === 'hi!', 'a1.x should strictly equal "hi!"'); - ; // write two lines to pass the assertions - ; + b1.x = 'bye!'; // write two lines to pass the assertions + b2.x = 'bye!'; console.assert(b1.x === b2.x, 'b1.x should strictly equal b2.x'); console.assert(b1.x === 'bye!', 'b1.x should strictly equal "bye!"'); // -- const index = 'y'; - ; // write one line to pass the assertions + a1[index] = 'roof!'; // write one line to pass the assertions console.assert(a1[index] === a2[index], 'a1[index] should strictly equal a2[index]'); console.assert(a1[index] === 'roof!', 'a1[index] should strictly equal "roof!"'); - ; // write two lines to pass the assertions - ; + b1[index]='floor!'; // write two lines to pass the assertions + b2[index]='floor!'; console.assert(b1[index] === b2[index], 'b1[index] should strictly equal b2[index]'); console.assert(b1[index] === 'floor!', 'b1[index] should strictly equal "floor!"'); From 798b1d1250f8523a6697107e8399a704c53251fa Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Mon, 4 Nov 2019 16:19:27 +0100 Subject: [PATCH 03/37] week1 explicit coercion exercises completed --- .../practice-problems/explicit-coercion.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/week-1-project/practice-problems/explicit-coercion.js b/week-1-project/practice-problems/explicit-coercion.js index 3beed6b..761a9a0 100644 --- a/week-1-project/practice-problems/explicit-coercion.js +++ b/week-1-project/practice-problems/explicit-coercion.js @@ -23,12 +23,12 @@ try { // fix the test cases' expected values to pass the function const StringTests = [ // string values remain unchanged - { name: 'str, any string', args: ['any string'], expected: null }, + { name: 'str, any string', args: ['any string'], expected: 'any string' }, // casting with String just puts quotes around a thing - { name: 'num, 3', args: [3], expected: null }, - { name: 'boo, true', args: [true], expected: null }, - { name: 'obj, null', args: [null], expected: null }, - { name: 'und, undefined', args: [undefined], expected: null }, + { name: 'num, 3', args: [3], expected: '3' }, + { name: 'boo, true', args: [true], expected: 'true' }, + { name: 'obj, null', args: [null], expected: 'null' }, + { name: 'und, undefined', args: [undefined], expected: 'undefined' }, // write at least 5 more test cases for the String function ]; String.quizzing = true; @@ -45,11 +45,11 @@ try { { name: 'num, Infinity', args: [Infinity], expected: Infinity }, { name: 'num, NaN', args: [NaN], expected: NaN }, // true and false, the only boolean values - { name: 'boo, true', args: [true], expected: 0 }, - { name: 'boo, false', args: [false], expected: 1 }, + { name: 'boo, true', args: [true], expected: 1 }, + { name: 'boo, false', args: [false], expected: 0 }, // null & undefined - { name: 'obj, null', args: [null], expected: NaN }, - { name: 'und, undefined', args: [undefined], expected: 0 }, + { name: 'obj, null', args: [null], expected: 0 }, + { name: 'und, undefined', args: [undefined], expected: NaN }, // strings are bit more interesting, write 4 more test cases with string args { name: 'str, undefined', args: ['undefined'], expected: NaN }, { name: 'str, Infinity', args: ['Infinity'], expected: Infinity }, @@ -68,19 +68,19 @@ try { { name: 'boo, false', args: [false], expected: false }, // anything but 0 & NaN is cast to true { name: 'num, 3', args: [3], expected: true }, - { name: 'num, 0', args: [0], expected: true }, + { name: 'num, 0', args: [0], expected: false }, { name: 'num, 1e3', args: [1000], expected: true }, - { name: 'num, Infinity', args: [Infinity], expected: false }, + { name: 'num, Infinity', args: [Infinity], expected: true }, { name: 'num, NaN', args: [NaN], expected: false }, // null & undefined - { name: 'obj, null', args: [null], expected: true }, - { name: 'und, undefined', args: [undefined], expected: true }, + { name: 'obj, null', args: [null], expected: false }, + { name: 'und, undefined', args: [undefined], expected: false }, // anything but an empty string is cast to true - { name: 'str, undefined', args: ['undefined'], expected: false }, - { name: 'str, false', args: ['false'], expected: false }, + { name: 'str, undefined', args: ['undefined'], expected: true }, + { name: 'str, false', args: ['false'], expected: true }, { name: 'str, Infinity', args: ['Infinity'], expected: true }, { name: 'str, three', args: ['three'], expected: true }, - { name: 'str, ', args: [''], expected: true }, + { name: 'str, ', args: [''], expected: false }, { name: 'str, 3', args: ['3'], expected: true }, ]; Boolean.quizzing = true; From bfc0c82e6a6f12fe0b05cd2d069f5973e79b2b47 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Tue, 5 Nov 2019 10:51:18 +0100 Subject: [PATCH 04/37] completed early return pattern exercises --- .../practice-problems/early-return-pattern.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/week-1-project/practice-problems/early-return-pattern.js b/week-1-project/practice-problems/early-return-pattern.js index e83285b..bc9eae6 100644 --- a/week-1-project/practice-problems/early-return-pattern.js +++ b/week-1-project/practice-problems/early-return-pattern.js @@ -93,10 +93,10 @@ try { { name: 'fifth', args: [2, 3], expected: 'hi!' }, ]; function earlyReturn1(a, b) { - if (null) return 'string'; // replace null with your logic! + if (typeof a === 'string') return 'string'; // replace null with your logic! console.assert(typeof a !== 'string', 'if a is a string, this assert should not be reached'); - if (null) return 'boolean'; // replace null with your logic! + if (typeof b === 'boolean') return 'boolean'; // replace null with your logic! console.assert(typeof b !== 'boolean', 'if b is a boolean, this assert should not be reached'); return 'hi!'; @@ -114,15 +114,12 @@ try { { name: 'sixth', args: [], expected: 'param must be an array' }, ]; function earlyReturn2(param) { - if (null) return 'param must be an array'; // replace null with your logic! - + if (!(param instanceof Array)) return 'param must be an array'; // replace null with your logic! return param.reduce((acc, item) => acc + item); } earlyReturn2.display = true; evaluate(earlyReturn2, earlyReturn2Tests); - - const earlyReturn3Tests = [ { name: 'first', args: [4, '4'], expected: 'a is not a string' }, { name: 'second', args: ['4', 4], expected: 'b is not a string' }, @@ -132,9 +129,9 @@ try { { name: 'sixth', args: ['by', 'e!'], expected: 'bye!' }, ]; function earlyReturn3(a, b) { - if (null) return 'a & b are not strings'; // replace null with your logic! - if (null) return 'a is not a string'; // replace null with your logic! - if (null) return 'b is not a string'; // replace null with your logic! + if (typeof a !== 'string' && typeof b !== 'string') return 'a & b are not strings'; // replace null with your logic! + if (typeof a !== 'string') return 'a is not a string'; // replace null with your logic! + if (typeof b !== 'string') return 'b is not a string'; // replace null with your logic! console.assert(typeof a === 'string', 'a should be a string'); console.assert(typeof b === 'string', 'b should be a string'); @@ -156,8 +153,8 @@ try { { name: 'seventh', args: [() => { }], expected: 'argForFunc must be a boolean' }, ]; function earlyReturn4(func, argForFunc) { - if (null) return 'func must be a function'; // replace null with your logic! - if (null) return 'argForFunc must be a boolean'; // replace null with your logic! + if (typeof func !== 'function') return 'func must be a function'; // replace null with your logic! + if (typeof argForFunc !== 'boolean') return 'argForFunc must be a boolean'; // replace null with your logic! console.assert(typeof func === 'function', 'func should be a function'); console.assert(typeof argForFunc === 'boolean', 'argForFunc should be a boolean'); From bd99a0c22b194c5fb0e2d64dfaeff0d6e610f180 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Tue, 5 Nov 2019 17:36:48 +0100 Subject: [PATCH 05/37] week1 array exercises completed --- week-1-project/practice-problems/arrays.js | 41 +++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/week-1-project/practice-problems/arrays.js b/week-1-project/practice-problems/arrays.js index c5add90..16d3cb9 100644 --- a/week-1-project/practice-problems/arrays.js +++ b/week-1-project/practice-problems/arrays.js @@ -102,22 +102,23 @@ try { function passTheAssertions1() { - ; // declare and assign a1 - ; // declare and assign a2 + let a1 = 2; // declare and assign a1 + let a2 = a1; // declare and assign a2 console.assert(a1 === a2, 'a1 should strictly equal a2'); - ; // declare and assign b1 - ; // declare and assign b2 + let b1 = 3; // declare and assign b1 + let b2 = 4; // declare and assign b2 console.assert(b1 !== b2, 'b1 should not strictly equal b2'); // --- - ; // write one line to pass the assertions + a1 = a2 = ["hi!"]; // write one line to pass the assertions console.assert(a1[0] === a2[0], 'a1[0] should strictly equal a2[0]'); console.assert(a1[0] === 'hi!', 'a1.x should strictly equal "hi!"'); - ; // write two lines to pass the assertions - ; + b1 = ['bye!']; // write two lines to pass the assertions + b2 = b1 = ['bye!']; + console.assert(b1[0] === b2[0], 'b1[0] should strictly equal b2[0]'); console.assert(b1[0] === 'bye!', 'b1.x should strictly equal "bye!"'); } @@ -128,32 +129,32 @@ try { const value1 = 5; let reference1 = []; - ; // write this line + let value2 = value1; // write this line console.assert(value2 === value1, "value1 should strictly equal value2"); - ; // write this line + let reference2 = reference1; // write this line console.assert(reference2 === reference1, "reference1 should strictly equal reference2"); value2 = value2 + 1; // write this line - console.assert(value1 !== null, "value1 should strictly equal ___"); + console.assert(value1 !== value2, "value1 should strictly equal ___"); - ; // write this line + reference1 = reference2 = ['hi!']; // write this line console.assert(reference1[0] === reference2[0], "references[0] should be strictly equal"); console.assert(reference1[0] === 'hi!', "reference1[0] should be strictly equal to 'hi!'"); - ; // write this line + reference1 === reference2; // write this line console.assert(reference1 === reference2, "references should be strictly equal"); // remove the array from memory - ; // write this line - ; // write this line + reference1 = null; // write this line + reference2 = null; // write this line } evaluate(passTheAssertions2); function passTheAssertions3() { - ; // write this line - ; // write this line + arr1 = ['A', 'B']; // write this line + arr2 = ['A', 'B']; // write this line console.assert(arr1 !== arr2, 'the variables should not be strictly equal'); console.assert(arr1[1] === arr2[1], 'their first entries should be the same'); console.assert(arr1[1] === 'B', 'arr1[1]] should be "B"'); @@ -162,18 +163,18 @@ try { console.assert(arr1[index] === arr2[index], 'arr1[index] should strictly equal arr2[index]'); console.assert(arr1[index] === 'A', 'arr1[index] should be "A"'); - ; // write this line - ; // write this line + arr1 = ['A','B','1','2']; // write this line + arr2 = ['A','B','1']; // write this line console.assert(arr1[arr2[2]] === 'B', 'arr2[2] should be "B"s index in arr1'); console.assert(arr1[arr2[2]] === arr2[arr1[2]], 'some tricky nested thing should be true'); - ; // write this line + let arr3 = arr2 = ['A','B','1']; // write this line console.assert(arr1 !== arr2, 'arr1 should strictly equal arr2'); console.assert(arr3 !== arr1, 'arr3 should not strictly equal arr`'); console.assert(arr3 === arr2, 'arr3 should strictly equal arr2'); console.assert(arr3[index] === arr1[0], 'arr3[index] should strictly equal arr1[0]'); - ; // write this line + arr3 = ['A', 'B', 'A']; // write this line console.assert(arr3[2] === arr2[index], 'arr3[2] should strictly equal arr2[index]'); } evaluate(passTheAssertions3); From 0c3635a26847d3b90f0b3d371fd4ebb83f18178b Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Tue, 5 Nov 2019 20:19:23 +0100 Subject: [PATCH 06/37] week1 avoiding side effects exercises completed --- .../avoiding-side-effects.js | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/week-1-project/practice-problems/avoiding-side-effects.js b/week-1-project/practice-problems/avoiding-side-effects.js index b77f5ef..a516042 100644 --- a/week-1-project/practice-problems/avoiding-side-effects.js +++ b/week-1-project/practice-problems/avoiding-side-effects.js @@ -152,7 +152,8 @@ try { { name: 'case 8', args: [mergingObject3, mergingObject2], expected: { x: 0, y: 2, a: 1 } }, ]; function mergeObjects(obj1, obj2) { - // write me! + const objCopy = Object.assign({}, obj2); + return Object.assign(objCopy, obj1); } mergeObjects.display = true; evaluate(mergeObjects, mergeObjsTests); @@ -169,7 +170,12 @@ try { { name: 'case 6', args: [['p', null], 0, null], expected: [null, null] }, ]; function replaceItem(arr, index, newItem) { - // write me! + let newArray = [...arr]; + newArray[index] = newItem; + return newArray; + // arr[index]=newItem; + // return arr; + } replaceItem.display = true; evaluate(replaceItem, replaceItemTests); @@ -188,7 +194,8 @@ try { { name: 'case 6', args: [combineArray2, [undefined]], expected: ['p', null, Infinity, undefined] }, ]; function combineArrays(arr1, arr2) { - // write me! + let newArray = [...arr1,...arr2]; + return newArray; } combineArrays.display = true; evaluate(combineArrays, combineArraysTests); @@ -232,8 +239,9 @@ try { { name: 'fourth', args: [['hello'], ['world']], expected: ['world', 'hello'] }, ]; function concatArrays(arr1, arr2) { - // write me! - } + let newArray = arr2.concat(arr1); + return newArray; + } concatArrays.display = true; evaluate(concatArrays, concatArraysTests); @@ -251,8 +259,14 @@ try { { name: 'fifth', args: [arrayToMerge2, arrayToMerge1], expected: [2, 3, 4, 1] }, ]; function mergeArrays(arr1, arr2) { - // write me! - // consider filtering one of the arrs with .indexOf in the others + let filtered = [...arr2]; + for (let i = 0; i < filtered.length; i++) { + if (arr1.indexOf(filtered[i]) >= 0){ + filtered.splice(i,1); + i--; + } + } + return [...arr1,...filtered]; } mergeArrays.display = true; evaluate(mergeArrays, mergeArraysTests); From db30042682cf385b2689660ec1e987da206a9b88 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Tue, 5 Nov 2019 22:35:08 +0100 Subject: [PATCH 07/37] week1 function to objects exercises completed --- .../practice-problems/functions-to-objects.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/week-1-project/practice-problems/functions-to-objects.js b/week-1-project/practice-problems/functions-to-objects.js index 67aa127..8ca9bbb 100644 --- a/week-1-project/practice-problems/functions-to-objects.js +++ b/week-1-project/practice-problems/functions-to-objects.js @@ -169,7 +169,9 @@ try { const obj = { array: [3], - mergeArrays: function (arrToMerge) { } + mergeArrays: function (arrToMerge) { + return this.array = [...this.array, ...arrToMerge]; + } } obj.mergeArrays([2]); @@ -195,7 +197,9 @@ try { const obj = { mixer: '', - remix: function (str) { } + remix: function (str) { + return str.split('').join(this.mixer); + } }; console.assert(obj.remix('hello') === 'hello', 'assert 1'); @@ -219,8 +223,10 @@ try { const obj = { mixer: '', remixed: '', - remix: function (str) { }, - getRemixed: function () { } + remix: function (str) { + this.remixed = str.split('').join(this.mixer); + }, + getRemixed: function () {return this.remixed} }; obj.remix('hello'); From d20d9278e58777f5d4fe280d82c05c4cfab19aa1 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Tue, 5 Nov 2019 22:37:53 +0100 Subject: [PATCH 08/37] Update README.md --- week-1-project/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week-1-project/README.md b/week-1-project/README.md index 623d867..81972dc 100644 --- a/week-1-project/README.md +++ b/week-1-project/README.md @@ -1,3 +1,6 @@ +## Week1 Practice Exercises Link + week-1-project/practice-problems/index.html + ## Week 1 Project The weekly projects in JS 2 will be making an even stronger distinction between your core application and the user interface. To help you with this transition there will be two assignment tables, one for the core app object and another for the user interface. From 6b15cb9348b472cb4df3a06e246935a766c2f59f Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Tue, 5 Nov 2019 22:40:40 +0100 Subject: [PATCH 09/37] Update README.md --- week-1-project/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week-1-project/README.md b/week-1-project/README.md index 81972dc..6cde7d0 100644 --- a/week-1-project/README.md +++ b/week-1-project/README.md @@ -1,5 +1,7 @@ ## Week1 Practice Exercises Link - week-1-project/practice-problems/index.html +

Please follow the link below to open the practice exercises of Week1 project

+ +Practice Exercises Week1

## Week 1 Project From 6b78a6c94b9632d0ec15bd53b07789c0d17387bf Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Tue, 5 Nov 2019 22:43:21 +0100 Subject: [PATCH 10/37] Update README.md --- week-1-project/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week-1-project/README.md b/week-1-project/README.md index 6cde7d0..b2e48ad 100644 --- a/week-1-project/README.md +++ b/week-1-project/README.md @@ -1,7 +1,7 @@ ## Week1 Practice Exercises Link -

Please follow the link below to open the practice exercises of Week1 project

+ ### Please follow the link below to open the practice exercises of Week1 project -Practice Exercises Week1

+Practice Exercises Week1

## Week 1 Project From 103d076e86f0bf0c360d8ce598b8a7813f7cf101 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Tue, 5 Nov 2019 22:50:21 +0100 Subject: [PATCH 11/37] Update README.md --- week-1-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1-project/README.md b/week-1-project/README.md index b2e48ad..ca008b2 100644 --- a/week-1-project/README.md +++ b/week-1-project/README.md @@ -1,7 +1,7 @@ ## Week1 Practice Exercises Link ### Please follow the link below to open the practice exercises of Week1 project -Practice Exercises Week1

+Practice Exercises Week1

## Week 1 Project From 42e282391e722159c3b193a0eb1acdc79dec32b3 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Tue, 5 Nov 2019 23:01:09 +0100 Subject: [PATCH 12/37] week2 and week3 files updated --- week-2-project/README.md | 79 +++++-- week-2-project/app.js | 11 +- .../practice-problems/arrays-vs-objects.js | 4 +- week-2-project/tests/add-entry.js | 4 +- week-2-project/tests/find-by-key.js | 4 +- week-2-project/tests/find-by-value.js | 14 +- week-2-project/tests/remove-entry.js | 4 +- week-2-project/tests/update-entry.js | 4 +- week-3-project/README.md | 80 +++++-- week-3-project/app.js | 68 +----- .../error-based-decisions.js | 124 +++++++++++ .../practice-problems/getters-and-setters.js | 206 +++++++++++++++++- week-3-project/practice-problems/index.html | 1 + week-3-project/tests/add-entry.js | 4 +- week-3-project/tests/find-by-key.js | 4 +- week-3-project/tests/get-current-entry.js | 14 ++ week-3-project/tests/remove-entry.js | 4 +- week-3-project/tests/update-entry.js | 4 +- .../user-interface/handlers/favoriting.js | 23 ++ week-3-project/user-interface/index.html | 104 +++++---- 20 files changed, 589 insertions(+), 171 deletions(-) create mode 100644 week-3-project/practice-problems/error-based-decisions.js create mode 100644 week-3-project/user-interface/handlers/favoriting.js diff --git a/week-2-project/README.md b/week-2-project/README.md index 4591553..abad0a2 100644 --- a/week-2-project/README.md +++ b/week-2-project/README.md @@ -1,19 +1,60 @@ -practice problems -- -- -- -- - -app object -- -- -- -- -- - -user interface -- -- -- -- -- + +## Week 2 Project + + + +The weekly projects in JS 2 will be making an even stronger distinction between your core application and the user interface. To help you with this transition there will be two assignment tables, one for the core app object and another for the user interface. + + + +What you should notice and think about is that there is not a perfect 1-1 pairing between user stories (what is seen on the screen) and the core object (the software that makes the UI possible). Some User Stories are all about the UI/UX and don't require any changes in the core object, while some methods in the core object have no direct representation in the UI. + + + +--- + + + +### Core Object + + + +> this table is written like documentation + + + + +| __it should ...__ | __syntax__ | __parameters__ | __return value__ | __description__ | +| --- | --- | --- | --- | --- | +| _... determines if values are primitive or not_ | ```obj.isPrimitive(value)``` | _value_: any JS value | Boolean | It returns ```true``` if the argument is a primitive, otherwise it returns ```false```. | +| _... determines if an object has a given key_ | ```obj.hasKey(obj, key)``` | _obj_: an object
_key_: a string | Boolean | It returns ```true``` if the object has the given key, otherwise it returns ```false```. | +| _... determines if an object has a given value_ | ```obj.hasValue(obj, value)``` | _obj_: an object
_value_: any JS type | Boolean | It returns ```true``` if any key in the object stores this value, otherwise it returns ```false```. | +| _... adds key/value pairs to ```this.entries```_ | ```obj.addEntry(key, value)``` | _key_: a string
_value_: any primitive value | ```true``` or an error | It returns ```true``` if the key/value pair was successfully added, otherwise it returns helpful error describing what went wrong. | +| _... removes a key/value pair from ```this.entries```_ | ```obj.removeEntry(key)``` | _key_: a string | ```true``` or an error | It returns ```true``` if the key/value pair was successfully removed, otherwise it returns helpful error describing what went wrong. | +| _... updates a key/value pair in ```this.entries```_ | ```obj.updateEntry(key, value)``` | _key_: a string
_value_: any primitive type | ```true``` or an error | It returns ```true``` if the key/value pair was successfully updated, otherwise it returns helpful error describing what went wrong. | +| _... returns all key/value pairs in ```this.entries```_ | ```obj.readAll()``` | (no parameters) | an object | a new object with the same key/value pairs as ```this.entries``` | +| _... finds an entry by key_ | ```obj.findByKey(key)``` | _key_: a string | an object or an error | It returns an object with the given key, and it's value in ```this.entries```. Or a helpful error | +| _... finds an entry by value | ```obj.findByValue(value)``` | _value_: a primitive | an object or an error | It returns an object with all key/value pairs in ```this.entries``` containing the given value. Or a helpful error | + + + +--- + + + +### User Interface + + + +> this table is written as user stories + + + +| __As a/n__ ... | __I can__ ... | __so that__ ... | +| --- | --- | --- | +| _... enthusiastic JS student_ | ... know what this site does and how to use it | ... I can use it to organize my favorite primitive values | +| _... enthusiastic JS student_ | ... set input new keys and primitive values | ... I can practice always being aware of what types _and_ values my application stores | +| _... enthusiastic JS student_ | ... select a method to call with my inputs | ... modify the values stored in this app, and search for particular entries | +| _... enthusiastic JS student_ | ... see the values stored in ```this.entries``` rendered to the DOM | ... I can easily know what is stored in the app without console.logging | +| _... HYF coach_ | ... resize the browser window | ... I can test your responsive design | +| _... HYF coach_ | ... inspect the web page | ... I can see if you correctly used HTML5 semantic elements, CSS classes, and generally wrote clean code | diff --git a/week-2-project/app.js b/week-2-project/app.js index 81aeeae..1eec39e 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -8,6 +8,9 @@ - and users can access & modify that data */ + + + const object = { entries: {}, isPrimitive: function (value) { @@ -27,7 +30,7 @@ const object = { return new TypeError('addEntry: value should be a primitive'); } if (null) { // write me! (using this.hasKey) - return { [key]: new Error(`addEntry: key "${key}" already exists`) }; + return new Error(`addEntry: key "${key}" already exists`); } // write me! @@ -37,7 +40,7 @@ const object = { return new TypeError('removeEntry: key should be a string'); } if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`removeEntry: no property "${key}" in this.entries`) }; + return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } // write me! @@ -50,7 +53,7 @@ const object = { return new TypeError('updateEntry: value should be a primitive'); } if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`updateEntry: no property "${key}" in this.entries`) }; + return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); } // write me! @@ -63,7 +66,7 @@ const object = { return new TypeError('findByKey: key should be a string'); } if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`findByKey: no property "${key}" in this.entries`) }; + return new ReferenceError(`findByKey: no property "${key}" in this.entries`); } // write me! diff --git a/week-2-project/practice-problems/arrays-vs-objects.js b/week-2-project/practice-problems/arrays-vs-objects.js index 4a7e82c..35261ea 100644 --- a/week-2-project/practice-problems/arrays-vs-objects.js +++ b/week-2-project/practice-problems/arrays-vs-objects.js @@ -35,8 +35,8 @@ try { // asserts - console.assert(obj[obj_key] === "object", "obj assert"); - console.assert(arr[arr_index] === "array", "arr assert"); + console.assert(obj[objKey] === "object", "obj assert"); + console.assert(arr[arrIndex] === "array", "arr assert"); } evaluate(swapValues2); diff --git a/week-2-project/tests/add-entry.js b/week-2-project/tests/add-entry.js index 47914e6..a0987f7 100644 --- a/week-2-project/tests/add-entry.js +++ b/week-2-project/tests/add-entry.js @@ -30,8 +30,8 @@ describe(`addEntry: should add a new key/value pair to this.entries`, () => { ['firstKey', 'secondKey', 'thirdKey', 'fourthKey'].forEach(arg => { it(`${arg}`, () => { const result = object.addEntry(arg, ''); - assert.ok(result[arg] instanceof Error); - assert.strictEqual(result[arg].message, `addEntry: key "${arg}" already exists`); + assert.ok(result instanceof Error); + assert.strictEqual(result.message, `addEntry: key "${arg}" already exists`); }); }); }); diff --git a/week-2-project/tests/find-by-key.js b/week-2-project/tests/find-by-key.js index 183cafe..1916184 100644 --- a/week-2-project/tests/find-by-key.js +++ b/week-2-project/tests/find-by-key.js @@ -21,8 +21,8 @@ describe(`findByKey: returns the requested key/value pair, or an informative err ['a', 'b', 'c', 'd'].forEach(arg => { it(`${arg}`, () => { const result = object.findByKey(arg); - assert.ok(result[arg] instanceof ReferenceError); - assert.strictEqual(result[arg].message, `findByKey: no property "${arg}" in this.entries`); + assert.ok(result instanceof ReferenceError); + assert.strictEqual(result.message, `findByKey: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-2-project/tests/find-by-value.js b/week-2-project/tests/find-by-value.js index 5042fcc..0ae4f3a 100644 --- a/week-2-project/tests/find-by-value.js +++ b/week-2-project/tests/find-by-value.js @@ -31,7 +31,7 @@ describe(`findByValue: returns the requested key/value pair, or an informative e }); }); }); - describe(`otherwise returns an object containing the requested key/value pair`, () => { + describe(`otherwise returns an object containing the requested key/value pairs`, () => { [ ['firstValue', { firstKey: 'firstValue' }], ['secondValue', { secondKey: 'secondValue' }], @@ -44,11 +44,19 @@ describe(`findByValue: returns the requested key/value pair, or an informative e }); it(`it finds all keys containing "fourthValue"`, () => { const result = object.findByValue('fourthValue'); - assert.deepStrictEqual(Object.keys(result), ['fourthKey', 'fifthKey', 'sixthKey']); + assert.deepStrictEqual(result, { + fourthKey: 'fourthValue', + fifthKey: 'fourthValue', + sixthKey: 'fourthValue' + }); }); it(`and all keys containing "fifthValue"`, () => { const result = object.findByValue('fifthValue'); - assert.deepStrictEqual(Object.keys(result), ['seventhKey', 'eighthKey', 'ninthKey']); + assert.deepStrictEqual(result, { + seventhKey: 'fifthValue', + eighthKey: 'fifthValue', + ninthKey: 'fifthValue' + }); }); }); }); diff --git a/week-2-project/tests/remove-entry.js b/week-2-project/tests/remove-entry.js index 17e22c5..3b02792 100644 --- a/week-2-project/tests/remove-entry.js +++ b/week-2-project/tests/remove-entry.js @@ -21,8 +21,8 @@ describe(`removeEntry: should remove a key/value pair from this.entries`, () => ['tomato', 'potato', 'patate', 'pomme de terre'].forEach(arg => { it(`${arg}`, () => { const result = object.removeEntry(arg); - assert.ok(result[arg] instanceof Error); - assert.strictEqual(result[arg].message, `removeEntry: no property "${arg}" in this.entries`); + assert.ok(result instanceof Error); + assert.strictEqual(result.message, `removeEntry: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-2-project/tests/update-entry.js b/week-2-project/tests/update-entry.js index fd9870f..f670fa0 100644 --- a/week-2-project/tests/update-entry.js +++ b/week-2-project/tests/update-entry.js @@ -30,8 +30,8 @@ describe(`updateEntry: should add a new key/value pair to this.entries`, () => { ['tomato', 'potato', 'patate', 'pomme'].forEach(arg => { it(`${arg}`, () => { const result = object.updateEntry(arg, ''); - assert.ok(result[arg] instanceof ReferenceError); - assert.strictEqual(result[arg].message, `updateEntry: no property "${arg}" in this.entries`); + assert.ok(result instanceof ReferenceError); + assert.strictEqual(result.message, `updateEntry: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-3-project/README.md b/week-3-project/README.md index 4591553..6850231 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,19 +1,61 @@ -practice problems -- -- -- -- - -app object -- -- -- -- -- - -user interface -- -- -- -- -- + +## Week 3 Project + + + +This project builds directly from last week's, so the tables below are pretty big but there actually less to build than last week! + + +--- + + + +### Core Object + + + +> this table is written like documentation + + + + +| __it should ...__ | __syntax__ | __parameters__ | __return value__ | __description__ | +| --- | --- | --- | --- | --- | +| _... set the the current key_| ```obj.currentEntry = key;``` | _key_: a string | ```true``` if the key is valid, otherwise throws an error | this setter will save the ```key``` as the currentKey if it is a string, and if ```this.entries``` has that key. otherwise it throws a helpful error | +| _... get the the current entry_| ```const entry = obj.currentEntry;``` | (none) | an object with one property: ```{key:value}```. key is the currentKey, and value is the value in ```this.entries``` or a helpful error | this getter allows you to always have access to an up-to-date value for ```this.currentKey```. If a user deletes that entry but doesn't reset the ```currentEntry```, no problem! | +| _... get an object with all the liked entries_| ```const likedEntries = obj.likedEntries``` | (none) | an Object with all liked entries | this getter will return an object with all the entries in ```this.entries``` matching the keys in ```this.likedKeys```. If an entry has been removed, the return value will have a helpful error | +| _... like entries_| ```obj.likeEntry(key)``` | _key_: a string | ```true``` if the key is valid, otherwise returns an error | it will push the key into ```this.likedKeys``` if it is valid, otherwise returns an error | +| _... unlike entries_| ```obj.unlikeEntry(key)``` | _key_: a string | ```true``` if the key is valid and already liked, otherwise returns an error | if the key is valid, and is in ```this.likedEntries``` it will be removed from the array. otherwise a helpful error is returned | +| _... determines if values are primitive or not_ | ```obj.isPrimitive(value)``` | _value_: any JS value | Boolean | It returns ```true``` if the argument is a primitive, otherwise it returns ```false```. | +| _... determines if an object has a given key_ | ```obj.hasKey(obj, key)``` | _obj_: an object
_key_: a string | Boolean | It returns ```true``` if the object has the given key, otherwise it returns ```false```. | +| _... determines if an object has a given value_ | ```obj.hasValue(obj, value)``` | _obj_: an object
_value_: any JS type | Boolean | It returns ```true``` if any key in the object stores this value, otherwise it returns ```false```. | +| _... adds key/value pairs to ```this.entries```_ | ```obj.addEntry(key, value)``` | _key_: a string
_value_: any primitive value | ```true``` or an error | It returns ```true``` if the key/value pair was successfully added, otherwise it returns helpful error describing what went wrong. | +| _... removes a key/value pair from ```this.entries```_ | ```obj.removeEntry(key)``` | _key_: a string | ```true``` or an error | It returns ```true``` if the key/value pair was successfully removed, otherwise it returns helpful error describing what went wrong. | +| _... updates a key/value pair in ```this.entries```_ | ```obj.updateEntry(key, value)``` | _key_: a string
_value_: any primitive type | ```true``` or an error | It returns ```true``` if the key/value pair was successfully updated, otherwise it returns helpful error describing what went wrong. | +| _... returns all key/value pairs in ```this.entries```_ | ```obj.readAll()``` | (no parameters) | an object | a new object with the same key/value pairs as ```this.entries``` | +| _... finds an entry by key_ | ```obj.findByKey(key)``` | _key_: a string | an object or an error | It returns an object with the given key, and it's value in ```this.entries```. Or a helpful error | +| _... finds an entry by value | ```obj.findByValue(value)``` | _value_: a primitive | an object or an error | It returns an object with all key/value pairs in ```this.entries``` containing the given value. Or a helpful error | + + + +--- + + + +### User Interface + + + +> this table is written as user stories. notice that users can't set or get the current entry! + + + +| __As a/n__ ... | __I can__ ... | __so that__ ... | +| --- | --- | --- | +| _... user_ | ... remember which entries were most interesting | ... I can study more effectively over long periods of time | +| _... enthusiastic JS student_ | ... know what this site does and how to use it | ... I can use it to organize my favorite primitive values | +| _... enthusiastic JS student_ | ... set input new keys and primitive values | ... I can practice always being aware of what types _and_ values my application stores | +| _... enthusiastic JS student_ | ... select a method to call with my inputs | ... modify the values stored in this app, and search for particular entries | +| _... enthusiastic JS student_ | ... see the values stored in ```this.entries``` rendered to the DOM | ... I can easily know what is stored in the app without console.logging | +| _... HYF coach_ | ... resize the browser window | ... I can test your responsive design | +| _... HYF coach_ | ... inspect the web page | ... I can see if you correctly used HTML5 semantic elements, CSS classes, and generally wrote clean code | diff --git a/week-3-project/app.js b/week-3-project/app.js index 4320ac7..b4fe84f 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -1,12 +1,10 @@ - - const object = { currentKey: '', set currentEntry(key) { - if (null) { // write this early return condition! + if (null) { // write the early return condition throw new TypeError('set currentEntry: key should be a string'); } - if (null) { // write this early return condition! (using this.hasKey) + if (null) { // write the early return condition throw new ReferenceError(`set currentEntry: no entry with key "${key}"`); } @@ -14,43 +12,35 @@ const object = { }, get currentEntry() { // write me! - // consider using this.findByKey & this.currentKey }, likedKeys: [], get likedEntries() { + // write me! - // consider using .map & this.findByKey, then .reduce & Object.assign - // this can be done in two steps: - // first, build an array of all the liked entries (this.findByKey) - // second, build a single object containing all of the liked entries }, likeEntry: function (key) { - if (null) { // write this early-return condition! + if (null) { // write the early return condition return new TypeError('likeEntry: key should be a string'); } - if (null) { // write this early-return condition! (using this.hasKey) + if (null) { // write the early return condition return new ReferenceError(`likeEntry: key "${key}" has been removed`); } - if (null) { // write this early-return condition! (using .every()) + if (null) { // write the early return condition return new Error(`likeEntry: key "${key}" is already liked`); } // write me! }, unlikeEntry: function (key) { - if (null) { // write this early-return condition! + if (null) { // write the early return condition return new TypeError('unlikeEntry: key should be a string'); } - if (null) { // write this early-return condition! (using .every()) + if (null) { // write the early return condition return new Error(`unlikeEntry: key "${key}" is not in this.likedKeys`); } // write me! - // consider using .filter }, - - // everything below here is the same as last week's project - // this week's project will build on top of last week's entries: {}, isPrimitive: function (value) { // write me! @@ -62,62 +52,22 @@ const object = { // write me! }, addEntry: function (key, value) { - if (null) { // write me! - return new TypeError('addEntry: key should be a string'); - } - if (null) { // write me! (using this.isPrimitive) - return new TypeError('addEntry: value should be a primitive'); - } - if (null) { // write me! (using this.hasKey) - return { [key]: new Error(`addEntry: key "${key}" already exists`) }; - } - // write me! }, removeEntry: function (key) { - if (null) { // write me! - return new TypeError('removeEntry: key should be a string'); - } - if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`removeEntry: no property "${key}" in this.entries`) }; - } - // write me! }, updateEntry: function (key, value) { - if (null) { // write me! - return new TypeError('updateEntry: key should be a string'); - } - if (null) { // write me! (using this.isPrimitive) - return new TypeError('updateEntry: value should be a primitive'); - } - if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`updateEntry: no property "${key}" in this.entries`) }; - } - // write me! }, readAll: function () { // write me! }, findByKey: function (key) { - if (null) { // write me! - return new TypeError('findByKey: key should be a string'); - } - if (null) { // write me! (using this.hasKey) - return { [key]: new ReferenceError(`findByKey: no property "${key}" in this.entries`) }; - } - // write me! }, findByValue: function (value) { - if (null) { // write me! (using this.isPrimitive) - return new TypeError('findByValue: value should be a primitive'); - } - if (null) { // write me! (using this.hasValue) - return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); - } - // write me! }, } + diff --git a/week-3-project/practice-problems/error-based-decisions.js b/week-3-project/practice-problems/error-based-decisions.js new file mode 100644 index 0000000..f0b2f66 --- /dev/null +++ b/week-3-project/practice-problems/error-based-decisions.js @@ -0,0 +1,124 @@ +{ + const pageTitle = 'error-based decisions'; + const header = document.createElement("h2"); + header.innerHTML = pageTitle; + document.body.appendChild(header); + console.groupCollapsed(pageTitle); +} + +try { + + + /* this function is meant to be confusing, no need to understand it! + the point of these exercises is to learn how to work with functions that return errors + you can (and often will!) have to work with functions that return errors + ... without understanding what they do! + */ + const mightReturnAnError = a => "object" == typeof a + ? a instanceof Object + ? (() => { try { return a(), !0 } catch (a) { return new Error('second error') } })() + : new Error('first error') + : +a === +a || new Error('third error'); + + + const exercise1Tests = [ + { name: 'first', args: [-5], expected: true }, + { name: 'second', args: [null], expected: false }, + { name: 'third', args: [undefined], expected: false }, + { name: 'fourth', args: [true], expected: true }, + { name: 'fifth', args: [['second error']], expected: false }, + { name: 'sixth', args: [{ a: 'x' }], expected: false }, + { name: 'seventh', args: [() => { }], expected: false }, + { name: 'eighth', args: ['13'], expected: true }, + { name: 'ninth', args: ['second error'], expected: false }, + ] + function exercise1(arg) { + const result = mightReturnAnError(arg); + + if (null) { // write this condition + // write me! + } else { + // write me! + } + + } + exercise1.display = true; + evaluate(exercise1, exercise1Tests); + + + const exercise2Tests = [ + { name: 'first', args: [4], expected: 4 }, + { name: 'second', args: [null], expected: 'first error' }, + { name: 'third', args: [undefined], expected: 'third error' }, + { name: 'fourth', args: [false], expected: false }, + { name: 'fifth', args: [[]], expected: 'second error' }, + { name: 'sixth', args: [{}], expected: 'second error' }, + { name: 'seventh', args: [function () { }], expected: 'third error' }, + { name: 'eighth', args: ['4'], expected: '4' }, + { name: 'ninth', args: ['e'], expected: 'third error' }, + ] + function exercise2(arg) { + const result = mightReturnAnError(arg); + + // write me! + + } + exercise2.display = true; + evaluate(exercise2, exercise2Tests); + + + + const exercise3Tests = [ + { name: 'first', args: [4], expected: { number: 4 } }, + { name: 'second', args: [null], expected: { object: 'first error' } }, + { name: 'third', args: [undefined], expected: { 'undefined': 'third error' } }, + { name: 'fourth', args: [false], expected: { 'boolean': false } }, + { name: 'fifth', args: [[]], expected: { object: 'second error' } }, + { name: 'sixth', args: [{}], expected: { object: 'second error' } }, + { name: 'seventh', args: [function () { }], expected: { 'function': 'third error' } }, + { name: 'eighth', args: ['4'], expected: { string: '4' } }, + { name: 'ninth', args: ['e'], expected: { string: 'third error' } }, + ] + function exercise3(arg) { + const result = mightReturnAnError(arg); + + // write me! + + } + exercise3.display = true; + evaluate(exercise3, exercise3Tests); + + + const exercise4Tests = [ + { name: 'first', args: [4], expected: [null, 4] }, + { name: 'second', args: [null], expected: ['first error'] }, + { name: 'third', args: [undefined], expected: ['third error'] }, + { name: 'fourth', args: [false], expected: [null, false] }, + { name: 'fifth', args: [[]], expected: ['second error'] }, + { name: 'sixth', args: [{}], expected: ['second error'] }, + { name: 'seventh', args: [function () { }], expected: ['third error'] }, + { name: 'eighth', args: ['4'], expected: [null, '4'] }, + { name: 'ninth', args: ['e'], expected: ['third error'] }, + ] + function exercise4(arg) { + const result = mightReturnAnError(arg); + + // write me! + + } + exercise4.display = true; + evaluate(exercise4, exercise4Tests); + + + +} catch (err) { + console.log(err); + document.body.appendChild( + evaluate.errorSearchComponent('.js file', err) + ); +} + +{ + console.groupEnd(); + document.body.appendChild(document.createElement('hr')); +} diff --git a/week-3-project/practice-problems/getters-and-setters.js b/week-3-project/practice-problems/getters-and-setters.js index 68671d0..c5f2609 100644 --- a/week-3-project/practice-problems/getters-and-setters.js +++ b/week-3-project/practice-problems/getters-and-setters.js @@ -15,7 +15,7 @@ try { - function example_refactorMethodToGetter() { + function getterRefactor1() { const obj1 = { name: 'obj1', @@ -24,27 +24,211 @@ try { } } - console.assert(obj1.getGreeting() === `hi, I'm obj1`, 'assert 1'); - obj1.name = 'brussels' - console.assert(obj1.getGreeting() === `hi, I'm brussels`, 'assert 2'); - const obj2 = { name: 'obj2', get greeting() { - return `hi, I'm ${this.name}`; + // write me! } } - console.assert(obj2.greeting === `hi, I'm obj2`, 'assert 3'); - obj2.name = 'belgium' - console.assert(obj2.greeting === `hi, I'm belgium`, 'assert 4'); + const obj1Greeting1 = obj1.getGreeting(); + console.assert(obj1Greeting1 === `hi, I'm obj1`, `obj1's greeting is correct (1)`); + + const obj2Greeting1 = null; // fix this line! + console.assert(obj2Greeting1 === `hi, I'm obj2`, `obj2's greeting is correct (1)`); + + obj1.name = "first"; + obj2.name = "second"; + + const obj1Greeting2 = obj1.getGreeting(); + console.assert(obj1Greeting2 === `hi, I'm first`, `obj1's greeting is correct (2)`); + + const obj2Greeting2 = null; // fix this line! + console.assert(obj2Greeting2 === `hi, I'm second`, `obj2's greeting is correct (2)`); + + } + getterRefactor1.display = true; + evaluate(getterRefactor1); + + + function getterRefactor2() { + + const obj1 = { + numbers: [12, 4, 9, 36, 7, 0, -2], + modulo: 3, + getZeroMods: function () { + return this.numbers.filter(x => x % this.modulo === 0); + } + } + + const obj2 = { + numbers: [12, 4, 9, 36, 7, 0, -2], + modulo: 3, + get zeroMods() { + // write me! + } + } + + const obj1mods3 = null; + console.assert(obj1mods3[0] === 12, 'assert 1'); + console.assert(obj1mods3[1] === 9, 'assert 2'); + console.assert(obj1mods3[2] === 36, 'assert 3'); + + const obj2mods3 = null; + console.assert(obj2mods3[0] === 12, 'assert 4'); + console.assert(obj2mods3[1] === 9, 'assert 5'); + console.assert(obj2mods3[2] === 36, 'assert 6'); + + + obj1.modulo = 6; + obj2.modulo = 6; + + const obj1mods3second = null; + console.assert(obj1mods3second[0] === 12, 'assert 7'); + console.assert(obj1mods3second[1] === 36, 'assert 8'); + + const obj2mods3second = null; + console.assert(obj2mods3second[0] === 12, 'assert 9'); + console.assert(obj2mods3second[1] === 36, 'assert 10'); + + } + getterRefactor2.display = true; + evaluate(getterRefactor2); + + + function getterRefactor3() { + + const obj1 = { + entries: { first: 'hi!', second: 'bye!' }, + currentKey: 'second', + getCurrentEntry: function () { + return this.entries[this.currentKey]; + } + } + + const obj2 = { + entries: { first: 'hi!', second: 'bye!' }, + currentKey: 'second', + get currentEntry() { + // write me! + } + } + + // replace the null's to pass the asserts: + + const obj1current1 = null; + console.assert(obj1current1 === 'bye!', 'assert 1'); + + const obj2current1 = null; + console.assert(obj2current1 === 'bye!', 'assert 2'); + + obj1.currentKey = null; + obj2.currentKey = null; + + const obj1current2 = null; + console.assert(obj1current2 === 'hi!', 'assert 3'); + + const obj2current2 = null; + console.assert(obj2current2 === 'hi!', 'assert 4'); } - evaluate(example_refactorMethodToGetter); + getterRefactor3.display = true; + evaluate(getterRefactor3); + + + + function setterRefactor1() { + + const obj1 = { + greeting: ``, + setGreetingName: function (newName) { + this.greeting = `hi, I'm ${newName}!`; + } + }; + + const obj2 = { + greeting: ``, + set greetingName(newName) { + // write me! + } + }; + + obj1.setGreetingName('obj1'); + console.assert(obj1.greeting === "hi, I'm obj1!", 'assert 1'); + + ; // write me! + console.assert(obj2.greeting === "hi, I'm obj2!", 'assert 2'); + + obj1.setGreetingName('hi'); + console.assert(obj1.greeting === "hi, I'm hi!", 'assert 3'); + + ; // write me! + console.assert(obj2.greeting === "hi, I'm bye!", 'assert 4'); + + } + setterRefactor1.display = true; + evaluate(setterRefactor1); + + + function setterRefactor2() { + + + + } + setterRefactor2.display = true; + evaluate(setterRefactor2); + + + function setterRefactor3() { + + const obj1 = { + entries: { first: 'hi!', second: 'bye!' }, + current: {}, + setCurrentEntry: function (key) { + if (this.entries.hasOwnProperty(key)) { + this.current = { [key]: this.entries[key] }; + } else { + this.current = { [key]: new Error(`no entry with key "${key}"`) } + } + } + } + + const obj2 = { + entries: { first: 'hi!', second: 'bye!' }, + current: {}, + // write me! + } + + obj1.setCurrentEntry('second'); + console.assert(obj1.current.second === "bye!", 'assert 1'); + + ; // write me! + console.assert(obj2.current.second === "bye!", 'assert 2'); + + + obj1.setCurrentEntry('first'); + console.assert(obj1.current.first === "hi!", 'assert 3'); + console.assert(obj1.current.hasOwnProperty('second') === false, 'assert 4'); + + ; // write me! + console.assert(obj2.current.first === "hi!", 'assert 5'); + console.assert(obj2.current.hasOwnProperty('second') === false, 'assert 6'); + + + obj1.setCurrentEntry('hi'); + console.assert(obj1.current.hi.message === 'no entry with key "hi"', 'assert 7'); + console.assert(obj1.current.hasOwnProperty('first') === false, 'assert 8'); + + ; // write me! + console.assert(obj2.current.hi.message === 'no entry with key "hi"', 'assert 9'); + console.assert(obj2.current.hasOwnProperty('first') === false, 'assert 10'); + + } + setterRefactor3.display = true; + evaluate(setterRefactor3); - // more coming soon } catch (err) { console.log(err); diff --git a/week-3-project/practice-problems/index.html b/week-3-project/practice-problems/index.html index 5e3f15a..5831ef6 100644 --- a/week-3-project/practice-problems/index.html +++ b/week-3-project/practice-problems/index.html @@ -24,6 +24,7 @@ +
diff --git a/week-3-project/tests/add-entry.js b/week-3-project/tests/add-entry.js index 47914e6..a0987f7 100644 --- a/week-3-project/tests/add-entry.js +++ b/week-3-project/tests/add-entry.js @@ -30,8 +30,8 @@ describe(`addEntry: should add a new key/value pair to this.entries`, () => { ['firstKey', 'secondKey', 'thirdKey', 'fourthKey'].forEach(arg => { it(`${arg}`, () => { const result = object.addEntry(arg, ''); - assert.ok(result[arg] instanceof Error); - assert.strictEqual(result[arg].message, `addEntry: key "${arg}" already exists`); + assert.ok(result instanceof Error); + assert.strictEqual(result.message, `addEntry: key "${arg}" already exists`); }); }); }); diff --git a/week-3-project/tests/find-by-key.js b/week-3-project/tests/find-by-key.js index b0600d4..beb14ec 100644 --- a/week-3-project/tests/find-by-key.js +++ b/week-3-project/tests/find-by-key.js @@ -21,8 +21,8 @@ describe(`findByKey: returns the requested key/value pair, or an informative err ['a', 'b', 'c', 'd'].forEach(arg => { it(`${arg}`, () => { const result = object.findByKey(arg); - assert.ok(result[arg] instanceof ReferenceError); - assert.strictEqual(result[arg].message, `findByKey: no property "${arg}" in this.entries`); + assert.ok(result instanceof ReferenceError); + assert.strictEqual(result.message, `findByKey: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-3-project/tests/get-current-entry.js b/week-3-project/tests/get-current-entry.js index 0c9d0ca..ca8f218 100644 --- a/week-3-project/tests/get-current-entry.js +++ b/week-3-project/tests/get-current-entry.js @@ -21,4 +21,18 @@ describe(`get currentEntry: get the value of this.currentKey if the argument is }); }); }); + describe(`if the current entry has been removed, return the key with it's error`, () => { + [ + 'firstKey', + 'secondKey', + 'thirdKey', + 'fourthKey', + ].forEach(key => { + it(`${key}`, () => { + delete object.entries[key]; + object.currentKey = key; + assert.deepStrictEqual(object.currentEntry[key].message, `findByKey: no property "${key}" in this.entries`); + }); + }); + }); }); diff --git a/week-3-project/tests/remove-entry.js b/week-3-project/tests/remove-entry.js index 17e22c5..3b02792 100644 --- a/week-3-project/tests/remove-entry.js +++ b/week-3-project/tests/remove-entry.js @@ -21,8 +21,8 @@ describe(`removeEntry: should remove a key/value pair from this.entries`, () => ['tomato', 'potato', 'patate', 'pomme de terre'].forEach(arg => { it(`${arg}`, () => { const result = object.removeEntry(arg); - assert.ok(result[arg] instanceof Error); - assert.strictEqual(result[arg].message, `removeEntry: no property "${arg}" in this.entries`); + assert.ok(result instanceof Error); + assert.strictEqual(result.message, `removeEntry: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-3-project/tests/update-entry.js b/week-3-project/tests/update-entry.js index fd9870f..f670fa0 100644 --- a/week-3-project/tests/update-entry.js +++ b/week-3-project/tests/update-entry.js @@ -30,8 +30,8 @@ describe(`updateEntry: should add a new key/value pair to this.entries`, () => { ['tomato', 'potato', 'patate', 'pomme'].forEach(arg => { it(`${arg}`, () => { const result = object.updateEntry(arg, ''); - assert.ok(result[arg] instanceof ReferenceError); - assert.strictEqual(result[arg].message, `updateEntry: no property "${arg}" in this.entries`); + assert.ok(result instanceof ReferenceError); + assert.strictEqual(result.message, `updateEntry: no property "${arg}" in this.entries`); }); }); }); diff --git a/week-3-project/user-interface/handlers/favoriting.js b/week-3-project/user-interface/handlers/favoriting.js new file mode 100644 index 0000000..cd52854 --- /dev/null +++ b/week-3-project/user-interface/handlers/favoriting.js @@ -0,0 +1,23 @@ +function addStringHandler() { + + // read and process user input + + /* write the code to read the string input from the user */ + const key = null; // <------------- + const prop = null; // <------------- + + // pass user input through core logic (this works! no need to change it) + const result = typeof obj[prop] === 'function' // <---------------- + ? obj[prop](key) + : obj[prop] + + // report result to user (this works, no need to change it!) + // <--------------- + + // logs for developer + console.log('\n--- addStringHandler ---'); + console.log('stringToAdd:', typeof stringToAdd, ',', stringToAdd); + console.log('isNumbery:', typeof isNumbery, ',', isNumbery); + +}; +// connect this to it's button with an event listener! <---------- diff --git a/week-3-project/user-interface/index.html b/week-3-project/user-interface/index.html index 0a3d9e1..1b6655b 100644 --- a/week-3-project/user-interface/index.html +++ b/week-3-project/user-interface/index.html @@ -14,54 +14,82 @@ -
-
- key
- -
-
- value
- - -
+
+

CRUDing entries

+
+
+ key
+ +
+
+ value
+ + +
+
+
+
+
+ Which method do you want to call? + + + +
+
+
+
+ +
+
+
+
-
-
-
- Which method do you want to call? - - - -
-
-
-
- + +
+ +
+

favoriting

+
+ +
+
+
+
+ What do you want to do with this key? + + + +
+
+
+
-
-


- +
+ From f09803b2846779bd61a69f6165a5312e91277524 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Wed, 6 Nov 2019 11:24:12 +0100 Subject: [PATCH 13/37] week2 practice problems object.js completed --- week-2-project/practice-problems/index.html | 7 +++-- week-2-project/practice-problems/objects.js | 35 +++++++++++---------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/week-2-project/practice-problems/index.html b/week-2-project/practice-problems/index.html index 9d592fc..33d2f32 100644 --- a/week-2-project/practice-problems/index.html +++ b/week-2-project/practice-problems/index.html @@ -22,8 +22,11 @@ - + + + +
@@ -31,4 +34,4 @@ - + \ No newline at end of file diff --git a/week-2-project/practice-problems/objects.js b/week-2-project/practice-problems/objects.js index db0580e..fa84314 100644 --- a/week-2-project/practice-problems/objects.js +++ b/week-2-project/practice-problems/objects.js @@ -129,7 +129,7 @@ try { console.assert(a1.x === 'hi!', 'a1.x should strictly equal "hi!"'); b1.x = 'bye!'; // write two lines to pass the assertions - b2.x = 'bye!'; + b2.x = b1.x; console.assert(b1.x === b2.x, 'b1.x should strictly equal b2.x'); console.assert(b1.x === 'bye!', 'b1.x should strictly equal "bye!"'); @@ -141,7 +141,7 @@ try { console.assert(a1[index] === 'roof!', 'a1[index] should strictly equal "roof!"'); b1[index]='floor!'; // write two lines to pass the assertions - b2[index]='floor!'; + b2[index]=b1[index]; console.assert(b1[index] === b2[index], 'b1[index] should strictly equal b2[index]'); console.assert(b1[index] === 'floor!', 'b1[index] should strictly equal "floor!"'); @@ -154,53 +154,56 @@ try { const value1 = 5; let reference1 = {}; - ; // write this line + let value2=value1; // write this line console.assert(value2 === value1, "value1 should strictly equal value2"); - ; // write this line + let reference2=reference1; // write this line console.assert(reference2 === reference1, "reference1 should strictly equal reference2"); value2 = value2 + 1; // write this line - console.assert(value1 !== null, "value1 should strictly equal ___"); + console.assert(value1 !== null, "value1 should strictly equal 5"); - ; // write this line + reference1.x = reference2.x = 'hi!'; // write this line console.assert(reference1.x === reference2.x, "references.x should be strictly equal"); console.assert(reference1.x === 'hi!', "references.x should strictly equal 'hi!'"); - ; // write this line + reference1=reference2; // write this line console.assert(reference1 === reference2, "references should be strictly equal"); // remove the object from memory - ; // write this line - ; // write this line + reference1=null; // write this line + reference2=null; // write this line } evaluate(passTheAssertions2); function passTheAssertions3() { - ; // write this line - ; // write this line + let obj1 ={}; + let obj2 ={}; + obj1.y = obj2.y = 'B'; // write this line + obj1.x = obj2.x = 'A'; // write this line console.assert(obj1 !== obj2, 'the variables should not be strictly equal'); console.assert(obj1.y === obj2.y, 'their first entries should be the same'); console.assert(obj1.y === 'B', 'obj1.y should be "B"'); - const index = 'x'; + let index = 'x'; console.assert(obj1[index] === obj2[index], 'obj1[index] should strictly equal obj2[index]'); console.assert(obj1[index] === 'A', 'obj1[index] should be "A"'); - ; // write this line - ; // write this line + obj2.z = obj1.z = 'y'; // write this line + obj1[obj2.z] === obj2[obj1.z]; // write this line console.assert(obj1[obj2.z] === 'B', 'obj2.z should be "B"s index in obj1'); console.assert(obj1[obj2.z] === obj2[obj1.z], 'some tricky nested thing should be true'); - ; // write this line + let obj3 = obj2; // write this line console.assert(obj1 !== obj2, 'obj1 should strictly equal obj2'); console.assert(obj3 !== obj1, 'obj3 should not strictly equal obj`'); console.assert(obj3 === obj2, 'obj3 should strictly equal obj2'); console.assert(obj3[index] === obj1.x, 'obj3[index] should strictly equal obj1.x'); - ; // write this line + index = 'z'; + obj3.z === obj2[index]; // write this line console.assert(obj3.z === obj2[index], 'obj3.z should strictly equal obj2[index]'); } evaluate(passTheAssertions3); From eee2861d5885386ba6821355b6d70fcc763aabc7 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Wed, 6 Nov 2019 11:26:47 +0100 Subject: [PATCH 14/37] Update README.md --- week-2-project/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week-2-project/README.md b/week-2-project/README.md index abad0a2..fa57be2 100644 --- a/week-2-project/README.md +++ b/week-2-project/README.md @@ -1,8 +1,10 @@ +## Week2 Practice Problems Link + ### Please follow the link below to open the practice problems of Week2 +Practice Problems Week2

## Week 2 Project - The weekly projects in JS 2 will be making an even stronger distinction between your core application and the user interface. To help you with this transition there will be two assignment tables, one for the core app object and another for the user interface. From 03cd3dbddb927a097f106c21e823bda13538c668 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 7 Nov 2019 17:00:29 +0100 Subject: [PATCH 15/37] update on app.js and using-objects.js files --- week-2-project/app.js | 39 +++++++---- .../practice-problems/using-objects.js | 67 ++++++++++++++----- 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index 1eec39e..937408a 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -14,37 +14,48 @@ const object = { entries: {}, isPrimitive: function (value) { - // write me! + if (Object(value) !== value) {return true}; + return false; }, hasKey: function (obj, key) { - // write me! - }, + let keyList = Object.keys(obj); + if(keyList.length > 0){ + if(keyList.includes(key)){return true} + else {return false} + } + }, hasValue: function (obj, value) { - // write me! + if(Object.values(obj).includes(value)) {return true}; + return false; }, addEntry: function (key, value) { - if (null) { // write me! + if (typeof key !== 'string') { // write me! return new TypeError('addEntry: key should be a string'); } - if (null) { // write me! (using this.isPrimitive) + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('addEntry: value should be a primitive'); } - if (null) { // write me! (using this.hasKey) + + if (this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new Error(`addEntry: key "${key}" already exists`); - } + } - // write me! - }, + else {this.entries[key] = value; + return true;} + }, removeEntry: function (key) { - if (null) { // write me! + if (typeof key !== 'string') { // write me! return new TypeError('removeEntry: key should be a string'); } - if (null) { // write me! (using this.hasKey) + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } - - // write me! + else { + delete this.entries[key]; + return true; + } }, + updateEntry: function (key, value) { if (null) { // write me! return new TypeError('updateEntry: key should be a string'); diff --git a/week-2-project/practice-problems/using-objects.js b/week-2-project/practice-problems/using-objects.js index 9f29459..410d21d 100644 --- a/week-2-project/practice-problems/using-objects.js +++ b/week-2-project/practice-problems/using-objects.js @@ -12,8 +12,8 @@ try { const obj = { a: 0, - getA: function () { }, - sumAB: function (b) { }, + getA: function () { return this.a}, + sumAB: function (b) { return this.a + b }, }; console.assert(obj.getA() === 0, '1: obj.getA() should return 0'); @@ -36,8 +36,12 @@ try { const obj = { word: '', - getWord: function () { }, - concat: function (secondHalf) { }, + getWord: function () { + if (typeof this.word === 'string'){ + return "the word is " + this.word + } else return this.word; + }, + concat: function (secondHalf) { return this.word.concat(secondHalf) }, }; console.assert(obj.getWord() === 'the word is ', `1: obj.getWord() should return 'the word is '`); @@ -59,8 +63,12 @@ try { const obj = { array: ['hi!'], - getArray: function () { }, - getCopy: function () { }, + copyArray:[], + + getArray: function () { return this.array }, + getCopy: function () { + this.copyArray = [...this.array]; + return this.copyArray }, }; const gottenArray1 = obj.getArray(); @@ -94,8 +102,8 @@ try { const obj = { string: '', - setString: function (str) { }, - remixString: function (mixer) { } + setString: function (str) { this.string = str}, + remixString: function (mixer) { this.string = this.string.split('').join(mixer) } } obj.setString('hoy'); @@ -129,8 +137,13 @@ try { number: 5, mod: 0, equals: 0, - setNumber: function (num) { }, - modulo: function (modder) { } + setNumber: function (num) { + this.number = num; + return this.number; }, + modulo: function (modder) { + this.mod=modder; + this.equals = this.number%modder; + return this.equals; } }; obj.modulo(2); @@ -162,8 +175,13 @@ try { const obj = { arr: [1, 0], - merge: function (toMerge) { }, - replace: function (newArr) { }, + merge: function (toMerge) { + this.arr = toMerge.concat(this.arr); + return this.arr; + }, + replace: function (newArr) { + return this.arr=newArr; + }, }; obj.merge([4]); @@ -196,10 +214,27 @@ try { const obj = { arr: [], - merge: function (toMerge) { }, - replaceAll: function (newEntry) { }, - getRemixed: function (mixer) { }, - getCopy: function () { } + merge: function (toMerge) { + this.arr.unshift(...toMerge) + // this.arr = this.arr.concat(toMerge); + // return this.arr; + }, + replaceAll: function (newEntry) { + this.arr = this.arr.map(x=>newEntry); + // for (let i=0 ; i < this.arr.length ; i++){ + // this.arr[i]=newEntry; + // } + // return this.arr; + }, + getRemixed: function (mixer) { + return [...this.arr].join(mixer); + // let mixedArr=this.arr.join(mixer); + // this.arr.unshift(mixedArr); + // return this.arr; + }, + getCopy: function () { + return [...this.arr]; + } }; obj.merge([1, 2]); From 1607bb6d2424c8f571f19a28f03120ad95dfbc47 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 7 Nov 2019 23:16:21 +0100 Subject: [PATCH 16/37] updated week-2 project files --- week-2-project/app.js | 48 +++++++----- week-2-project/tests/has-key.js | 73 ++++++++++++++++++- week-2-project/tests/has-value.js | 11 +++ .../user-interface/handlers/call-it.js | 4 +- week-2-project/user-interface/index.html | 2 +- 5 files changed, 116 insertions(+), 22 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index 937408a..e684de8 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -19,14 +19,19 @@ const object = { }, hasKey: function (obj, key) { let keyList = Object.keys(obj); - if(keyList.length > 0){ + if(keyList.length >= 0){ if(keyList.includes(key)){return true} else {return false} } }, hasValue: function (obj, value) { - if(Object.values(obj).includes(value)) {return true}; - return false; + let valueList = Object.values(obj); + if(valueList.length >= 0){ + if(valueList.includes(value)){return true} + else {return false} + // if(Object.values(obj).includes(value)) {return true}; + // return false; + } }, addEntry: function (key, value) { if (typeof key !== 'string') { // write me! @@ -50,46 +55,51 @@ const object = { if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } - else { - delete this.entries[key]; - return true; - } + delete this.entries[key]; + return true; + }, updateEntry: function (key, value) { - if (null) { // write me! + if (typeof key !== 'string') { // write me! return new TypeError('updateEntry: key should be a string'); } - if (null) { // write me! (using this.isPrimitive) + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('updateEntry: value should be a primitive'); } - if (null) { // write me! (using this.hasKey) + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); } + else {this.entries[key] = value; + return true;} // write me! }, readAll: function () { - // write me! + // return this.entries; + var clonedObj = {...this.entries}; + return clonedObj; }, findByKey: function (key) { - if (null) { // write me! + if (typeof key !== 'string') { // write me! return new TypeError('findByKey: key should be a string'); } - if (null) { // write me! (using this.hasKey) + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + console.log('haskey'); return new ReferenceError(`findByKey: no property "${key}" in this.entries`); } - - // write me! + var newObj = {}; + newObj[key] = this.entries[key]; + return newObj; + }, findByValue: function (value) { - if (null) { // write me! (using this.isPrimitive) + if (typeof key !== 'string') { // write me! (using this.isPrimitive) return new TypeError('findByValue: value should be a primitive'); } - if (null) { // write me! (using this.hasValue) + if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); } - - // write me! (this one is a bit trickier) + }, } diff --git a/week-2-project/tests/has-key.js b/week-2-project/tests/has-key.js index 7388df3..7b7a9ec 100644 --- a/week-2-project/tests/has-key.js +++ b/week-2-project/tests/has-key.js @@ -1,45 +1,116 @@ + + describe(`hasKey: determines if an object has a given key`, () => { + before(() => { + object.entries = {}; + [ + ['firstKey', 'firstValue'], + ['secondKey', 'secondValue'], + ['thirdKey', 'thirdValue'], + ['fourthKey', 'fourthValue'] + ].forEach(newEntry => object.entries[newEntry[0]] = newEntry[1]); + }); + describe(`it is a pure function that works with any arguments`, () => { + it('{a:3} has "a"', () => { + const result = object.hasKey({ a: 3 }, "a"); + assert.strictEqual(result, true); + }); + it('{a:3} does not have "b"', () => { + const result = object.hasKey({ a: 3 }, "b"); + assert.strictEqual(result, false); + }); + it('{x:4} has "x"', () => { + const result = object.hasKey({ x: 4 }, "x"); + assert.strictEqual(result, true); + }); + it('{x:4} does not have "y"', () => { + const result = object.hasKey({ x: 4 }, "y"); + assert.strictEqual(result, false); + }); + }); + describe(`it returns true for existant entries`, () => { + ['firstKey', 'secondKey', 'thirdKey', 'fourthKey'].forEach(arg => { + it(arg, () => { + const result = object.hasKey(object.entries, arg); + assert.strictEqual(result, true); + }); + }); + }); + describe(`and returns false for non-existant entries.`, () => { - ['entries', 'hasKey', 'toSource', 'valueOf', 'hasOwnProperty'].forEach(arg => { + + ['entries', 'hasKey', 'toSource', 'valueOf', 'hasOwnProperty', ''].forEach(arg => { + it(arg, () => { + const result = object.hasKey(object.entries, arg); + + console.log(result) + assert.strictEqual(result, false); + }); + }); + }); + + describe(`or when there are no entries!`, () => { + + before(() => { + + object.entries = {}; + + }); + + ['entries', 'hasKey', 'toSource', 'valueOf', 'hasOwnProperty', ''].forEach(arg => { + + it(arg, () => { + + const result = object.hasKey(object.entries, arg); + + assert.strictEqual(result, false); + + }); + + }); + + }); + }); + diff --git a/week-2-project/tests/has-value.js b/week-2-project/tests/has-value.js index 8e9c4fa..beb3f29 100644 --- a/week-2-project/tests/has-value.js +++ b/week-2-project/tests/has-value.js @@ -42,4 +42,15 @@ describe(`hasValue: determines if an object has a given value`, () => { }); }); }); + describe(`even when there are no entries!`, () => { + before(() => { + object.entries = {}; + }); + ['tomato', null, true, undefined, 4].forEach(arg => { + it(arg, () => { + const result = object.hasValue(object.entries, arg); + assert.strictEqual(result, false); + }); + }); + }); }); diff --git a/week-2-project/user-interface/handlers/call-it.js b/week-2-project/user-interface/handlers/call-it.js index 76cec49..93f88f5 100644 --- a/week-2-project/user-interface/handlers/call-it.js +++ b/week-2-project/user-interface/handlers/call-it.js @@ -1,4 +1,5 @@ function callItHandler() { + // read user input // notice this: the UI doesn't allow you to enter invalid types! @@ -54,8 +55,9 @@ function callItHandler() { console.log('methodName:', typeof methodName, ',', methodName); console.log('args:', typeof args, ',', args); console.log('result:', typeof result, ',', result); - } + + const callItButton = document.getElementById('call-it'); callItButton.addEventListener('click', callItHandler); // connect this to it's button with an event listener! <---------- diff --git a/week-2-project/user-interface/index.html b/week-2-project/user-interface/index.html index a27de33..6d08f7a 100644 --- a/week-2-project/user-interface/index.html +++ b/week-2-project/user-interface/index.html @@ -52,8 +52,8 @@

Entries

-
+
From aea62feb003921ae86cbdc431f9c141fed590af9 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Fri, 8 Nov 2019 13:54:36 +0100 Subject: [PATCH 17/37] updated app.js and index files --- week-2-project/app.js | 13 ++- week-2-project/user-interface/index.html | 128 +++++++++++------------ 2 files changed, 72 insertions(+), 69 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index e684de8..69f694e 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -13,6 +13,7 @@ const object = { entries: {}, + isPrimitive: function (value) { if (Object(value) !== value) {return true}; return false; @@ -88,11 +89,15 @@ const object = { console.log('haskey'); return new ReferenceError(`findByKey: no property "${key}" in this.entries`); } - var newObj = {}; + const newObj = {}; newObj[key] = this.entries[key]; return newObj; }, + copyEntries: function() { + let copied = this.entries; + return copied; + }, findByValue: function (value) { if (typeof key !== 'string') { // write me! (using this.isPrimitive) return new TypeError('findByValue: value should be a primitive'); @@ -100,6 +105,10 @@ const object = { if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); } - + let copiedEntries = this.copyEntries(); + let newKey = Object.keys(copiedEntries).find(keyOfValue => copiedEntries[keyOfValue] === value); + let requestedObj={}; + requestedObj[newKey]=value; + return requestedObj; }, } diff --git a/week-2-project/user-interface/index.html b/week-2-project/user-interface/index.html index 6d08f7a..ef15e80 100644 --- a/week-2-project/user-interface/index.html +++ b/week-2-project/user-interface/index.html @@ -1,69 +1,63 @@ - - - - week 2 project UI - - - - - - - - - - - - - -
-
- key
- -
-
- value
- - -
-
-
-
-
- Which method do you want to call? - - - -
-
-
-
-

Entries

-
-
- -
- -
-
- - - back to index - - - - + + + week 2 project UI + + + + + + + + + + + +
+
+ key
+ +
+
+ value
+ + +
+
+
+
+
+ Which method do you want to call? + + + +
+
+
+
+

Entries

+
+
+ +
+
+ + + back to index + + + From 9ab7e9df58232ad02893c91ca57c055731f8a86f Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Fri, 8 Nov 2019 16:00:35 +0100 Subject: [PATCH 18/37] app.js and project-prep files are updated --- week-2-project/app.js | 52 ++++++++++++------- .../practice-problems/project-prep.js | 51 ++++++++++++++---- 2 files changed, 75 insertions(+), 28 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index 69f694e..fc78f27 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -19,20 +19,23 @@ const object = { return false; }, hasKey: function (obj, key) { - let keyList = Object.keys(obj); - if(keyList.length >= 0){ - if(keyList.includes(key)){return true} - else {return false} - } + return (obj.hasOwnProperty(key)); + + // let keyList = Object.keys(obj); + // if(keyList.length >= 0){ + // if(keyList.includes(key)){return true} + // else {return false} + // } }, hasValue: function (obj, value) { - let valueList = Object.values(obj); - if(valueList.length >= 0){ - if(valueList.includes(value)){return true} - else {return false} + return (Object.values(obj).indexOf(value) > -1); + // let valueList = Object.values(obj); + // if(valueList.length >= 0){ + // if(valueList.includes(value)){return true} + // else {return false} // if(Object.values(obj).includes(value)) {return true}; // return false; - } + }, addEntry: function (key, value) { if (typeof key !== 'string') { // write me! @@ -50,6 +53,7 @@ const object = { return true;} }, removeEntry: function (key) { + if (typeof key !== 'string') { // write me! return new TypeError('removeEntry: key should be a string'); } @@ -58,7 +62,6 @@ const object = { } delete this.entries[key]; return true; - }, updateEntry: function (key, value) { @@ -95,7 +98,7 @@ const object = { }, copyEntries: function() { - let copied = this.entries; + let copied = {...this.entries}; return copied; }, findByValue: function (value) { @@ -103,12 +106,23 @@ const object = { return new TypeError('findByValue: value should be a primitive'); } if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) - return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); + return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); } - let copiedEntries = this.copyEntries(); - let newKey = Object.keys(copiedEntries).find(keyOfValue => copiedEntries[keyOfValue] === value); - let requestedObj={}; - requestedObj[newKey]=value; - return requestedObj; - }, + + let expectedObj = {}; + const copiedObj = {...this.entries}; + let keyArr = Object.keys(copiedObj).filter(element => copiedObj[element] === value); + if(keyArr.length===0){ + return {}; + }else if (keyArr.length===1){ + expectedObj[key]=value; + return expectedObj; + }else if (keyArr.length>1){ + for (let i=0 ; i < keyArr.length ; i++ ) { + expectedObj[keyArr[i]]=value; + } + return expectedObj; + // (remember to avoid side effects) + } +}, } diff --git a/week-2-project/practice-problems/project-prep.js b/week-2-project/practice-problems/project-prep.js index 936b72e..9638f92 100644 --- a/week-2-project/practice-problems/project-prep.js +++ b/week-2-project/practice-problems/project-prep.js @@ -21,7 +21,8 @@ try { { name: 'tenth', args: [new WeakMap()], expected: false }, ]; function isPrimitive(thing) { - // write me! + if (Object(thing) !== thing) {return true}; + return false; } isPrimitive.display = true; evaluate(isPrimitive, isPrimitiveTests); @@ -37,8 +38,16 @@ try { { name: 'eighth', args: [{ b: undefined }, 'b'], expected: true }, ]; function hasKey(obj, key) { - // write me! - } + return (obj.hasOwnProperty(key)); + + // if(Object.keys(obj).includes(key)) { + // const newObj = {}; + // newObj[key] = obj[key]; + // return newObj; + // } + }; + + hasKey.display = true; evaluate(hasKey, hasKeyTests); @@ -53,7 +62,7 @@ try { { name: 'seventh', args: [{ a: false, b: false }, true], expected: false }, ]; function hasValue(obj, value) { - // write me! + return (Object.values(obj).indexOf(value) > -1); // consider using Object.keys, .filter and obj.hasOwnProperty } hasValue.display = true; @@ -71,7 +80,9 @@ try { { name: 'fifth', args: [{}, 'b', 'hi!'], expected: { b: 'hi!' } }, ]; function modifyToObjectWithBrackets(obj, key, value) { - // write me! + let newObj={...obj}; + newObj[key] = value; + return newObj; // (remember to avoid side effects) } modifyToObjectWithBrackets.display = true; @@ -85,8 +96,12 @@ try { { name: 'fifth', args: [secondObj, 'b'], expected: { a: 0 } }, { name: 'fifth', args: [{ b: 'hi!' }, 'b'], expected: {} }, ]; + function deleteFromObject(obj, key) { - // write me! + + let newObj={...obj}; + delete newObj[key]; + return newObj; // (remember to avoid side effects) } deleteFromObject.display = true; @@ -102,7 +117,12 @@ try { { name: 'fifth', args: [{ b: 'hi!' }, 'b'], expected: { b: 'hi!' } }, ]; function findByKey(obj, key) { - // write me! + const newObj2 = {}; + if (!obj.hasOwnProperty(key)){ + return newObj2; + } + newObj2[key] = obj[key]; + return newObj2; // (remember to avoid side effects) } findByKey.display = true; @@ -118,12 +138,25 @@ try { { name: 'fifth', args: [{ b: 'hi!', c: 'hi!' }, 'hi!'], expected: { b: 'hi!', c: 'hi!' } }, ]; function findByValue(obj, value) { - // write me! + let objNew = {}; + const obje3 = {...obj}; + let key = Object.keys(obje3).filter(key => obje3[key] === value); + if(key.length===0){ + return {}; + }else if (key.length===1){ + objNew[key]=value; + return objNew; + }else if (key.length>1){ + for (let i=0 ; i < key.length; i++) { + objNew[key[i]]=value; + } + return objNew; // (remember to avoid side effects) } +} findByValue.display = true; evaluate(findByValue, findByValueTests) - + } catch (err) { console.log(err); document.body.appendChild( From faaa4db087e32e7dbfad439818df4babcbe0698a Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Fri, 8 Nov 2019 20:59:30 +0100 Subject: [PATCH 19/37] some comments added, experimental codes removed --- week-2-project/app.js | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index fc78f27..7349597 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -19,22 +19,27 @@ const object = { return false; }, hasKey: function (obj, key) { - return (obj.hasOwnProperty(key)); - + return (obj.hasOwnProperty(key)); // SINGLE LINE SOLUTION + + // AlTERNATIVE SOLUTION // // let keyList = Object.keys(obj); // if(keyList.length >= 0){ - // if(keyList.includes(key)){return true} + // if(keyList.includes(key)){return true} // else {return false} // } }, hasValue: function (obj, value) { - return (Object.values(obj).indexOf(value) > -1); + return (Object.values(obj).indexOf(value) > -1); // SINGLE LINE SOLUTION + + // AlTERNATIVE SOLUTION-1 // // let valueList = Object.values(obj); // if(valueList.length >= 0){ // if(valueList.includes(value)){return true} // else {return false} + + // AlTERNATIVE SOLUTION-2 // // if(Object.values(obj).includes(value)) {return true}; - // return false; + // return false; }, addEntry: function (key, value) { @@ -47,9 +52,7 @@ const object = { if (this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new Error(`addEntry: key "${key}" already exists`); - } - - else {this.entries[key] = value; + } else {this.entries[key] = value; return true;} }, removeEntry: function (key) { @@ -73,15 +76,11 @@ const object = { } if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); - } - - else {this.entries[key] = value; + } else {this.entries[key] = value; return true;} - // write me! }, readAll: function () { - // return this.entries; - var clonedObj = {...this.entries}; + let clonedObj = {...this.entries}; return clonedObj; }, findByKey: function (key) { @@ -97,15 +96,12 @@ const object = { return newObj; }, - copyEntries: function() { - let copied = {...this.entries}; - return copied; - }, - findByValue: function (value) { - if (typeof key !== 'string') { // write me! (using this.isPrimitive) + // THIS METHOD SHOULD PASS THE TEST CASES, BECAUSE IT PASSES THE TEST CASES IN PRACTICE PROBLEMS + findByValue: function (value) { + if (typeof key !== 'string') { // (using this.isPrimitive) return new TypeError('findByValue: value should be a primitive'); } - if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) + if (!this.hasValue(this.entries, value)) { // (using this.hasValue) return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); } @@ -122,7 +118,6 @@ const object = { expectedObj[keyArr[i]]=value; } return expectedObj; - // (remember to avoid side effects) - } -}, + } + }, } From ae702a179d1e9f59b4aa7378f74d4d199161c1b1 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Sat, 9 Nov 2019 01:23:52 +0100 Subject: [PATCH 20/37] final version of app.js, findByValue method fixed --- week-2-project/app.js | 62 ++++++++++++--------------- week-2-project/tests/find-by-value.js | 22 +++++----- week-2-project/tests/has-value.js | 2 +- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/week-2-project/app.js b/week-2-project/app.js index 7349597..07e7196 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -8,9 +8,6 @@ - and users can access & modify that data */ - - - const object = { entries: {}, @@ -29,18 +26,17 @@ const object = { // } }, hasValue: function (obj, value) { - return (Object.values(obj).indexOf(value) > -1); // SINGLE LINE SOLUTION - + if(Object.values(obj).includes(value)) {return true}; + return false; // AlTERNATIVE SOLUTION-1 // + // return (Object.values(obj).indexOf(value) > -1); + + // AlTERNATIVE SOLUTION-2 // // let valueList = Object.values(obj); // if(valueList.length >= 0){ // if(valueList.includes(value)){return true} // else {return false} - - // AlTERNATIVE SOLUTION-2 // - // if(Object.values(obj).includes(value)) {return true}; - // return false; - + }, addEntry: function (key, value) { if (typeof key !== 'string') { // write me! @@ -94,30 +90,28 @@ const object = { const newObj = {}; newObj[key] = this.entries[key]; return newObj; - }, - // THIS METHOD SHOULD PASS THE TEST CASES, BECAUSE IT PASSES THE TEST CASES IN PRACTICE PROBLEMS - findByValue: function (value) { - if (typeof key !== 'string') { // (using this.isPrimitive) - return new TypeError('findByValue: value should be a primitive'); - } - if (!this.hasValue(this.entries, value)) { // (using this.hasValue) - return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); + +copyEntries: function() { + let copied = {...this.entries}; + return copied; + }, + +findByValue: function (value) { + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) + return new TypeError('findByValue: value should be a primitive'); + } + if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) + return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); } - - let expectedObj = {}; - const copiedObj = {...this.entries}; - let keyArr = Object.keys(copiedObj).filter(element => copiedObj[element] === value); - if(keyArr.length===0){ - return {}; - }else if (keyArr.length===1){ - expectedObj[key]=value; - return expectedObj; - }else if (keyArr.length>1){ - for (let i=0 ; i < keyArr.length ; i++ ) { - expectedObj[keyArr[i]]=value; - } - return expectedObj; + let copiedEntries = this.copyEntries(); + let requestedObj={}; + let newKey = Object.keys(copiedEntries).filter(keyOfValue => copiedEntries[keyOfValue] === value); + for (let i = 0; i < newKey.length; i++) { + if (this.entries[newKey[i]] === value) { + requestedObj[newKey[i]] = value; } - }, -} + } + return requestedObj; +}, +}; diff --git a/week-2-project/tests/find-by-value.js b/week-2-project/tests/find-by-value.js index 0ae4f3a..6f346c8 100644 --- a/week-2-project/tests/find-by-value.js +++ b/week-2-project/tests/find-by-value.js @@ -31,32 +31,32 @@ describe(`findByValue: returns the requested key/value pair, or an informative e }); }); }); - describe(`otherwise returns an object containing the requested key/value pairs`, () => { + describe(`otherwise returns an object containing the requested key/value pair`, () => { [ ['firstValue', { firstKey: 'firstValue' }], ['secondValue', { secondKey: 'secondValue' }], ['thirdValue', { thirdKey: 'thirdValue' }], ].forEach(arg => { - it(`it finds the correct key for ${arg[0]}`, () => { + it(`it returns the correct entry for value : ${arg[0]}`, () => { const result = object.findByValue(arg[0]); assert.deepStrictEqual(result, arg[1]); }); }); - it(`it finds all keys containing "fourthValue"`, () => { + it(`it finds all keys containing "fourthValue ... "`, () => { const result = object.findByValue('fourthValue'); assert.deepStrictEqual(result, { - fourthKey: 'fourthValue', - fifthKey: 'fourthValue', - sixthKey: 'fourthValue' + 'fourthKey': 'fourthValue', + 'fifthKey': 'fourthValue', + 'sixthKey': 'fourthValue' }); }); - it(`and all keys containing "fifthValue"`, () => { + it(`... and all keys containing "fifthValue"`, () => { const result = object.findByValue('fifthValue'); assert.deepStrictEqual(result, { - seventhKey: 'fifthValue', - eighthKey: 'fifthValue', - ninthKey: 'fifthValue' + 'seventhKey': 'fifthValue', + 'eighthKey': 'fifthValue', + 'ninthKey': 'fifthValue' }); }); }); -}); +}); \ No newline at end of file diff --git a/week-2-project/tests/has-value.js b/week-2-project/tests/has-value.js index beb3f29..28f006e 100644 --- a/week-2-project/tests/has-value.js +++ b/week-2-project/tests/has-value.js @@ -53,4 +53,4 @@ describe(`hasValue: determines if an object has a given value`, () => { }); }); }); -}); +}); \ No newline at end of file From f0eb94c4c9bb725592c6cf6bd717a513e09f10a4 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Sun, 10 Nov 2019 13:11:06 +0100 Subject: [PATCH 21/37] files changed --- week-2-project/README.md | 7 ------ week-2-project/app.js | 28 --------------------- week-2-project/practice-problems/objects.js | 12 +++++---- 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/week-2-project/README.md b/week-2-project/README.md index 2b356d2..fa57be2 100644 --- a/week-2-project/README.md +++ b/week-2-project/README.md @@ -1,17 +1,10 @@ -<<<<<<< HEAD ## Week2 Practice Problems Link ### Please follow the link below to open the practice problems of Week2 Practice Problems Week2

-======= ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 ## Week 2 Project -<<<<<<< HEAD -======= - ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 The weekly projects in JS 2 will be making an even stronger distinction between your core application and the user interface. To help you with this transition there will be two assignment tables, one for the core app object and another for the user interface. diff --git a/week-2-project/app.js b/week-2-project/app.js index 86ad7b2..e7659c2 100644 --- a/week-2-project/app.js +++ b/week-2-project/app.js @@ -48,12 +48,6 @@ const object = { if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('addEntry: value should be a primitive'); } -<<<<<<< HEAD -======= - if (null) { // write me! (using this.hasKey) - return new Error(`addEntry: key "${key}" already exists`); - } ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 if (this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new Error(`addEntry: key "${key}" already exists`); @@ -65,21 +59,11 @@ const object = { if (typeof key !== 'string') { // write me! return new TypeError('removeEntry: key should be a string'); } -<<<<<<< HEAD if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } delete this.entries[key]; return true; -======= - if (null) { // write me! (using this.hasKey) - return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); - } - - delete this.entries[key] - return true - // write me! ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 }, updateEntry: function (key, value) { @@ -89,18 +73,10 @@ const object = { if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('updateEntry: value should be a primitive'); } -<<<<<<< HEAD if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); } else {this.entries[key] = value; return true;} -======= - if (null) { // write me! (using this.hasKey) - return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); - } - - // write me! ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 }, readAll: function () { let clonedObj = {...this.entries}; @@ -110,12 +86,8 @@ const object = { if (typeof key !== 'string') { // write me! return new TypeError('findByKey: key should be a string'); } -<<<<<<< HEAD if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) console.log('haskey'); -======= - if (null) { // write me! (using this.hasKey) ->>>>>>> e8dd2760382752acce1b0a0a6fccff6087b18294 return new ReferenceError(`findByKey: no property "${key}" in this.entries`); } const newObj = {}; diff --git a/week-2-project/practice-problems/objects.js b/week-2-project/practice-problems/objects.js index 76c51a5..87c331a 100644 --- a/week-2-project/practice-problems/objects.js +++ b/week-2-project/practice-problems/objects.js @@ -135,8 +135,11 @@ try { console.assert(b1.x === 'bye!', 'b1.x should strictly equal "bye!"'); // -- - const index = 'y'; - + const index = 'y'; if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); + } + delete this.entries[key]; + return true; a1[index] = 'roof!'; // write one line to pass the assertions console.assert(a1[index] === a2[index], 'a1[index] should strictly equal a2[index]'); console.assert(a1[index] === 'roof!', 'a1[index] should strictly equal "roof!"'); @@ -219,8 +222,7 @@ try { const objValues = Object.values(obj); } - evaluate(footnote_objectDotKeysAndValues); - + evaluate(footnote_objectDotKeysAndValue, err); } catch (err) { console.log(err); document.body.appendChild( @@ -230,4 +232,4 @@ try { { console.groupEnd(); document.body.appendChild(document.createElement('hr')); -} +} \ No newline at end of file From 8f1ae7dddb1870d8606d239bf3a00346762b0e98 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Sun, 10 Nov 2019 22:53:53 +0100 Subject: [PATCH 22/37] week3 practice problems completed --- .../practice-problems/arrays-vs-objects.js | 21 +++++--- .../error-based-decisions.js | 43 +++++++++++---- .../practice-problems/getters-and-setters.js | 53 ++++++++++--------- 3 files changed, 73 insertions(+), 44 deletions(-) diff --git a/week-3-project/practice-problems/arrays-vs-objects.js b/week-3-project/practice-problems/arrays-vs-objects.js index 4a7e82c..7e54d73 100644 --- a/week-3-project/practice-problems/arrays-vs-objects.js +++ b/week-3-project/practice-problems/arrays-vs-objects.js @@ -14,8 +14,10 @@ try { function swapValues1() { const obj = { prop: "array" }; const arr = ["object"]; - let _ = null; - + let temp = obj.prop; + obj.prop = arr[0]; + arr[0]=temp; + // swap the values stored in each structure @@ -27,16 +29,17 @@ try { function swapValues2() { const obj = { prop: "array" }; const arr = ["object"]; - let _ = null; - - // swap the values stored in each structure using brackets and these variables + // swap the values stored in each structure using brackets and these variables const objKey = 'prop'; const arrIndex = 0; + let temp = obj[objKey]; + obj[objKey] = arr[arrIndex]; + arr[arrIndex]=temp; // asserts - console.assert(obj[obj_key] === "object", "obj assert"); - console.assert(arr[arr_index] === "array", "arr assert"); + console.assert(obj[objKey] === "object", "obj assert"); + console.assert(arr[arrIndex] === "array", "arr assert"); } evaluate(swapValues2); @@ -51,10 +54,12 @@ try { console.assert(evaluate.compareValues(obj1, obj3), 'obj: same keys/values, same order'); const arr1 = [1, 2, 3]; - const arr2 = [3, 2, 1]; + const arr2 = [1, 2, 3]; const arr3 = [1, 2, 3]; // do you remember why '===' won't work here? + console.assert(evaluate.compareValues(arr1, arr2), 'arr: same values, different order'); + // debugger; console.assert(evaluate.compareValues(arr1, arr3), 'arr: same values, same order'); diff --git a/week-3-project/practice-problems/error-based-decisions.js b/week-3-project/practice-problems/error-based-decisions.js index f0b2f66..b4b5c1b 100644 --- a/week-3-project/practice-problems/error-based-decisions.js +++ b/week-3-project/practice-problems/error-based-decisions.js @@ -35,10 +35,12 @@ try { function exercise1(arg) { const result = mightReturnAnError(arg); - if (null) { // write this condition - // write me! + // debugger; + + if (result instanceof Error) { // write this condition + return false;// write me! } else { - // write me! + return true;// write me! } } @@ -59,10 +61,15 @@ try { ] function exercise2(arg) { const result = mightReturnAnError(arg); + + if (result instanceof Error) { // write this condition + return result.message;// write me! + } else { + return arg;// write me! + } - // write me! - - } + + } exercise2.display = true; evaluate(exercise2, exercise2Tests); @@ -81,10 +88,16 @@ try { ] function exercise3(arg) { const result = mightReturnAnError(arg); - - // write me! - - } + if (result instanceof Error) { // write this condition + let output = new Object(); + output[typeof arg]=result.message; + return output; + } else if (typeof arg === 'number' || typeof arg === 'boolean' || typeof arg === 'string') { + let output = new Object(); + output[typeof arg]=arg; + return output; + } +} exercise3.display = true; evaluate(exercise3, exercise3Tests); @@ -103,7 +116,15 @@ try { function exercise4(arg) { const result = mightReturnAnError(arg); - // write me! + if (result instanceof Error) { // write this condition + let output = []; + output[0]=result.message; + return output; + } else if (typeof arg === 'number' || typeof arg === 'boolean' || typeof arg === 'string') { + let output = []; + output=[null,arg]; + return output; + } } exercise4.display = true; diff --git a/week-3-project/practice-problems/getters-and-setters.js b/week-3-project/practice-problems/getters-and-setters.js index c5f2609..cc08c66 100644 --- a/week-3-project/practice-problems/getters-and-setters.js +++ b/week-3-project/practice-problems/getters-and-setters.js @@ -27,14 +27,14 @@ try { const obj2 = { name: 'obj2', get greeting() { - // write me! + return `hi, I'm ${this.name}` ; } } const obj1Greeting1 = obj1.getGreeting(); console.assert(obj1Greeting1 === `hi, I'm obj1`, `obj1's greeting is correct (1)`); - const obj2Greeting1 = null; // fix this line! + const obj2Greeting1 = obj2.greeting; // fix this line! console.assert(obj2Greeting1 === `hi, I'm obj2`, `obj2's greeting is correct (1)`); obj1.name = "first"; @@ -43,7 +43,7 @@ try { const obj1Greeting2 = obj1.getGreeting(); console.assert(obj1Greeting2 === `hi, I'm first`, `obj1's greeting is correct (2)`); - const obj2Greeting2 = null; // fix this line! + const obj2Greeting2 = obj2.greeting; // fix this line! console.assert(obj2Greeting2 === `hi, I'm second`, `obj2's greeting is correct (2)`); } @@ -65,16 +65,16 @@ try { numbers: [12, 4, 9, 36, 7, 0, -2], modulo: 3, get zeroMods() { - // write me! + return this.numbers.filter(x => x % this.modulo === 0);// write me! } } - const obj1mods3 = null; + const obj1mods3 = obj1.getZeroMods(); console.assert(obj1mods3[0] === 12, 'assert 1'); console.assert(obj1mods3[1] === 9, 'assert 2'); console.assert(obj1mods3[2] === 36, 'assert 3'); - const obj2mods3 = null; + const obj2mods3 = obj2.zeroMods; console.assert(obj2mods3[0] === 12, 'assert 4'); console.assert(obj2mods3[1] === 9, 'assert 5'); console.assert(obj2mods3[2] === 36, 'assert 6'); @@ -83,11 +83,11 @@ try { obj1.modulo = 6; obj2.modulo = 6; - const obj1mods3second = null; + const obj1mods3second = obj1.getZeroMods(); console.assert(obj1mods3second[0] === 12, 'assert 7'); console.assert(obj1mods3second[1] === 36, 'assert 8'); - const obj2mods3second = null; + const obj2mods3second = obj2.zeroMods; console.assert(obj2mods3second[0] === 12, 'assert 9'); console.assert(obj2mods3second[1] === 36, 'assert 10'); @@ -110,25 +110,25 @@ try { entries: { first: 'hi!', second: 'bye!' }, currentKey: 'second', get currentEntry() { - // write me! + return this.entries[this.currentKey];// write me! } } // replace the null's to pass the asserts: - const obj1current1 = null; + const obj1current1 = obj1.getCurrentEntry(); console.assert(obj1current1 === 'bye!', 'assert 1'); - const obj2current1 = null; + const obj2current1 = obj2.currentEntry; console.assert(obj2current1 === 'bye!', 'assert 2'); - obj1.currentKey = null; - obj2.currentKey = null; + obj1.currentKey = 'first'; + obj2.currentKey = 'first'; - const obj1current2 = null; + const obj1current2 = obj1.getCurrentEntry();; console.assert(obj1current2 === 'hi!', 'assert 3'); - const obj2current2 = null; + const obj2current2 = obj2.currentEntry; console.assert(obj2current2 === 'hi!', 'assert 4'); } @@ -149,20 +149,20 @@ try { const obj2 = { greeting: ``, set greetingName(newName) { - // write me! + this.greeting = `hi, I'm ${newName}!`;// write me! } }; obj1.setGreetingName('obj1'); console.assert(obj1.greeting === "hi, I'm obj1!", 'assert 1'); - ; // write me! + obj2.greetingName='obj2'; // write me! console.assert(obj2.greeting === "hi, I'm obj2!", 'assert 2'); obj1.setGreetingName('hi'); console.assert(obj1.greeting === "hi, I'm hi!", 'assert 3'); - ; // write me! + obj2.greetingName='bye'; // write me! console.assert(obj2.greeting === "hi, I'm bye!", 'assert 4'); } @@ -173,8 +173,6 @@ try { function setterRefactor2() { - - } setterRefactor2.display = true; evaluate(setterRefactor2); @@ -197,13 +195,19 @@ try { const obj2 = { entries: { first: 'hi!', second: 'bye!' }, current: {}, - // write me! + set currentEntry(key){ + if (this.entries.hasOwnProperty(key)) { + this.current = { [key]: this.entries[key] }; + } else { + this.current = { [key]: new Error(`no entry with key "${key}"`) } + } + }// write me! } obj1.setCurrentEntry('second'); console.assert(obj1.current.second === "bye!", 'assert 1'); - ; // write me! + obj2.currentEntry='second'; // write me! console.assert(obj2.current.second === "bye!", 'assert 2'); @@ -211,16 +215,15 @@ try { console.assert(obj1.current.first === "hi!", 'assert 3'); console.assert(obj1.current.hasOwnProperty('second') === false, 'assert 4'); - ; // write me! + obj2.currentEntry='first'; // write me! console.assert(obj2.current.first === "hi!", 'assert 5'); console.assert(obj2.current.hasOwnProperty('second') === false, 'assert 6'); - obj1.setCurrentEntry('hi'); console.assert(obj1.current.hi.message === 'no entry with key "hi"', 'assert 7'); console.assert(obj1.current.hasOwnProperty('first') === false, 'assert 8'); - ; // write me! + obj2.currentEntry='hi'; // write me! console.assert(obj2.current.hi.message === 'no entry with key "hi"', 'assert 9'); console.assert(obj2.current.hasOwnProperty('first') === false, 'assert 10'); From 04b7cde28af2f5dee968bac7bfd93c4d6d2693e8 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Sun, 10 Nov 2019 22:56:31 +0100 Subject: [PATCH 23/37] Update README.md --- week-3-project/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week-3-project/README.md b/week-3-project/README.md index 6850231..8e37e04 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,3 +1,6 @@ +## Week3 Practice Problems Link + ### Please follow the link below to open the practice problems of Week2 +Practice Problems Week2

## Week 3 Project From 5c999b2618cd06fa89f03932b623c094861c1ca9 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Sun, 10 Nov 2019 22:57:04 +0100 Subject: [PATCH 24/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index 8e37e04..b49935b 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week2 -Practice Problems Week2

+Practice Problems Week3

## Week 3 Project From c81ccb939f177df38f675c35ed342031a8ad306d Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Mon, 11 Nov 2019 07:45:16 +0100 Subject: [PATCH 25/37] updated practice exercises --- week-2-project/practice-problems/using-objects.js | 3 ++- week-3-project/practice-problems/arrays-vs-objects.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/week-2-project/practice-problems/using-objects.js b/week-2-project/practice-problems/using-objects.js index 410d21d..056c65c 100644 --- a/week-2-project/practice-problems/using-objects.js +++ b/week-2-project/practice-problems/using-objects.js @@ -220,7 +220,8 @@ try { // return this.arr; }, replaceAll: function (newEntry) { - this.arr = this.arr.map(x=>newEntry); + // this.arr = this.arr.map(x=>newEntry); + this.arr = this.arr.fill(newEntry); // for (let i=0 ; i < this.arr.length ; i++){ // this.arr[i]=newEntry; // } diff --git a/week-3-project/practice-problems/arrays-vs-objects.js b/week-3-project/practice-problems/arrays-vs-objects.js index 7e54d73..d7746de 100644 --- a/week-3-project/practice-problems/arrays-vs-objects.js +++ b/week-3-project/practice-problems/arrays-vs-objects.js @@ -54,10 +54,11 @@ try { console.assert(evaluate.compareValues(obj1, obj3), 'obj: same keys/values, same order'); const arr1 = [1, 2, 3]; - const arr2 = [1, 2, 3]; + const arr2 = [3, 2, 1]; const arr3 = [1, 2, 3]; // do you remember why '===' won't work here? - + arr2[0]=1; + arr2[2]=3; console.assert(evaluate.compareValues(arr1, arr2), 'arr: same values, different order'); // debugger; console.assert(evaluate.compareValues(arr1, arr3), 'arr: same values, same order'); From 35757ef4ac523be695bbac869508f9d385b9a1f5 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Mon, 11 Nov 2019 11:24:20 +0100 Subject: [PATCH 26/37] updated the solution of the last 2 exercises --- week-3-project/practice-problems/error-based-decisions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week-3-project/practice-problems/error-based-decisions.js b/week-3-project/practice-problems/error-based-decisions.js index b4b5c1b..b334e9b 100644 --- a/week-3-project/practice-problems/error-based-decisions.js +++ b/week-3-project/practice-problems/error-based-decisions.js @@ -92,7 +92,7 @@ try { let output = new Object(); output[typeof arg]=result.message; return output; - } else if (typeof arg === 'number' || typeof arg === 'boolean' || typeof arg === 'string') { + } else { let output = new Object(); output[typeof arg]=arg; return output; @@ -120,7 +120,7 @@ try { let output = []; output[0]=result.message; return output; - } else if (typeof arg === 'number' || typeof arg === 'boolean' || typeof arg === 'string') { + } else { let output = []; output=[null,arg]; return output; From 322e58c0f6b76e2d18f81385d9fefb02f440c5b9 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:28:42 +0100 Subject: [PATCH 27/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index b49935b..2a137eb 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week2 -Practice Problems Week3

+Practice Problems Week3

## Week 3 Project From df15fc08d210acb8046f825d8322779380390b0e Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:29:02 +0100 Subject: [PATCH 28/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index 2a137eb..b49935b 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week2 -Practice Problems Week3

+Practice Problems Week3

## Week 3 Project From 2e26885fc3455c61e1a9ae592b70f7166a7739f8 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:29:50 +0100 Subject: [PATCH 29/37] Update README.md --- week-3-project/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index b49935b..ab96209 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link - ### Please follow the link below to open the practice problems of Week2 -Practice Problems Week3

+ ### Please follow the link below to open the practice problems of Week3 +Practice Problems Week3

## Week 3 Project From ad9a11d2e2ae640c84fb01059a579cbda7eecfb7 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:30:52 +0100 Subject: [PATCH 30/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index ab96209..7a8e07e 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week3 -Practice Problems Week3

+Practice Problems Week3

## Week 3 Project From b6668c9e9a698c0a6c3173995d7bd50722d02f5a Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:31:32 +0100 Subject: [PATCH 31/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index 7a8e07e..ff643c7 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week3 -Practice Problems Week3

+Practice Problems Week3

## Week 3 Project From f2fd391cb62ed3847f368eb578256622a4025285 Mon Sep 17 00:00:00 2001 From: Mert Demirok <55088530+Mert1980@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:32:46 +0100 Subject: [PATCH 32/37] Update README.md --- week-3-project/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3-project/README.md b/week-3-project/README.md index ff643c7..49714fa 100644 --- a/week-3-project/README.md +++ b/week-3-project/README.md @@ -1,6 +1,6 @@ ## Week3 Practice Problems Link ### Please follow the link below to open the practice problems of Week3 -Practice Problems Week3

+Practice Problems Week3

## Week 3 Project From 6197810975fb4d0d461b842f5602dd8b8f287836 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 14 Nov 2019 20:24:49 +0100 Subject: [PATCH 33/37] updated app.js --- week-3-project/app.js | 101 ++++++++++++++++++++++----- week-3-project/tests/has-key.js | 14 ++++ week-3-project/tests/remove-entry.js | 2 +- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/week-3-project/app.js b/week-3-project/app.js index b4fe84f..0beb258 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -1,22 +1,31 @@ const object = { currentKey: '', set currentEntry(key) { - if (null) { // write the early return condition + if (typeof key !== 'string') { // write the early return condition throw new TypeError('set currentEntry: key should be a string'); } - if (null) { // write the early return condition + if (!this.entries.hasOwnProperty(key)) { // write the early return condition throw new ReferenceError(`set currentEntry: no entry with key "${key}"`); } - + this.currentKey = key; // write me! }, get currentEntry() { - // write me! + // debugger; + return this.findByKey(this.currentKey); + if (this.entries.hasOwnProperty(this.currentKey)) { + return { [this.currentKey]: this.entries[this.currentKey] }; + } + + // return this.findByKey(this.currentKey); + // return { [this.currentKey]: this.entries[this.currentKey] }; }, + likedKeys: [], get likedEntries() { - - // write me! + + + return { [this.likedKeys]: this.entries[this.likedKeys] }; }, likeEntry: function (key) { if (null) { // write the early return condition @@ -43,31 +52,87 @@ const object = { }, entries: {}, isPrimitive: function (value) { - // write me! - }, - hasKey: function (obj, key) { - // write me! + if (Object(value) !== value) {return true}; + return false; }, + hasKey: function (obj, key){ + return (obj.hasOwnProperty(key));// write me! + }, hasValue: function (obj, value) { - // write me! + if(Object.values(obj).includes(value)) {return true}; + return false; }, addEntry: function (key, value) { - // write me! + if (typeof key !== 'string') { // write me! + return new TypeError('addEntry: key should be a string'); + } + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) + return new TypeError('addEntry: value should be a primitive'); + } + + if (this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + return new Error(`addEntry: key "${key}" already exists`); + } else {this.entries[key] = value; + return true;} }, removeEntry: function (key) { - // write me! - }, + if (typeof key !== 'string') { // write me! + return new TypeError('removeEntry: key should be a string'); + } + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); + } + delete this.entries[key]; + return true; + }, updateEntry: function (key, value) { - // write me! + if (typeof key !== 'string') { // write me! + return new TypeError('updateEntry: key should be a string'); + } + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) + return new TypeError('updateEntry: value should be a primitive'); + } + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + return new ReferenceError(`updateEntry: no property "${key}" in this.entries`); + } else {this.entries[key] = value; + return true;} }, readAll: function () { - // write me! + let clonedObj = {...this.entries}; + return clonedObj; }, findByKey: function (key) { - // write me! + if (typeof key !== 'string') { // write me! + return new TypeError('findByKey: key should be a string'); + } + if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + // console.log('haskey'); + return new ReferenceError(`findByKey: no property "${key}" in this.entries`); + } + const newObj = {}; + newObj[key] = this.entries[key]; + return newObj; }, + copyEntries: function() { + let copied = {...this.entries}; + return copied; + }, findByValue: function (value) { - // write me! + if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) + return new TypeError('findByValue: value should be a primitive'); + } + if (!this.hasValue(this.entries, value)) { // write me! (using this.hasValue) + return new ReferenceError(`findByValue: no entry with value (${typeof value}, ${value})`); + } + let copiedEntries = this.copyEntries(); + let requestedObj={}; + let newKey = Object.keys(copiedEntries).filter(keyOfValue => copiedEntries[keyOfValue] === value); + for (let i = 0; i < newKey.length; i++) { + if (this.entries[newKey[i]] === value) { + requestedObj[newKey[i]] = value; + } + } + return requestedObj; }, } diff --git a/week-3-project/tests/has-key.js b/week-3-project/tests/has-key.js index ff26be3..6fcbdf0 100644 --- a/week-3-project/tests/has-key.js +++ b/week-3-project/tests/has-key.js @@ -34,7 +34,19 @@ describe(`hasKey: determines if an object has a given key`, () => { }); }); }); + describe(`and returns false for non-existant entries.`, () => { + ['entries', 'hasKey', 'toSource', 'valueOf', 'hasOwnProperty', ''].forEach(arg => { + it(arg, () => { + const result = object.hasKey(object.entries, arg); + assert.strictEqual(result, false); + }); + }); + }); + describe(`or when there are no entries!`, () => { + before(() => { + object.entries = {}; + }); ['entries', 'hasKey', 'toSource', 'valueOf', 'hasOwnProperty', ''].forEach(arg => { it(arg, () => { const result = object.hasKey(object.entries, arg); @@ -43,6 +55,7 @@ describe(`hasKey: determines if an object has a given key`, () => { }); }); }); + describe(`or when there are no entries!`, () => { before(() => { object.entries = {}; @@ -55,3 +68,4 @@ describe(`hasKey: determines if an object has a given key`, () => { }); }); }); + diff --git a/week-3-project/tests/remove-entry.js b/week-3-project/tests/remove-entry.js index f3b310f..8a14f0c 100644 --- a/week-3-project/tests/remove-entry.js +++ b/week-3-project/tests/remove-entry.js @@ -38,7 +38,7 @@ describe(`removeEntry: should remove a key/value pair from this.entries`, () => describe(`... and actually removes the entries!`, () => { valuesToRemove.forEach(arg => { it(`object.hasKey(object.entries, ${arg}) === false`, () => { - assert.strictEqual(this.entries.hasOwnProperty(arg), false); + assert.strictEqual(object.entries.hasOwnProperty(arg), false); }); }); }); From fa0dc1739360149c5b11aab6e65c398179d237c5 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 14 Nov 2019 21:47:23 +0100 Subject: [PATCH 34/37] updated app.js file --- week-3-project/app.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/week-3-project/app.js b/week-3-project/app.js index 0beb258..7a78459 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -11,16 +11,13 @@ const object = { // write me! }, get currentEntry() { - // debugger; - return this.findByKey(this.currentKey); + debugger; if (this.entries.hasOwnProperty(this.currentKey)) { return { [this.currentKey]: this.entries[this.currentKey] }; - } - - // return this.findByKey(this.currentKey); - // return { [this.currentKey]: this.entries[this.currentKey] }; + } else {throw new ReferenceError(`set currentEntry: no entry with key "${this.currentKey}"`); + } }, - + likedKeys: [], get likedEntries() { From 81c9ce31fd3a484f12f56a2eeceb51ac170cddfd Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Thu, 14 Nov 2019 23:01:46 +0100 Subject: [PATCH 35/37] updated app.js --- week-3-project/app.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/week-3-project/app.js b/week-3-project/app.js index 7a78459..5ec5d7e 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -11,11 +11,9 @@ const object = { // write me! }, get currentEntry() { - debugger; - if (this.entries.hasOwnProperty(this.currentKey)) { - return { [this.currentKey]: this.entries[this.currentKey] }; - } else {throw new ReferenceError(`set currentEntry: no entry with key "${this.currentKey}"`); - } + // debugger; + if (this.removed === false) {return this.findByKey(this.currentKey)} + else return { [this.currentKey]: this.entries[this.currentKey] }; }, likedKeys: [], @@ -72,15 +70,17 @@ const object = { } else {this.entries[key] = value; return true;} }, + removed: true, removeEntry: function (key) { if (typeof key !== 'string') { // write me! return new TypeError('removeEntry: key should be a string'); } - if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) + else if (!this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } - delete this.entries[key]; - return true; + else {delete this.entries[key]; + this.removed = !this.removed; + return true;} }, updateEntry: function (key, value) { if (typeof key !== 'string') { // write me! From 0293b2ea6fa650fb738f90051106165ebd158149 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Fri, 15 Nov 2019 17:08:32 +0100 Subject: [PATCH 36/37] updated app.js --- week-3-project/app.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/week-3-project/app.js b/week-3-project/app.js index 5ec5d7e..6240e75 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -8,42 +8,49 @@ const object = { throw new ReferenceError(`set currentEntry: no entry with key "${key}"`); } this.currentKey = key; - // write me! }, get currentEntry() { // debugger; if (this.removed === false) {return this.findByKey(this.currentKey)} else return { [this.currentKey]: this.entries[this.currentKey] }; }, - likedKeys: [], get likedEntries() { - - - return { [this.likedKeys]: this.entries[this.likedKeys] }; + let likedObject = {}; + if (this.likeEntry()){ + for (let i=0 ; i < this.likedKeys.length ; i++){ + likedObject[this.likedKeys[i]] = this.entries[this.likedKeys[i]]; + } + return likedObject; + } else { + return this.likeEntry(); + } }, likeEntry: function (key) { - if (null) { // write the early return condition + // debugger; + if (typeof key !== 'string') { // write the early return condition return new TypeError('likeEntry: key should be a string'); } - if (null) { // write the early return condition + if (!this.entries.hasOwnProperty(key)) { // write the early return condition return new ReferenceError(`likeEntry: key "${key}" has been removed`); } - if (null) { // write the early return condition + if (this.likedKeys.includes(key)) { // write the early return condition return new Error(`likeEntry: key "${key}" is already liked`); } - - // write me! + this.likedKeys.push(key); + if (this.addEntry()){ + return true; + } }, unlikeEntry: function (key) { - if (null) { // write the early return condition + if (typeof key !== 'string') { // write the early return condition return new TypeError('unlikeEntry: key should be a string'); } - if (null) { // write the early return condition + if (!this.likedKeys.includes(key)) { // write the early return condition return new Error(`unlikeEntry: key "${key}" is not in this.likedKeys`); } - - // write me! + this.likedKeys.unshift(key); + return true; }, entries: {}, isPrimitive: function (value) { @@ -64,7 +71,6 @@ const object = { if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('addEntry: value should be a primitive'); } - if (this.hasKey(this.entries, key)) { // write me! (using this.hasKey) return new Error(`addEntry: key "${key}" already exists`); } else {this.entries[key] = value; From e3bfcacd500cfd65d2a5687af4730e8ccc452f85 Mon Sep 17 00:00:00 2001 From: Mert Demirok Date: Sat, 16 Nov 2019 12:09:51 +0100 Subject: [PATCH 37/37] final version of app.js --- week-3-project/app.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/week-3-project/app.js b/week-3-project/app.js index 6240e75..b87d0f0 100644 --- a/week-3-project/app.js +++ b/week-3-project/app.js @@ -10,21 +10,25 @@ const object = { this.currentKey = key; }, get currentEntry() { - // debugger; - if (this.removed === false) {return this.findByKey(this.currentKey)} - else return { [this.currentKey]: this.entries[this.currentKey] }; + const entry = this.findByKey(this.currentKey); + if (entry instanceof Error) { + return {[this.currentKey]:entry} + } else { + return entry; + } }, likedKeys: [], get likedEntries() { let likedObject = {}; - if (this.likeEntry()){ - for (let i=0 ; i < this.likedKeys.length ; i++){ + for (let i=0 ; i < this.likedKeys.length ; i++){ + let likedEntry = this.findByKey(this.likedKeys[i]); + if (likedEntry instanceof Error) { + likedObject[this.likedKeys[i]] = likedEntry + } else { likedObject[this.likedKeys[i]] = this.entries[this.likedKeys[i]]; } - return likedObject; - } else { - return this.likeEntry(); } + return likedObject; }, likeEntry: function (key) { // debugger; @@ -76,7 +80,6 @@ const object = { } else {this.entries[key] = value; return true;} }, - removed: true, removeEntry: function (key) { if (typeof key !== 'string') { // write me! return new TypeError('removeEntry: key should be a string'); @@ -85,7 +88,6 @@ const object = { return new ReferenceError(`removeEntry: no property "${key}" in this.entries`); } else {delete this.entries[key]; - this.removed = !this.removed; return true;} }, updateEntry: function (key, value) { @@ -120,7 +122,7 @@ const object = { let copied = {...this.entries}; return copied; }, - findByValue: function (value) { + findByValue: function(value) { if (!this.isPrimitive(value)) { // write me! (using this.isPrimitive) return new TypeError('findByValue: value should be a primitive'); }