Blog Day 28: transformFirstAndLast

Inseo Park·2021년 8월 4일
0

Path to Data Scientist

목록 보기
27/58
post-thumbnail

1. TIL (Today I learned)

transformFirstAndLast

Today afternoon, a discord message from our manager for codestates reminded us that we have a exercise added to our calendar. Algorithm questions for us to let us practice and not forget about past lessons. The manager said that it is vital to do it consistently everyday. A question a day or more if you can actually bear it(I failed on the 2nd question today). To remind myself and also add something to my daily blog, I will add the first question which I succeeded:

The question was to receive a input(array) which consists of different strings as elements. The problem wanted me to not touch the given array and return a new object which consists of the first element of the array as a key and the last element of the array as the key's value.
Result should look like the code below.

	let arr = ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'];
	let output = transformFirstAndLast(arr);
	console.log(output); // --> { Queen : 'Beyonce' }
    
    

Now the below code is what I came up with.

function transformFirstAndLast(arr) {
  const newObj = {};  // new Object is made, a blank one;
  if(arr.length === 0){
    return newObj; // if arr is a empty array, reutrn newObj which is empty object;
  } else {
   newObj[arr[0]] = arr[arr.length - 1]; // if arr is not an empty array, add the first value as a key and last value as a value;
  }
  return newObj;
}

It wasn't too hard but quite challenging and a good reminder.

2. 3 Things to be thankful for

  1. Thankful for being able to finish a algorithm question.
  2. Thankful for being safe and healthy.
  3. Thankful for giving me someone to rely on.

3. Ideas and things to think about.

  1. As tech-student, we have to think in different ways than that of the normal student. Tech-students have to be eager to fail and try new things. We should not focus on going into one deep thing but we just have to know how to use different tools to come up with something new. A innovative mindset is key and vital. Look at Elon Musk, he doesn't care about other things but always focus on how to be the guy who comes up with something no one else ever did. That is a true tech-student mindset.
profile
my journey to become a data scientist.

0개의 댓글