-
Notifications
You must be signed in to change notification settings - Fork 328
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @u61, good work here. Some minor comments, but I have no issue with approving this PR.
Week3/homework/step2-2.js
Outdated
// 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++) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
threeCallback(i); | ||
} else if (i % 5 === 0) { | ||
fiveCallback(i); | ||
} |
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Week3/homework/step2-2.js
Outdated
} | ||
|
||
function sayThree(number) { | ||
// Replace this comment and the next line with your code | ||
console.log(number); | ||
console.log(number, 'can divide by 3'); |
There was a problem hiding this comment.
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.'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar error, corrected.
Week3/homework/step2-3.js
Outdated
@@ -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) { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
Week3/homework/step2-3.js
Outdated
@@ -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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Week3/homework/step3.js
Outdated
console.log(base); | ||
return function addSix(num) { | ||
const sum = base + num; | ||
return sum; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Week3/homework/step3-bonus.js
Outdated
// 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; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
Appreciated for your review Sir. Carefully read and corrected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@u61, thanks for your rapid and accurate follow-up.
All tests have been carried out.