It extends the functionality through an optional, third parameter, which should be an object specifying the innerHTML and/or the attributes that you want to set for the new HTML element. It can make your code a bit more concise and reduce the need to introduce intermediate variables. For instance, you could rewrite your getDetails() function as follows:
The code is shorter and there is no longer a need to introduce variables li1, li2, li3 and li4. (Note that instead of using textContent I've used innerHTML. Technically, there is a difference between the two, but in the way we use it here it makes no difference).
You can apply the same principle in other places where you use createAndAppend(), e.g. in function main().
I wrote this function createAndAppend() because I noticed the repetition we were doing with createElement() and appendChild(). Then later, I realised that setting attributes was also repetitively done in a couple of places and that I could improve createAndAppend() to the current version that accepts this optional third parameter. I'm telling you this to show how you should be looking out for ways to make your code simpler and more concise, to make it easier for yourself and reduce the chance for error.
2. This code snippet comes from your function main():
In the then() method you has passed a (fat arrow) function that takes a single parameter and executes the function setupSelect() passing that same parameter. By setupSelect() itself is a function that takes a single parameter. The same holds true for renderError(). So, you could simply rewrite this snippet as follows:
3. As per my feedback, you replaced some for loops with forEach. But you are not taking full advantage of the forEach construct. The (fat arrow) function that you pass as the argument to forEach() receives as its first argument, the 'next' array element. If we look at your getContributors() function, we get contributor as the first argument. This contributor is the same as contributors[i].
This becomes self-evident if you could peek inside the implementation of forEach. It will look like this (since you now know about this and prototype):
Array.prototype.forEach = function (fn) {
for (let i = 0; i < this.length; i++) {
fn(this[i], i, this);
}
};
So your code could be simplified (including the improved createAndAppend) to:
Finally, most JavaScript developers now prefer two spaces instead of four to indent their code. If you would like to try that, in VSCode you can do that by adding these User Settings:
Hi Aya, here is my feedback on your week 2 homework.
Your week 2 homework looks fine, and the code is a pleasure to look at. I think you covered all the issues I identified in my previous feedback.
The comments I'm going to make are about details to further improve already excellent code.
1. At some point in the lectures, I introduced a slightly more advanced version of the
createAndAppend
utility function:It extends the functionality through an optional, third parameter, which should be an object specifying the
innerHTML
and/or the attributes that you want to set for the new HTML element. It can make your code a bit more concise and reduce the need to introduce intermediate variables. For instance, you could rewrite yourgetDetails()
function as follows:The code is shorter and there is no longer a need to introduce variables
li1
,li2
,li3
andli4
. (Note that instead of usingtextContent
I've usedinnerHTML
. Technically, there is a difference between the two, but in the way we use it here it makes no difference).You can apply the same principle in other places where you use
createAndAppend()
, e.g. in functionmain()
.I wrote this function
createAndAppend()
because I noticed the repetition we were doing withcreateElement()
andappendChild()
. Then later, I realised that setting attributes was also repetitively done in a couple of places and that I could improvecreateAndAppend()
to the current version that accepts this optional third parameter. I'm telling you this to show how you should be looking out for ways to make your code simpler and more concise, to make it easier for yourself and reduce the chance for error.2. This code snippet comes from your function
main()
:In the
then()
method you has passed a (fat arrow) function that takes a single parameter and executes the functionsetupSelect()
passing that same parameter. BysetupSelect()
itself is a function that takes a single parameter. The same holds true forrenderError()
. So, you could simply rewrite this snippet as follows:3. As per my feedback, you replaced some
for
loops withforEach
. But you are not taking full advantage of theforEach
construct. The (fat arrow) function that you pass as the argument toforEach()
receives as its first argument, the 'next' array element. If we look at yourgetContributors()
function, we getcontributor
as the first argument. Thiscontributor
is the same ascontributors[i]
.This becomes self-evident if you could peek inside the implementation of
forEach
. It will look like this (since you now know aboutthis
andprototype
):So your code could be simplified (including the improved
createAndAppend
) to:Finally, most JavaScript developers now prefer two spaces instead of four to indent their code. If you would like to try that, in VSCode you can do that by adding these User Settings:
If you now reformat your code in VSCode it will indent with two space.
Overall, excellent work. I'm looking forward to review your final version next week.