Skip to content
98 changes: 94 additions & 4 deletions week-1-project/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,131 @@ const object = {
NaNyStrings: [],
isNumberyString: function (param) {
// write me!

if (typeof param !== 'string') {
return false;
}

return !isNaN(param);
},
addString: function (param) {
if (null) return false; // write this early return condition

// write me! (using this.isNumberyString)
if (typeof param !== "string"){
return false;
}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;
}
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 even(num){
return (num % 2 === 0);
}
return this.numberyStrings.filter(even);
}else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){
function even(num){
return (num % 2 === 0);
}
return this.numberyStrings.filter(even);
}
},
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 odd(num){
return (num%2 !== 0);
}
return this.numberyStrings.filter(odd);
}else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){
function odd(num){
return (num % 2 !== 0);
}
return this.numberyStrings.filter(odd);
}
},
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 negative(num){
return (num < 0);
}
return this.numberyStrings.filter(negative);
}else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){
function negative(num){
return (num < 0);
}
return this.numberyStrings.filter(negative);
}
},
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 positive(num){
return (num > 0);
}
return this.numberyStrings.filter(positive);
}else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){
function positive(num){
return (num > 0 || num === "");
}
return this.numberyStrings.filter(positive);
}
},
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 zero(num){
return (num == 0);
}
return this.numberyStrings.filter(zero);
}else if (this.NaNyStrings.length !== 0 && this.numberyStrings.length !== 0){
function zero(num){
return (num == 0);
}
return this.numberyStrings.filter(zero);
}
},
numberyAsNumbers: function () {
// write me!
return this.numberyStrings.map(num => Number(num));

},
NaNyAsNumbers: function () {
// write me!
return this.NaNyStrings.map( num => Number(num));
},
sumOfNumbery: function () {
// write me! (using a Array.prototype.reduce())
// write me!
const reducer = (accumulator, currentValue) => Number(accumulator) + Number(currentValue);
return this.numberyStrings.reduce(reducer, 0);
},
sumOfNaNy: function () {
// write me!
return NaN;
},
};

Expand Down
8 changes: 8 additions & 0 deletions week-1-project/practice-problems/avoiding-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ try {
];
function replaceItem(arr, index, newItem) {
// write me!
/*function repeatItems(items, numRepeats) {
let result = items.map(e => {
let arr1=[];
let filled = arr1.fill(e, 0, numRepeats)
return filled;
});
return result;*/

}
replaceItem.display = true;
evaluate(replaceItem, replaceItemTests);
Expand Down
84 changes: 62 additions & 22 deletions week-2-project/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,112 @@ const object = {
entries: {},
isPrimitive: function (value) {
// write me!
if (Object(value) !== value) {
return true
}
else {
return false
}
},
hasKey: function (obj, key) {
// write me!
return (obj.hasOwnProperty(key));
},
hasValue: function (obj, value) {
// write me!
if(Object.values(obj).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)
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
}
// write me!
},
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`);
}

else {
(this.entries[key] = value);
return true;
}
// write me!
},
readAll: function () {
// write me!
var readObj = {... this.entries};
return readObj;

},
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!

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)
if (!this.isPrimitive(value)) { // 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)

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;
// write me! (this one is a bit trickier)
},
}
34 changes: 18 additions & 16 deletions week-2-project/practice-problems/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ try {
// --
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] = 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!"');

Expand All @@ -155,33 +155,35 @@ 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';
obj1.x = obj2.x = 'A';
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"');
Expand All @@ -190,18 +192,18 @@ try {
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
obj[obl2.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
obj3.z = obj2.x; // write this line
console.assert(obj3.z === obj2[index], 'obj3.z should strictly equal obj2[index]');
}
evaluate(passTheAssertions3);
Expand Down
Loading