[TIL : 2] JavaScript 기초

jabae·2021년 10월 7일
0

TIL

목록 보기
2/44

👩‍💻 Achievement Goals 문자열

  • 문자열의 속성과 메소드를 이용해 원하는 형태로 만들 수 있다.

    yes!

  • 문자열의 length라는 속성을 활용해 길이를 확인할 수 있다. str.length
    let str = 'jabae'
    console.log(str.length) // 5
  • 문자열의 글자 하나하나에 접근할 수 있다. str[1]
    let str = 'Happy'
    console.log(str[0]) // H
    console.log(str[4]) // y
  • 문자열을 합칠 수 있다. word1 + " " + word2
    let word1 = 'Hello'
    let word2 = 'world!'
    console.log(word1 + " " + word2) // Hello world!

    template literal: string과 변수를 함께 쓸 때 유용하다!

    console.log(`${word1} ${word2}) // Hello world! 
  • 문자열을 원하는 만큼만 선택할 수 있다. str.slice(0, 3) 또는 str.substring(0, 3)
    let str = 'Happy Day!'
    console.log(str.slice(0, 5)) // Happy
    console.log(str.slice(0, -5)) // Happy
    console.log(str.slice(0, 2)) // Ha
    console.log(str.slice(0, -8)) // Ha
  • 영문을 모두 대문자로 바꿀 수 있다. str.toUpperCase()
    let str = 'i love cat'
    console.log(str.toUpperCase()) // I LOVE CAT
  • 영문을 모두 소문자로 바꿀 수 있다. str.toLowerCase()
    let str = 'I LOVE DOG'
    console.log(str.toLowerCase()) // i love dog
  • 문자열 중 원하는 문자의 index를 찾을 수 있다 str.indexOf('a') 또는 str.lastIndexOf('a')
    let str = 'There is a car'
    console.log(str.indexOf('a')) // 9
    console.log(str.lastIndexOf('a')) // 12
  • 문자열 중 원하는 문자가 포함되어 있는지 알 수 있다. str.includes('a')
    let str1 = 'cat'
    let str2 = 'dog'
    console.log(str1.includes('a')) // true
    console.log(str12.includes('a')) // false

👩‍💻 Achievement Goals 반복문

  • 반복문을 활용하여 단순한 기능을 반복하여 수행할 수 있다.

    yes!

  • 반복문(for문)과 문자열, 숫자를 이용해 반복적으로 코드를 실행시킬 수 있다.

    귀여운 트리/피라미드 만들기

    let j = 1
    let floor = 15 // 높이
    for (let i = floor; i > 0; i--) {
        console.log(' '.repeat(i - 1) + '*'.repeat(j) + '\n')
        j += 2
    }
  • 기본적인 for문 (for (let i = 0; i < 5; i++))을 응용하여 다양한 for문을 만들 수 있다.

    멋있는 다이아몬드 만들기

    for (let i = 0; i < 5; i++) {
        console.log(' '.repeat(4 - i) + '*'.repeat(i*2 + 1) + '\n')
    }
    for (let i = 3; i >= 0; i--) {
        console.log(' '.repeat(4 - i) + '*'.repeat(i*2 + 1) + '\n')
    }
  • for와 while의 차이에 대해서 설명할 수 있다.

    for : 반복을 위한 변수를 안에서 선언하고 초기화한다. (for문 밖 return은 안된다!)

    let factorial = 3
    let result = 1
    for (let i = 1; i <= factorial; i++) {
    	result *= i 
    }
    console.log(result) // 1 * 2 * 3 = 6

    while : 변수를 밖에서 선언하고 초기화해야 한다.

    let factorial = 3
    let result = 1
    let i = 1
    while (i <= factorial) {
    	result *= i 
        i++
    }
    console.log(result) // 1 * 2 * 3 = 6
  • 반복문에 조건문을 적용하여 특정 조건에서만 코드가 실행되도록 할 수 있다.

    문자열 'haha'에서 'a'일 때 'o'로 바꾸기

    let str = 'haha'
    let result = ''
    for (let i = 0; i < str.length; i++) {
        if (str[i] === 'a')
            result += 'o'
        else
            result += str[i]
    }
    console.log(result) // hoho
  • 이중 for문이 무엇인지 이해하고 활용할 수 있다.

    1단부터 9단까지 구구단 만들기

    for (let i = 1; i <= 9; i++) {
        console.log(`----${i}단----`)
        for (let j = 1; j <= 9; j++) {
            console.log(`${i} x ${j} = ${i * j}`)
        }
    }

📚 그 외

  • 문자열에 index로 접근은 가능하지만 값을 바꿀 수 없기에 버퍼 변수를 만들어야 한다.
  • String 메소드는 모두 원본이 변하지 않는다!
  • str.split(seperator) : 문자열 분리
  • Math.floor(num) : 내림 Math.ceil(num) : 올림 Math.round(num) : 반올림
  • boolean으로 반환하는 경우 바로 그냥 써 주는 게 좋다.
  • 줄바꿈은 최소로 한다.

재밌다! 😝

profile
it's me!:)

0개의 댓글