Day4 - Array Cardio Day 1 ๐Ÿ’ช

ujinujinยท2022๋…„ 2์›” 22์ผ
0

30days JS coding challenge

๋ชฉ๋ก ๋ณด๊ธฐ
4/9

์ฃผ์ œ: ๋ฐฐ์—ด์„ ๋‹ค๋ฃฐ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ method๋“ค ์ตํžˆ๊ธฐ

๐Ÿง‘โ€๐Ÿ’ป ๊ฐ•์˜ ์˜์ƒ

์ฝ”๋“œ๋น„๊ต

๋‚ด ์ฝ”๋“œ

<script>
 	// Array.prototype.filter()
    // 1. Filter the list of inventors for those who were born in the 1500's

    const bornIn1500 = inventors.filter((v) => v.year >= 1500 && v.year < 1600)
    console.log(bornIn1500)

    // Array.prototype.map()
    // 2. Give us an array of the inventors first and last names
    const names = inventors.map(v => "" + v.first + " " + v.last)
    console.log(names)

    // Array.prototype.sort()
    // 3. Sort the inventors by birthdate, oldest to youngest
    const age = inventors.sort((a,b) => a.year - b.year)
    console.log(age)
    
    // Array.prototype.reduce()
    // 4. How many years did all the inventors live all together?
    const totalYears = inventors.reduce((acc, inventor) => {
      return acc + (inventor.passed - inventor.year)
    }, 0 )
    console.log(totalYears)
    
    // 5. Sort the inventors by years lived
    const yearsLived = inventors.sort((a,b) => (b.passed - b.year) - (a.passed - a.year))
    console.log(yearsLived)


    // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
    // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
    // const category = document.querySelector('.mw-category');
    // const links = [...ategory.querySelectorAll('a')];
    // const de = links
    //         .map(link => link.textContent)
    //         .filter(streetName => streetName.includes('de'));

    // 7. sort Exercise
    // Sort the people alphabetically by last name
    const sortedName = people.sort((a,b) =>  {return a.split(', ')[0] > b.split(', ')[0] ? 1 : -1})
    console.log(sortedName)

    // 8. Reduce Exercise
    // Sum up the instances of each of these
    const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
    const sumInstances = data.reduce((acc, value) => {
      if (acc[value] === undefined) acc[value] = 1
      else acc[value] += 1
      return acc
    }, {})
    console.log(sumInstances)
</script>

์ œ๊ณต๋œ ์ฝ”๋“œ

<script>
	// Array.prototype.filter()
    // 1. Filter the list of inventors for those who were born in the 1500's
    const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
    console.table(fifteen);

    // Array.prototype.map()
    // 2. Give us an array of the inventor first and last names
    const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
    console.log(fullNames);

    // Array.prototype.sort()
    // 3. Sort the inventors by birthdate, oldest to youngest
    // const ordered = inventors.sort(function(a, b) {
    //   if(a.year > b.year) {
    //     return 1;
    //   } else {
    //     return -1;
    //   }
    // });

    const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
    console.table(ordered);

    // Array.prototype.reduce()
    // 4. How many years did all the inventors live?
    const totalYears = inventors.reduce((total, inventor) => {
      return total + (inventor.passed - inventor.year);
    }, 0);
    console.log(totalYears);

    // 5. Sort the inventors by years lived
    const oldest = inventors.sort(function(a, b) {
      const lastInventor = a.passed - a.year;
      const nextInventor = b.passed - b.year;
      return lastInventor > nextInventor ? -1 : 1;
    });
    console.table(oldest);

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

    // const category = document.querySelector('.mw-category');
    // const links = Array.from(category.querySelectorAll('a'));
    // const de = links
    //             .map(link => link.textContent)
    //             .filter(streetName => streetName.includes('de'));

    // 7. sort Exercise
    // Sort the people alphabetically by last name
    const alpha = people.sort((lastOne, nextOne) => {
      const [aLast, aFirst] = lastOne.split(', ');
      const [bLast, bFirst] = nextOne.split(', ');
      return aLast > bLast ? 1 : -1;
    });
    console.log(alpha);

    // 8. Reduce Exercise
    // Sum up the instances of each of these
    const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick'];

    const transportation = data.reduce(function(obj, item) {
      if (!obj[item]) {
        obj[item] = 0;
      }
      obj[item]++;
      return obj;
    }, {});
    console.log(transportation);
</script>

JS๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๋ฉด์„œ ์ต์ˆ™ํ•ด์กŒ๋˜ map, reduce๋“ฑ์„ ๋‹ค์‹œ ์ •๋ฆฌํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์ด์—ˆ๋‹ค.
sort๋Š” ์ข€ ๋” ๊ณต๋ถ€ํ•˜๊ณ ๋‚˜์„œ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌ๋ฅผ ๋”ฐ๋กœ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.

profile
๋ฐฑ์ˆ˜์™€ ์ทจ์ค€์ƒ ๊ทธ ์‚ฌ์ด ์–ด๋””

0๊ฐœ์˜ ๋Œ“๊ธ€