TIL_0604 알고리즘풀이

Koohyeon·2021년 6월 4일

Algorithm

목록 보기
8/19

15. 가운데 문자 출력

    function solution(str) {
      let answer = '';
      const index = Math.round(str.length / 2) - 1;
      if (str.length % 2 === 0) answer = [str[index], str[index+1]];
      else answer = str[index];
      return answer;
    }

->Math.floor() : 소수점 이하를 버림.
Math.ceil() : 소수점 이하를 올림.
Math.round() : 소수점 이하를 반올림.

The substring() method returns the part of the string between the start and end indexes, or to the end of the string.

알고 있던 것도 헷갈리고, 모르는 것도 여전히 너무 많다.

16. 중복문자제거

    function solution(str) {
      let answer = str.split('');
      answer.map((char, index) => {
        for(let i = index + 1; i < answer.length; i++) {
          if(char == answer[i]) answer = answer.slice(i);
        }
      })
      return answer;
    }

-> index가 연산 중 바뀌는 것을 생각 못했다. 바보.

0개의 댓글