Utku / Class 19 / Homework Week 3#244
Utku / Class 19 / Homework Week 3#244allhandsondeck wants to merge 2 commits intoHackYourFuture:masterfrom allhandsondeck:week3
Conversation
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.
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.
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.
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.
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.
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.
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.
Just a comment on the English language: 'can divide by 3' -> 'can be divided by 3.'
There was a problem hiding this comment.
Grammar error, corrected.
Week3/homework/step2-3.js
Outdated
|
|
||
| // 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.
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.
Absolutely Sir. If statement is deleted.
Week3/homework/step2-3.js
Outdated
|
|
||
| // Replace this comment and the next line with your code | ||
| console.log(str, num, result); | ||
| do { |
There was a problem hiding this comment.
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.
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.
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.
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.
See my comment below with respect to creating the variable unique.
There was a problem hiding this comment.
Also this one is shorthened.
|
Appreciated for your review Sir. Carefully read and corrected. |
All tests have been carried out.