Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Week3 homework #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions Week1/homework/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
'use strict';

{
const bookTitles = [
// Replace with your own book titles
'harry_potter_chamber_secrets',
];

// Replace with your own code
console.log(bookTitles);
}
1 change: 0 additions & 1 deletion Week1/homework/index.html
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
<!-- replace this with your HTML content -->
1 change: 0 additions & 1 deletion Week1/homework/style.css
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
/* add your styling here */
4 changes: 1 addition & 3 deletions Week3/homework/step2-1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

function foo(func) {
// What to do here?
// Replace this comment and the next line with your code
console.log(func);
func();
}

function bar() {
Expand Down
28 changes: 19 additions & 9 deletions Week3/homework/step2-2.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
'use strict';

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
const numbers = [];

// Replace this comment and the next line with your code
console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);
}

function sayThree(number) {
// Replace this comment and the next line with your code
console.log(number);
}

function sayFive(number) {
// Replace this comment and the next line with your code
console.log(number);
}

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
const numbers = [];
let numbersStart = startIndex;
const numbersEnd = stopIndex;
while (numbersStart <= numbersEnd) {
numbers.push(numbersStart);
numbersStart++;
}
for (let i = 0; i < numbers.length; i++) {
const number = numbers[i];
if (number % 3 === 0) {
threeCallback(number);
}
if (number % 5 === 0) {
fiveCallback(number);
}
}
}

threeFive(10, 15, sayThree, sayFive);

// Do not change or remove anything below this line
Expand Down
29 changes: 16 additions & 13 deletions Week3/homework/step2-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);

for (let i = 0; i < num; i++) {
result += str;
}
return result;
}

Expand All @@ -17,10 +16,11 @@ console.log('for', repeatStringNumTimesWithFor('abc', 3));
function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);

let counter = 0;
while (counter < num) {
result += str;
counter++;
}
return result;
}

Expand All @@ -30,14 +30,17 @@ console.log('while', repeatStringNumTimesWithWhile('abc', 3));
function repeatStringNumTimesWithDoWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);

let i = 0;
if (num > 0) {
do {
result += str;
i++;
} while (i < num);
}
return result;
}

console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 0));

// Do not change or remove anything below this line
module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion Week3/homework/step2-4.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

function Dog() {
// add your code here
this.name = 'Fido';
this.color = 'white and brown';
this.numLegs = 4;
}

const hound = new Dog();
Expand Down
7 changes: 5 additions & 2 deletions Week3/homework/step2-5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ function multiplyAll(arr) {
// eslint-disable-next-line
let product = 1;

// Replace this comment and the next line with your code
console.log(arr, product);
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
product = product * arr[i][j];
}
}

return product;
}
Expand Down
20 changes: 16 additions & 4 deletions Week3/homework/step2-6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ const arr2d = [[1, 2], [3, 4], [5, 6]];
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];

function flattenArray2d(arr) {
// Replace this comment and the next line with your code
console.log(arr);
const newArray = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
newArray.push(arr[i][j]);
}
}
return newArray.sort();
}

function flattenArray3d(arr) {
// Replace this comment and the next line with your code
console.log(arr);
const arr3dim = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let k = 0; k < arr[i][j].length; k++) {
arr3dim.push(arr[i][j][k]);
}
}
}
return arr3dim;
}

console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]
Expand Down
6 changes: 5 additions & 1 deletion Week3/homework/step2-7.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ f2(y);

console.log(y);

// Add your explanation as a comment here
/* In the x-code, we are logging the value of x, which has not changed. If we want this code to return 10,
we need to console.log(f1(x)). In the y-code, y is an object. The properties of an object can be altered this way,
so the function changes the value of x to 10. No alteration has been done to the variable reference itself. If f2
attempted to change the *value* of y (rather than an attribute of its object), it would yield results like the x-code.
For example, if the function is altered, val = { x: 10 }; return val;, it will return { x: 9 } */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you get it (?). Another example:

let a = 4
let b = a
a += 1
What's the value of b?

vs objects...

let a = { name: 'gabe' }
b = a
a.country = 'us'
what's the value of b?

Do you see the difference?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - I assume it has to do with how JS stores them in memory - when b is set to an object, it's a reference to that object, not a new object with the same details. So whenever a property of 'a' changes, 'b' will 'change' as well (because they're both just pointing to the same object). But when a = 4 and b = a, it's setting b equal to a value of 4 (not pointing to an object). So changing the value of 'a' after that doesn't change the value of 'b'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the objects example, both a and b would have a value of

{
name: 'gabe',
country: 'us'
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

6 changes: 4 additions & 2 deletions Week3/homework/step3-bonus.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];

function makeUnique(arr) {
// Replace this comment and the next line with your code
console.log(arr);
const newArray = arr.filter(function(index, current) {
return arr.indexOf(index) === current;
});
return newArray;
}

const uniqueValues = makeUnique(values);
Expand Down
7 changes: 5 additions & 2 deletions Week3/homework/step3.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

function createBase(base) {
// Replace this comment and the next line with your code
console.log(base);
return function(number) {
return number + base;
};
}

const addSix = createBase(6);
const addSeven = createBase(7);

console.log(addSix(10)); // returns 16
console.log(addSix(21)); // returns 27
console.log(addSeven(21)); // returns 28

// Do not change or remove anything below this line
module.exports = createBase;
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.