Repository navigation
Utku / Class 19 / Homework Week 3 #244
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,3 +59,5 @@ typings/ | |
|
|
||
| .netlify | ||
| dist/ | ||
| exercise | ||
| live | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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++) { | ||
| 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); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify this code by using two if (i % 3 === 0) {
threeCallback(i);
}
if (i % 5 === 0) {
fiveCallback(i);
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.'
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for this
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you do need to test before entering the loop whether
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment below with respect to creating the variable
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this one is shorthened. |
||
| } | ||
|
|
||
| const uniqueValues = makeUnique(values); | ||
|
|
||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just return
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand. It is shorthened. |
||
| }; | ||
| } | ||
|
|
||
| const addSix = createBase(6); | ||
|
|
||
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
iinside the parentheses of theforloop, like this:Apart from this code being more concise, it also ensures that the
ivariable is created inside the block scope of theforloop body (because you are, correctly, usingletand notvar). 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.