Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

.netlify
dist/
exercise
live
3 changes: 1 addition & 2 deletions Week3/homework/step2-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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
22 changes: 15 additions & 7 deletions Week3/homework/step2-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

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);
let i = startIndex;
for (i; i <= stopIndex; i++) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can (and should) declare and initialize the loop index variable i inside the parentheses of the for loop, like this:

for (let i = startIndex; i <= stopIndex; i++) {

Apart from this code being more concise, it also ensures that the i variable is created inside the block scope of the for loop body (because you are, correctly, using let and not var). The way you have done it creates the variable in the local scope of the function where it is not needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Got it and put it inside of for loop as you show.

if (i % 3 === 0 && i % 5 === 0) {
threeCallback(i);
fiveCallback(i);
} else if (i % 3 === 0) {
threeCallback(i);
} else if (i % 5 === 0) {
fiveCallback(i);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can simplify this code by using two if statements in a row, rather than chaining them in an if-then-else sequence. Each condition is then tested independently:

if (i % 3 === 0) {
  threeCallback(i);
}
if (i % 5 === 0) {
  fiveCallback(i);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I was also uncomfortable with these if statement block and then solved more logically as you say.

numbers.push(i);
}
console.log('numbers:', numbers);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You did not completely follow the assignment here as it asked to create the array first and then loop through the elements (requiring to for loops in a row rather one for loop:

The function should first generate an array containing values from start value to end value (inclusive).

Then the function should take the newly created array and iterate over it, and calling the first callback if the array value is divisible by 3.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right Sir. I changed sequence and added another for loop for creating array and iterating it.

}

function sayThree(number) {
// Replace this comment and the next line with your code
console.log(number);
console.log(number, 'can divide by 3');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a comment on the English language: 'can divide by 3' -> 'can be divided by 3.'

@allhandsondeck allhandsondeck Jan 23, 2019 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Grammar error, corrected.

}

function sayFive(number) {
// Replace this comment and the next line with your code
console.log(number);
console.log(number, 'can divide by 5');
}

threeFive(10, 15, sayThree, sayFive);
Expand Down
19 changes: 13 additions & 6 deletions Week3/homework/step2-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line
let result = '';

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is no need for this if statement as the test num > 0 is already done by the for loop. The for loop will execute the loop body only while num > 0.

@allhandsondeck allhandsondeck Jan 23, 2019 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Absolutely Sir. If statement is deleted.

for (num; num > 0; num--) {
result += str;
}
}

return result;
}
Expand All @@ -18,8 +21,10 @@ function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);
while (num > 0) {
result += str;
num--;
}

return result;
}
Expand All @@ -31,8 +36,10 @@ function repeatStringNumTimesWithDoWhile(str, num) {
// eslint-disable-next-line
let result = '';

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Here, you do need to test before entering the loop whether num <= 0. This is because in the case of the do-while loop, the condition is tested at the end of the loop and therefore the loop body is executed at least once. This also makes the do-while loop rather useless in practice, and it can always be rewritten as a regular while loop.

@allhandsondeck allhandsondeck Jan 23, 2019 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Got it. I couldn't think that. do-while loop is implemented in if statement.

result += str;
num--;
} while (num > 0);

return result;
}
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 = 'rocky';
this.color = 'red';
this.numLegs = 5;
}

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 = arr[i][j] * product;
}
}

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

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

function printArray3d(arr) {
// Replace this comment and the next line with your code
console.log(arr);
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++) {
console.log(arr[i][j][k]);
}
}
}
}

printArray2d(arr2d);
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
// Pass by value rule: if you use a variable inside a function and change its value
// it doesn't affect variable's value which is outside of the function.

// Pass by reference rule: if you use a variable's address or reference inside a function
// and change them it affects variable's value which is outside of the function.
4 changes: 2 additions & 2 deletions Week3/homework/step3-bonus.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 unique = arr.filter((x, y, z) => z.indexOf(x) === y);
return unique;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

See my comment below with respect to creating the variable unique.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Also this one is shorthened.

}

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

function createBase(base) {
// Replace this comment and the next line with your code
console.log(base);
return function addSix(num) {
const sum = base + num;
return sum;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You could just return base + sum directly without creating an intermediate variable sum. However, sometimes it makes sense to create an intermediate variable as you did here if you want to make it absolutely clear how the human reader should interpret the result of the computation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I understand. It is shorthened.

};
}

const addSix = createBase(6);
Expand Down