diff --git a/week-1-project/README.md b/week-1-project/README.md
index 623d867..ca008b2 100644
--- a/week-1-project/README.md
+++ b/week-1-project/README.md
@@ -1,3 +1,8 @@
+## Week1 Practice Exercises Link
+ ### Please follow the link below to open the practice exercises of Week1 project
+
+Practice Exercises Week1
+
## 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.
diff --git a/week-1-project/app.js b/week-1-project/app.js
index 5fdfbea..ed1bcfc 100644
--- a/week-1-project/app.js
+++ b/week-1-project/app.js
@@ -1,43 +1,119 @@
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;
+ if(this.numberyStrings.length===0) return this.NaNyStrings;
+ 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!
+
+ 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 () {
- // 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! (using a Array.prototype.reduce())
+ 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);
},
};
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/arrays.js b/week-1-project/practice-problems/arrays.js
index 9342ce9..9cf3e35 100644
--- a/week-1-project/practice-problems/arrays.js
+++ b/week-1-project/practice-problems/arrays.js
@@ -103,22 +103,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!"');
}
@@ -129,32 +130,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"');
@@ -163,18 +164,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);
diff --git a/week-1-project/practice-problems/avoiding-side-effects.js b/week-1-project/practice-problems/avoiding-side-effects.js
index 823001d..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);
@@ -206,7 +213,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);
@@ -225,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);
@@ -244,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);
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');
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;
diff --git a/week-1-project/practice-problems/functions-to-objects.js b/week-1-project/practice-problems/functions-to-objects.js
index 6677dac..3fb4b62 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);
+ }
}
@@ -166,7 +169,9 @@ try {
const obj = {
array: [3],
- mergeArrays: function (arrToMerge) { }
+ mergeArrays: function (arrToMerge) {
+ return this.array = [...this.array, ...arrToMerge];
+ }
}
obj.mergeArrays([2]);
@@ -192,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');
@@ -216,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');
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/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.
diff --git a/week-2-project/app.js b/week-2-project/app.js
index 90e883b..e7659c2 100644
--- a/week-2-project/app.js
+++ b/week-2-project/app.js
@@ -13,74 +13,108 @@
const object = {
entries: {},
+
isPrimitive: function (value) {
- // write me!
+ if (Object(value) !== value) {return true};
+ return false;
},
hasKey: function (obj, key) {
- // write me!
- },
+ return (obj.hasOwnProperty(key)); // SINGLE LINE SOLUTION
+
+ // AlTERNATIVE SOLUTION //
+ // 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;
+ // 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}
+
},
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)
- return new Error(`addEntry: key "${key}" already exists`);
- }
- // write me!
- },
+ 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) {
- 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`);
}
-
- delete this.entries[key]
- return true
- // write me!
+ 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`);
- }
-
- // write me!
+ } else {this.entries[key] = value;
+ return true;}
},
readAll: function () {
- // write me!
+ let 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`);
}
+ const newObj = {};
+ newObj[key] = this.entries[key];
+ return newObj;
+ },
- // write me!
+copyEntries: function() {
+ let copied = {...this.entries};
+ return copied;
},
- findByValue: function (value) {
- if (null) { // write me! (using this.isPrimitive)
- return new TypeError('findByValue: value should be a primitive');
+
+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})`);
}
- if (null) { // 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;
}
-
- // write me! (this one is a bit trickier)
- },
-}
+ }
+ return requestedObj;
+},
+};
diff --git a/week-2-project/practice-problems/index.html b/week-2-project/practice-problems/index.html
index 48a7c5f..33d2f32 100644
--- a/week-2-project/practice-problems/index.html
+++ b/week-2-project/practice-problems/index.html
@@ -34,4 +34,4 @@