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.

Homework week3 #40

Closed
wants to merge 4 commits into from
Closed

Conversation

Meazer
Copy link

@Meazer Meazer commented May 31, 2018

No description provided.


console.log('Hello, I am foo! I can call another function:');
func();

Copy link

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');
}
Copy link

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));
Copy link

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);
Copy link

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]]));
Copy link

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);
Copy link

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));
Copy link

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.
Copy link

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);
Copy link

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));
Copy link

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.

@rohni
Copy link

rohni commented Jun 8, 2018

Nice work Meazer.
Nicely laid out. I have some comments, but mostly they are style or ideas of different ways to approach a particular problem that you might find interesting or useful.

Well done, and good luck with the rest of the course. 😄

@Meazer
Copy link
Author

Meazer commented Jun 10, 2018

Thank you Rohni for your time

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