Day 4 of JavaScript30

Array exercises is always useful and I enjoyed these. Think I solidified some concepts here that was needed.

Some alternative solutions again:

number 3 when sorting by the birth date. You shouldn’t leave out the return 0 for duplicate which Wes Bos does in his example instead something like this is more stable:


     const sortedInventors = inventors.sort((a, b) => {
       const property = 'year';
        if (a[property] < b[property]) { 
          return -1; 
         } 
        if (a[property] > b[property]) {
          return 1;
        }
        // a must be equal to b
        return 0;
      });
      console.table(sortedInventors);

My solution to no 5, Same thing again:


    const yearLived = inventors.sort(function(a,b){
      const lastGuy = a.passed - a.year;
      const nextGuy = b.passed - b.year;

      if (lastGuy < nextGuy) {  
         return -1; 
         } 
         if (lastGuy > nextGuy) {
        return 1;
          }
      // a must be equal to b
      return 0;
    });
    console.table(yearLived);

6. create a list of Boulevards in Paris that contain ‘de’ anywhere in the name from
https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris

What I found most interesting here is that we use different methods to turn the nodelist into an array.
Wes Bos is using

var nodes = Array.from(nodes);

while I use

 var nodes = Array.prototype.slice.call(nodes);

My solution:

    var titles = document.querySelectorAll(".mw-category-group ul li");
    titles = Array.prototype.slice.call(titles);
    var deTitles = titles.filter(function(event){
        if (event.innerText.toLowerCase().indexOf("de") >= 0){
          return true;
        }
    })
    
    console.log(deTitles);

Add your comment