-
Notifications
You must be signed in to change notification settings - Fork 328
Conversation
|
||
console.log('Hello, I am foo! I can call another function:'); | ||
func(); | ||
|
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.
Nice work!
I would suggest returning the func()
call.
The foo
function does not know how the function will be used, and so should return the results of calling the function.
map, filter, and reduce all return the results of calling the function on the element.
|
||
function sayFive(num) { | ||
console.log(num + ' is divisible by five'); | ||
} |
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.
✅
} | ||
|
||
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3)); | ||
console.log('do..while', repeatStringNumTimesWithDoWhile('abc', 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.
✅
console.log(myCar); | ||
|
||
const myMotorBike = new MotorBike(); | ||
console.log(myMotorBike); |
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.
✅
} | ||
|
||
|
||
console.log(multiplyAll([[1, 2], [3, 4], [5, 6, 7]])); |
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.
✅
for (let i = 0; i < arr.length; i++) { | ||
printArray(arr[i]); | ||
} | ||
} else console.log(arr); |
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.
Nice work.
One style point here. While what you wrote is legal and interpreted correctly by the interpretor, it is confusing to read. I would suggest wrapping the else statement in {} as well.
return product; | ||
} | ||
|
||
console.log(multiplyAll(arr3d)); |
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.
This works perfectly. 😄
What i find very interesting is that there is an awful lot of duplicate code to do, essentially, the same thing in these two operations. On slack Jim gave the flattenArray
function, which then allows you to work with the values. And this is one of the great powers of using higher order functions.
Here is an example using reduce, and then doing the two actions above:
const flatten = (arr, result=[]) => {
return arr.reduce((acc, elem) => {
if (Array.isArray(elem)) {
return acc.concat(flatten(elem, result));
} else {
return acc.concat([elem]);
}
}, result);
}
Now I want to print each element of the array:
flatten(arr3d).forEach(item => console.log(item));
And to find the product of multiplying all the items in the nested array
flatten(arr3d).reduce((product, el) => product * el)
And it keeps going. So you take the thing you want to do frequently and try to create a utility that handles it for you, and then you only have to think about the specific use you have each time you have to flatten the array. And it makes it much more readable as you are not bogged down with implementation details.
And the other reason, and one of my favourites, is that if it is hard to do, you only have to hurt your brain once trying to figure it out. 😉
// Add your explanation as a comment here | ||
// numbers and strings passed to functions by value | ||
// so changing the argument inside the function doesn’t affect the variable passed from outside the function | ||
// objects passed by reference and changing the argument inside the function affect the variable passed from outside the function. |
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.
✅
return uniqueArr; | ||
} | ||
|
||
const uniqueValues = removeRepetition(values); | ||
console.log(uniqueValues); |
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.
✅ Excellent.
addSix(21); // returns 27 | ||
const addFive = createBase(5); | ||
console.log(addFive(5)); | ||
console.log(addFive(10)); |
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 you go, another good example of using higher-order functions.
The more you start using them, the more uses you will find for this technique.
Nice work Meazer. Well done, and good luck with the rest of the course. 😄 |
Thank you Rohni for your time |
No description provided.