Blog Day 35: convertListToObject & convertDoubleSpaceToSingle

Inseo Park·2021년 8월 11일
0

Path to Data Scientist

목록 보기
33/58
post-thumbnail

1. TIL (Today I Learned)

convertListToObject

Take a two-dimensional array (an array with an array as an element) as input and return an object created using each array.

function convertListToObject(arr) {
  let result = {};
  for (i = 0; i < arr.length; i++) {
      if (arr[i].length > 0 && result[arr[i][0]] === undefined) { // return only if arr[i] is not an empty array && if result[key]'s value is not defined.
        result[arr[i][0]] = arr[i][1];
    }
  }
  return result;
}

Reference Code:

function convertListToObject(arr) {
  let result = {};

  for (let i = 0; i < arr.length; i++) {
    if (arr[i].length > 0 && result[arr[i][0]] === undefined) {
      result[arr[i][0]] = arr[i][1];
    }
  }

  return result;
}

convertDoubleSpaceToSingle

Take a string as input and return a string in which all two spaces in the string are replaced with a single space.

function convertDoubleSpaceToSingle(str) {
  return str.split("  ").join(" ");
}

Reference Code:

  function convertDoubleSpaceToSingle(str) {
  let result = '';
  let before = '';
  for (let i = 0; i < str.length; i++) {
    // 직전의 문자가 공백이고, 현재의 문자도 공백인 경우
    // 즉, 현재의 문자가 두 번째 공백인 경우(에만), 무시한다.
    if (before !== ' ' || str[i] !== ' ') {
      result = result + str[i];
    }
    before = str[i];
  }
  return result;
}

2. 3 Things to be Thankful for

  1. Thank God for guiding me throughout the day and keeping me safe.
  2. Thank God for keeping my family good.
  3. Thank God for giving me the ability to try to work hard in all things.

3. Ideas and Things to think about

  1. Remember that it is also important to not let other people feel you are too obssessed on something. Even if what you are obssessed is something good, keep it a secret and act as if there is nothing. Keep your hustle a secret.
profile
my journey to become a data scientist.

0개의 댓글