Blog Day 27: Higher-Order Function Practical Question (studentReports, sumOfArraysInArray)

Inseo Park·2021년 8월 3일
0

Path to Data Scientist

목록 보기
26/58
post-thumbnail

1. TIL (Today I Learned)

  1. Today was a not so easy day but as I came back home after work with a negative mind-set, something good came into my mind. I remembered that I should be thanful for what I have and stop thinking about what I lack. This gave me a good mind-set to just give myself a rest when I want to and leave all the rest to God.
  2. I decided to share my few practical questions I go through on weekends with my pair (I failed to do so this week), which is about higher-order functions. I finished 2 practical questions which I will describe and explain below.

The first problem was studentReports.

Problem :
You will receive a array which holds a value in the form of an object and the information is about student's information. The data set looks something like this

	let studentList = [
        {
          name: 'Anna',
          gender: 'female',
          grades: [4.5, 3.5, 4],
        },
        {
          name: 'Dennis',
          gender: 'male',
          country: 'Germany',
          grades: [5, 1.5, 4],
        },
        {
          name: 'Martha',
          gender: 'female',
          grades: [5, 4, 4, 3],
        },
        {
          name: 'Brock',
          gender: 'male',
          grades: [4, 3, 2],
        },
      ];

Your mission is to get a new array which holds the information of 'female' students and the 'grades' to averaged.

	console.log(output); // -->
        [
          { name: 'Anna', gender: 'female', grades: 4 },
          { name: 'Martha', gender: 'female', grades: 4 },
        ];
        

Your return answer should be something like the above.
I will now show you my code and the code holds some explanation part.

	function studentReports(students) {
      
      // Filter the array 'students' to get an array called femaleArr which consists of only elements with 'gender' = female // 
	
    	const femaleArr = students.filter(function(el) {
	return el.gender === 'female';
    });
     
     // inside the femaleArr.map function return another el.grades.reduce function to put the sum of grades for each student in 'sum'
	
   	 return femaleArr.map(function(el) {
	 let sum = el.grades.reduce(function(acc, val) {
	 return acc + val;
     });
     
        // now assign 'avg' to find the mean which is 'sum' divided by the length of el.grades. 

    	 const avg = sum / el.grades.length;
     
     	// because the el.grades.length is inside the function map, it goes through one student at a time.
     
        // change the value inside the key grades to become the average.
        
        el.grades = el;
        
       // return the whole array with females information and mean grades.

        return el;
        });
        }
        

The second problem was sumOfArraysInArray.

Problem:
The input is a multi-dimensional array,

let output = [
    [1, 2],
    [undefined, 4, '5'],
    [9, 'hello'],
    ];
    

The question requires you to find the sum of all 'number' values inside the multi-dimensional array.

The code I wrote is written below with explanation:

    function sumOfArraysinArray(arr) {
	 
     // make all the multi-dimensional arrays to become a single array by using concat to merge

    const mergedArray = arr.reduce(function(acc, val) {
    return acc.concat(val)
    }, []);
    
    // return the mergedArray into another reduce function and if the current value is a 'number' 

    // add it to the accumulative value which is set as '0'. If there is no number, return accumulative value which is '0'.
    
    return mergedArray.reduce(function(acc, val) {
    if(typeof val === 'number') {
        return acc + val;
        } else {
       return acc;
   }, 0);
 }
 

2. 3 Things to be Thankful for

  1. Thankful for a wonderful day to start off.
  2. Thankful for God reminding me to give thanks not complain.
  3. Thankful for working out well.

3. Ideas and Things to Think about

  1. Never ever complain because God gave you a lot and a lot of grace. You complaining will bring you no where. When you want to complain, praise God.
profile
my journey to become a data scientist.

0개의 댓글