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

Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Utku / Class 19 / Homework Week 3 #244

Closed
wants to merge 2 commits into from
Closed

Utku / Class 19 / Homework Week 3 #244

wants to merge 2 commits into from

Conversation

allhandsondeck
Copy link

All tests have been carried out.

remarcmij
remarcmij previously approved these changes Jan 23, 2019
Copy link
Collaborator

@remarcmij remarcmij left a 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.

// 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
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
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.

threeCallback(i);
} else if (i % 5 === 0) {
fiveCallback(i);
}
Copy link
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
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
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
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
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.'

Copy link
Author

@allhandsondeck allhandsondeck Jan 23, 2019

Choose a reason for hiding this comment

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

Grammar error, corrected.

@@ -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
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.

Copy link
Author

@allhandsondeck allhandsondeck Jan 23, 2019

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.

@@ -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
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.

Copy link
Author

@allhandsondeck allhandsondeck Jan 23, 2019

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.

console.log(base);
return function addSix(num) {
const sum = base + num;
return sum;
Copy link
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
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.

// 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
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
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.

@allhandsondeck
Copy link
Author

Appreciated for your review Sir. Carefully read and corrected.

Copy link
Collaborator

@remarcmij remarcmij left a 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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants