[#4] KoreanCoding 101: strings and string methods

[#4] KoreanCoding 101: strings and string methods

한국어로 배우는 코딩 101: 문자열과 문자열 메소드

·

9 min read

Hi, hello, and welcome!

Today we are shifting our focus towards strings and their methods! I've always found string manipulation challenges to be really enjoyable, especially in JavaScript where we have a ton of awesome string methods at our disposal.

Let's get into it!

Vocabulary

Nouns:
문자열 - string
정수 - integer
메소드 - method
길이 - length
알파벳 - alphabet
영대문자 - uppercase (English characters)
영소문자 - lowercase (English characters)
영문자 - English alphabet letter
대소문자 - uppercase and lowercase letters
특수문자 - special character
대소문자 구분 - case sensitivity
공백 문자 - whitespace
숫자 - digit, number
슬라이스 - slice
대체 - replace
추가 - append/insert
단항 더하기 연산자 - unary plus operator

Verbs:
행을 바꾸다 - to start a new line/paragraph
정수로 변환하다 - to convert to an integer
작성하다 - to write
곱하다 - to multiply
구분하다 - to divide, sort, classify
제거하다 - to remove

Phrases:

k번 반복한 문자열
string repeated k times

my_string에서 letter를 제거한 문자열
string with letter removed from my_string

대문자와 소문자를 구별하다
to distinguish between uppercase and lowercase characters

my_string의 앞의 n글자로 이루어진 문자열
a string consisting of the first n characters of my_string

Challenges

문자열 곱하기

Description

문자열 my_string정수 k가 주어질 때, my_stringk번 반복한 문자열을 return 하는 solution 함수를 작성해 주세요.

제한사항

  • 1 ≤ my_string의 길이 ≤ 100

  • my_string영소문자로만 이루어져 있습니다.

  • 1 ≤ k ≤ 100

입출력 예

my_stringkresult
"string"3"stringstringstring"
"love"10"lovelovelovelovelovelovelovelovelovelove"

입출력 예 설명

입출력 예 #1

  • 예제 1번의 my_string은 "string"이고 이를 3번 반복한 문자열은 "stringstringstring"이므로 이를 return 합니다.

입출력 예 #2

  • 예제 2번의 my_string은 "love"이고 이를 10번 반복한 문자열은 "lovelovelovelovelovelovelovelovelovelove"이므로 이를 return 합니다.

Description:

문자열 곱하기
Multiply the string

문자열 my_string과 정수 k가 주어질 때,
When given a string my_string and an integer k,

my_string을 k번 반복한 문자열을 return 하는 solution 함수를 작성해 주세요.
complete the solution function that returns a string obtained by repeating my_string k times.

제한사항 (constraints):

  • The length of my_string should be between 1 and 100 (inclusive).

  • my_string is composed only of lowercase English alphabet letters.

  • The value of k should be between 1 and 100 (inclusive).

입출력 예 (input-output examples):

#1: When my_string = 'string' and k = 3 return "stringstringstring".

입출력 예 설명 (explanation of input-output examples):

Based on the example #1:

Since my_string is “string” and the string repeated 3 times is “stringstringstring”, which should be returned.

JavaScript Solution

The first solution takes advantage of the built-in repeat() method (메소드). This approach is very easy, we simply chain repeat() to my_string and give it an argument of k. And as a result, we are given a concatenated my_string.

The second solution introduced a for loop (for문), which iterates k times. During each iteration, it appends my_string to the variable str. After the loop completes, it returns the concatenated string str.

// by using JS string method repeat()
function solution(my_string, k) {
    return my_string.repeat(k)
}

// by using a for loop
function solution(my_string, k) {
  let str = ''
  for(let i = 0; i < k; i++) {
    str += my_string
  }
  return str
}

특정 문자 제거하기

Description

문자열 my_string과 문자 letter이 매개변수로 주어집니다. my_string에서 letter를 제거한 문자열을 return하도록 solution 함수를 완성해주세요.

제한사항

  • 1 ≤ my_string의 길이 ≤ 100

  • letter은 길이가 1인 영문자입니다.

  • my_stringletter알파벳 대소문자로 이루어져 있습니다.

  • 대문자소문자를 구분합니다.

입출력 예

my_stringletterresult
"abcdef""f""abcde"
"BCBdbe""B""Cdbe"

입출력 예 설명

입출력 예 #1

  • "abcdef" 에서 "f"를 제거한 "abcde"를 return합니다.

입출력 예 #2

  • "BCBdbe" 에서 "B"를 모두 제거한 "Cdbe"를 return합니다.

Description:

특정 문자 제거하기
Remove specific characters

문자열 my_string과 문자 letter이 매개변수로 주어집니다.
The my_string and letter are given as parameters.

my_string에서 letter를 제거한 문자열을 return하도록 solution 함수를 완성해주세요.
Complete the solution function to return a string with all letter removed from my_string .

제한사항 (constraints):

  • The length of my_string should be between 1 and 100 (inclusive).

  • The letter variable represents a single English alphabet letter.

  • my_string and letter are composed of both uppercase and lowercase English alphabet letters.

  • Uppercase and lowercase letters are treated as distinct and should be differentiated.

입출력 예 (input-output examples):

#1: When my_string = "abcdef" and letter = "f", return should be "abcde".

#2: When my_string = "BCBdbe" and letter = "B", return should be "Cdbe".

입출력 예 설명 (explanation of input-output examples):

Based on the example #1:

"f" is removed from "abcdef" and "abcde" is returned.

Based on the example #2:

"B" is removed from "BCBdbe" and "Cdbe" is returned.

JavaScript Solution

Based on the challenge description and given examples, we aim to remove all occurrences of the letter from my_string. The most concise way of dealing with that is to use replaceAll() method (메소드). This method replaces all occurrences of a specified character (or a substring, it can be more than one character). Moreover, it is also case-sensitive, which means it will replace only the exact match. Therefore even if we have both lowercase (소문자) and uppercase (대문자) letters in our string, only the matching one will be replaced.

Since we want the letter gone from our string, we can simply replace it with an empty string. After that is done, the function then returns the modified string.

function solution(my_string, letter) {
    return my_string.replaceAll(letter , "") 
}

문자열을 정수로 변환하기

Description

숫자로만 이루어진 문자열 n_str이 주어질 때, n_str정수로 변환하여 return하도록 solution 함수를 완성해주세요.

제한사항

  • 1 ≤ n_str ≤ 5

  • n_str은 0부터 9까지의 정수 문자로만 이루어져 있습니다.

입출력 예

n_strresult
"10"10
"8542"8542

입출력 예 설명

입출력 예 #1

  • "10"을 정수로 바꾸면 10입니다.

입출력 예 #2

  • "8542"를 정수로 바꾸면 8542입니다.

Description:

문자열을 정수로 변환하기
Convert a string to an integer

숫자로만 이루어진 문자열 n_str이 주어질 때,
Given a string n_str consisting only of numbers

n_str을 정수로 변환하여 return하도록 solution 함수를 완성해주세요.
complete the solution function to convert n_str to an integer and return it.

제한사항 (constraints):

  • n_str's length should be between 1 and 5 (inclusive).

  • n_str is composed only of digits from 0 to 9 represented as integer characters.

입출력 예 (input-output examples):

#1: When n_str = "10, return 10.

입출력 예 설명 (explanation of input-output examples):

Based on the example #1:

Replace "10" with an integer 10.

JavaScript Solution

For this challenge, there are multiple solutions available, but one of the most popular and concise approaches involves using the unary plus operator. This operator converts a string representation of a number to its numeric value. By applying the unary plus operator, we can achieve a shorter and more concise solution.

Another solution uses parseInt(), which parses a string and returns an integer based on the provided radix (base). In this case, the radix is set to 10 to indicate decimal (base 10) representation. This method is actually much more flexible and can do much more than just change a string into a number.

You can read more about various ways of changing a string into a number in FreeCodeCamp's article: How to Convert a String to a Number in JavaScript.

// solution with a unary plus operator
function solution(n_str) {
    return +n_str
}

// solution with parseInt()
function solution(n_str) {
    return parseInt(n_str, 10)
}

문자열의 앞의 n글자

Description

문자열 my_string정수 n이 매개변수로 주어질 때, my_string의 앞의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요.

제한사항

  • my_string은 숫자와 알파벳으로 이루어져 있습니다.

  • 1 ≤ my_string의 길이 ≤ 1,000

  • 1 ≤ nmy_string의 길이

입출력 예

my_stringnresult
"ProgrammerS123"11"ProgrammerS"
"He110W0r1d"5"He110"

입출력 예

입출력 예 #1

  • 예제 1번의 my_string에서 앞의 11글자는 "ProgrammerS"이므로 이 문자열을 return 합니다.

입출력 예 #2

  • 예제 2번의 my_string에서 앞의 5글자는 "He110"이므로 이 문자열을 return 합니다.

Description:

문자열의 앞의 n글자
The first n characters of a string

문자열 my_string과 정수 n이 매개변수로 주어질 때,
When given a string my_string and an integer n as parameters,

my_string의 앞의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요.
complete the solution function so it returns a string consisting of the first n characters of my_string.

제한사항 (constraints):

  • The string my_string is composed of numbers and alphabetic characters.

  • The length of my_string should be between 1 and 1,000 (inclusive).

  • The value of n should be between 1 and the length of my_string (inclusive).

입출력 예 (input-output examples):

#1: When my_string = "ProgrammerS123" and n = 11, return "ProgrammerS".

입출력 예 설명 (explanation of input-output examples):

Based on the example #1:

In my_string, the first 11 letters are "ProgrammerS", so that's what is returned.

JavaScript Solution

To solve this challenge, we can use another JavaScript method: slice(), which extracts a portion of a string without modifying the original, and returns it as a new string. The slice() method takes two arguments: the starting index and the ending index (exclusive). In this case, 0 is the starting index and n is the ending index minus one (n-1).

function solution(my_string, n) {
    return my_string.slice(0, n)
}

Wrap-up

And that's it for today! I hope today's lesson was fun and helped you better grasp vocabulary related to strings and their methods.

For those who are keen on trying to solve some similar challenges on their own, I have picked some additional challenges related to strings:

My plan is to keep solving those simple challenges for a while until the vocabulary sticks and move on to more complicated ones once we build a solid foundation of the basics. I'll be also using this opportunity to polish my JS skills as well as my explanation skills.

You can support this series and the work I put into it by buying me a coffee here:

See you in a week!