-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Hi Hamza, here is my feedback on your homework.
Step 4.1
You were supposed to call the foo function like this (notice no parentheses after bar):
foo(bar);When calling foo in this way you pass the function bar as a parameter to foo. In your case, you are calling bar and pass the result the result of bar (which is undefined) to foo. We explained this in the last lecture.
The correct code is like this:
function foo(callback) {
callback();
}
function bar() {
console.log('Hello, I am bar!');
}
foo(bar);Step 4.2
While the code works, the approach you have taken for testing divisibility is a bit convoluted:
- First you divide the number by respectively 3 or 5.
- Then you test using the module operator whether the division result is a whole number.
It is simpler and easier to follow if you use the modulo operator directly, e.g.
if (arrNum[j] % 3 === 0) {
sayThree(arrNum[j]);
}
if (arrNum[j] % 5 === 0) {
sayFive(arrNum[j]);
}Step 4.3
Missing
Step 4.4
Missing
Step 4.5
Missing
Step 4.6
See the solution that I posted in slack shows how you can do this with a recursive function.
Step 4.7
I'm not entirely sure, but I think you understand the difference between call by value and call by reference.
Step 5
OK, but you can replace += by simply + because the value of x is not used after the return statement.
Bonus
Both solutions produce the correct result.
In the second solution you don't really need the value variable. And you should not use console.log inside your function. Let the caller decide what to do with the new array, perhaps console.log it or do something else. It's all about reusability. Look how I modified your code:
function newArray2(arr) {
let obj = {};
let newArray = [];
// let value = 1
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = 1;
// value++;
}
for (let key in obj) {
newArray.push(key);
}
return newArray;
}
console.log(newArray2(arr));