The journey to becoming a developer

My future is created by what I do today, not tomorrow.

Algorithms

자바스크립트 100제 : 1~9번 정리

Millie 2021. 10. 5. 10:05

 

알고리즘 스터디를 함께 하는 분이 자바스크립트 100제라는 것을 알려주셔서, 오늘부터 이것도 병행해서 풀기로 했다.

이 책은 저작권 없이 무료로, 심지어 노션으로 제공이 된다. 문제를 보고, 바로 밑에 코드블럭을 넣어서 풀 수 있어서 정말 편리하다. 대신 문제의 해설 강의를 보고 싶다면 인프런에서 유료로 구매해서 볼 수 있다.

 

1권과 2권으로 각 50문제씩 나눠져 있는데, 초반에는 메서드의 사용 방법만 알면 풀 수 있는 쉬운 문제이지만, 뒤로 갈수록 생각할 게 많아지는 약간은 복잡한 문제로 구성되어 있다.

 

1번부터 9번까지의 문제는 로직을 짤 필요 없이 메서드의 사용 방법과 자바스크립트 문법만 안다면 풀 수 있는 문제들이다. 그래서 메서드들의 사용 방법을 복습하고, 정리해 보려고 한다. MDN을 참고하여 정리하였다.

 


 

Array Methods

pop

The pop() method removes the last element from an array and returns that element. This method changes the length of the array. If you call pop() on an empty array, it returns undefined.

Array.prototype.shift() has similar behavior to pop, but applied to the first element in an array. 

  • 배열의 마지막 요소를 제거하고, 그 요소를 반환한다.
  • 원본 배열을 변화시킨다.
  • 비슷한 것으로는 shift()가 있는데 이것은 배열의 첫 번째 요소에 적용된다.

slice

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

  • 배열의 begin부터 end까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환한다.
  • 원본을 변화시키지 않는다.

splice

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice(). 

  • 배열의 기존 요소를 삭제/교체 혹은 새 요소를 추가한다.
  • 원본을 변화시킨다.

 

typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

typeof 연산자는 평가되지 않은 피연산자의 자료형을 나타내는 문자열을 반환한다.

number

  • 37, 3.14같은 정수와 실수
  • Math.LN2, Math.E
  • Infinity
  • NaN : Not-A-Number라고 해도 타입은 number이다.
  • Number('1') : Number tries to parse things into numbers
  • Number('shoe') : including values that cannot be type coerced to a number (숫자로 강제 변환할 수 없는 값도 포함한다.)

string

  • '1', 'string' 같은 일반적인 문자열
  • '' (empty string)
  • `template literal`
  • typeof 1 같은 것. typeof은 항상 문자열을 반환한다.
  • String(1) : String converts anything into a string, safer than toString

Boolean

  • true, false
  • Boolean(1) : Boolean() will convert values based on if they're truthy or falsy
  • !!(1) : two calls of the ! (logical NOT) operator are equivalent to Boolean()

Symbol

  • Symbol
  • Symbol('foo')
  • Symbol.iterator

undefined

  • undefined
  • declared but undefined variable
  • undeclared variable

Object

  • object : {a: 1}
  • array : use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
  • new Date()
  • /regex/

Function

  • function() {}
  • class C {}
  • Math.sin

 

True or False

  • false : 0, -0, null, false, NaN, undefined, empty string('')
  • true : all other values, including any object, an empty array([]), or the string "false"

 

변수 이름 작성 규칙

  • 변수명의 첫 글자는 문자, underscore, $로 시작할 수 있다.
  • 숫자로 시작할 수 없다.
  • 예약어는 사용 불가능하다.
  • 대소문자를 구분한다.

 

객체의 key 중복

key가 중복되었을 경우엔 마지막 key의 값을 가져온다.

var d = {
    'height':180,
    'weight':78,
    'weight':84,
    'temperature':36,
    'eyesight':1
};

console.log(d['weight']); // 84

참고할 만한 글 : 배열 내 객체 프로퍼티 값 중, 중복 id 값 제거

 

JavaScript - new Date 날짜 다루기

JavaScript - new Date 날짜 다루기

kyounghwan01.github.io

 

concat()

concat()은 string과 array에 모두 사용될 수 있는 메서드이다.

String.prototype.concat()

The concat() method concatenates the string arguments to the calling string and returns a new string.

매개변수로 전달된 문자열을 메서드를 호출한 문자열에 붙여 새로운 문자열로 반환한다.

 

Array.prototype.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

두 개 이상의 배열을 합칠 때 쓴다. 기존 배열을 수정하지 않고 새로운 배열을 반환한다.

 


 

기본적이지만 중요한 개념들을 한 번씩 다시 정리하면서 복습할 수 있는 기회가 되었다.