DEV Community

Cover image for [#2] KoreanCoding 101: operators, if... else, ternary operator
Ha Anna
Ha Anna

Posted on • Originally published at blog.haanna.com

[#2] KoreanCoding 101: operators, if... else, ternary operator

Hello there! Today, we'll explore some advanced concepts related to logic and conditionals in programming. Don't worry if you're not yet confident in these areas - I'll guide you through each step and introduce some Korean technical vocabulary to help you better understand the concepts.

As we dive into these topics, we'll encounter a charming mascot named ๋จธ์“ฑ์ด, who represents Programmers.co.kr. Let's get started!

Operators | ์—ฐ์‚ฐ์ž

We already are familiar with arithmetic operators (addition, subtraction, division, multiplication, remainder) but now it's time to get to know comparison operators.

Comparison operators are used to compare two values and return a boolean value (either true (๊ธ์ •, ์ฐธ) or false (๋ถ€์ •, ๊ฑฐ์ง“)) based on the result of the comparison (๋น„๊ต). If you are not sure how to use them, I recommend going through javascript.info's JavaScript Fundamentals: Comparisons.

if... else statement | if... else ์กฐ๊ฑด๋ฌธ

We use if... else to make decisions in code. It works by checking a condition (์กฐ๊ฑด) (like whether a number is greater than 1), and then performing different actions based on whether the condition is true (๊ธ์ •, ์ฐธ) or false (๋ถ€์ •, ๊ฑฐ์ง“).

let number = 1

if(number === 1) {
  console.log('Oho! The number is 1!') // This is the output.
} else {
  console.log('The number is not 1.')
}
Enter fullscreen mode Exit fullscreen mode

If we were to change number to another number, for example, 3:

let number = 3

if(number === 1) {
  console.log('Oho! The number is 1!')
} else {
  console.log('The number is not 1.') // This is the output.
}
Enter fullscreen mode Exit fullscreen mode

Then the console would log out the 'Oh! The number is not 1.' from the else statement.

We can also add multiple conditions (์กฐ๊ฑด) by adding else if (condition) between if and else elements:

let number = 3

if(number === 1) {
  console.log('Oho! Number is 1!')
} else if (number > 1) {
  console.log('Not 1 but bigger than 1.') // This is the output.
} else {
  console.log('Hmm... Number is not 1.')
}
Enter fullscreen mode Exit fullscreen mode

And it is also worth mentioning that else is optional and we can have standalone if statements (if ์กฐ๊ฑด๋ฌธ) or if... if else statements (if... if else ์กฐ๊ฑด๋ฌธ) too:

let number = 1

if(number === 1) {
  console.log('Oho! Number is 1!') // This is the output.
} 

if(number > 0) {
  console.log('Number is bigger than 0!') // This is also the output.
}
Enter fullscreen mode Exit fullscreen mode

Here, both statements evaluate true and get logged out because they are not combined into one statement.

Ternary operator | ์‚ผํ•ญ ์—ฐ์‚ฐ์ž

Now that you are a bit more comfortable with conditional statements, let's take a look at something even cooler - the ternary operator.

The ternary operator, also known as the conditional operator, is a shorthand way of writing a if...else statement. It has the following syntax:

condition ? expression1 : expression2
Enter fullscreen mode Exit fullscreen mode

The condition is evaluated first. If it is true, expression1 is executed. If it is false, expression2 is executed. Here's a real-life example:

let age = 22
let message = age >= 18 ? "You are an adult" : "You are not an adult";
console.log(message) // Output: "You are an adult"
Enter fullscreen mode Exit fullscreen mode

If age is greater than or equal to 18, the value of the message variable will be "You are an adult", and if it is less than 18, the value of the message will be "You are not an adult". The value of the message variable is determined by the result of the ternary operator.

Ternary operators can also be chained together. This technique is used to simplify nested if...else statements or switch cases. It allows you to write a single line of code that performs multiple conditional checks and returns different values based on those conditions.

condition1 ? value1 : condition2 ? value2 : condition3 ? value3 : defaultValue
Enter fullscreen mode Exit fullscreen mode
const temperature = 20;
const isRaining = false;
const message = temperature > 30 ? "It's hot outside" :
                isRaining ? "It's raining outside" :
                "It's a nice day outside";

console.log(message); // Output: It's a nice day outside
Enter fullscreen mode Exit fullscreen mode

The ternary operator can make code more concise and readable in some cases, but it can also make code harder to read if it is overused. I'll show you an example later when solving one of the challenges. When it comes to the ternary operator, it is best to use it only when it improves the clarity of the code.

Vocabulary

Nouns:

๋ณ€์ˆ˜ - variable

๋ฌธ์ž์—ด - string

์ˆซ์ž - number

๋ถˆ๋ฆฐ - boolean

๋ฐฐ์ˆ˜ - multiple (a number that can be divided by another number without a remainder)

์ง์ˆ˜ - even (number)

ํ™€์ˆ˜ - odd (number)

if... else ์กฐ๊ฑด๋ฌธ - if... else conditional statement

์‚ผํ•ญ ์—ฐ์‚ฐ์ž - ternary operator

์กฐ๊ฑด ์—ฐ์‚ฐ์ž - conditional operator

๊ธ์ •, ์ฐธ - true

๋ถ€์ •, ๊ฑฐ์ง“ - false

๊ฑฐ์ง“๊ฐ™์€ ๊ฐ’ - falsy

์ฐธ ๊ฐ™์€ ๊ฐ’ - truthy

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž - logical operator (&&, ||, !)

๋…ผ๋ฆฌ๊ณฑ ์—ฐ์‚ฐ์ž - AND operator (&&)

๋…ผ๋ฆฌํ•ฉ ์—ฐ์‚ฐ์ž - OR operator (||)

๋…ผ๋ฆฌ๋ถ€์ • ์—ฐ์‚ฐ์ž - NOT operator (!)

๊ด€๊ณ„์—ฐ ์‚ฐ์ž - relational operator (==, ===, !=, !==, <, >, <=, >=)

๋™๋“ฑ ์—ฐ์‚ฐ์ž - equality operator (==)

์ผ์น˜ ์—ฐ์‚ฐ์ž - strict equality operator (===)

๋ถ€๋“ฑ ์—ฐ์‚ฐ์ž - inequality operator (!=)

๋ถˆ์ผ์น˜ ์—ฐ์‚ฐ์ž - strict inequality operator (!==)

์ž‘์Œ - less than (<)

ํผ - greater than (>)

ํฌ๊ฑฐ๋‚˜ ๊ฐ™์Œ - greater or equal to (>=)

์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ - less or equal to (<=)

for (๋ฐ˜๋ณต)๋ฌธ - for loop

์ฝ”๋“œ ๋ธ”๋ก - code block

ํ˜• ๋ณ€ํ™˜ - type conversion

์˜ˆ๊ฐ - acute angle

์ง๊ฐ - right angle

๋‘”๊ฐ - obtuse angle

ํ‰๊ฐ - straight angle

Verbs:

์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•˜๋‹ค - to evaluate the condition

๋งŒ์กฑํ•˜๋‹ค - to be satisfied (a condition)

์‹คํ–‰ํ•˜๋‹ค - to run (a statement)

๋ฐ˜ํ™˜ํ•˜๋‹ค - to return (a value)

๋น„๊ตํ•˜๋‹ค - to compare

๋ฐ˜๋ณตํ•˜๋‹ค - to repeat

Phrases:

condition1์„ ๋งŒ์กฑํ•˜๋ฉด statement1์„ ์‹คํ–‰ํ•˜๋‹ค.

if condition1 is satisfied run statement1.

Challenges

n์˜ ๋ฐฐ์ˆ˜

Description

์ˆ˜ num๊ณผ n์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, num์ด n์˜ ๋ฐฐ์ˆ˜์ด๋ฉด 1์„ return n์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 0์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 2 โ‰ค num โ‰ค 100

  • 2 โ‰ค n โ‰ค 9

์ž…์ถœ๋ ฅ ์˜ˆ

num n result
98 2 1
34 3 0

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • 98์€ 2์˜ ๋ฐฐ์ˆ˜์ด๋ฏ€๋กœ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • 32๋Š” 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ 0์„ returnํ•ฉ๋‹ˆ๋‹ค.

Description:

n์˜ ๋ฐฐ์ˆ˜

the multiple of n

์ˆ˜ num๊ณผ n์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ

Integers num and n are given as parameters,

num์ด n์˜ ๋ฐฐ์ˆ˜์ด๋ฉด 1์„ return

return 1 if num is a multiple of n

n์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 0์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

and return 0 if it is not to complete the solution function.

์ œํ•œ์‚ฌํ•ญ (constraints):

The constraints section of the challenge informs us that num can have values ranging from 2 to 100, and that n can have values ranging from 2 to 9.

์ž…์ถœ๋ ฅ ์˜ˆ (input-output examples):

#1: When num = 98 and n = 1 the result should be 1.

#2: When num = 32 and n = 3 the result should be 0.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… (explanation of input-output examples):

Based on the example #1:

98์€ 2์˜ ๋ฐฐ์ˆ˜์ด๋ฏ€๋กœ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

It returns 1 because 98 is a multiple of 2.

Based on the example #2:

32๋Š” 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ 0์„ returnํ•ฉ๋‹ˆ๋‹ค.

It returns 0 because 32 is not a multiple of 3.

JavaScript Solution

If we want to determine whether one number can be divided evenly by another, we have a few options in JavaScript. One popular way is to use an if...else statement (if...else ์กฐ๊ฑด๋ฌธ), which is a great fit for this problem.

To check whether num is divisible by n, we use the modulo operator % to see if there is a remainder. If there is no remainder, we know that num is divisible by n and we return 1. If there is a remainder, we return 0.

Another approach is to use a ternary operator (์‚ผํ•ญ ์—ฐ์‚ฐ์ž), which is a shorthand way to write the same code. This approach is shorter and more concise, which some developers might prefer.

// if... else solution
function solution(num, n) {
  if (num % n === 0) {
    return 1
  } else {
    return 0
  }
}

// ternary operator solution
function solution(num, n) {
  return (num % n === 0) ? 1 : 0
}
Enter fullscreen mode Exit fullscreen mode

๊ฐ๋„๊ธฐ

Description

๊ฐ์—์„œ 0๋„ ์ดˆ๊ณผ 90๋„ ๋ฏธ๋งŒ์€ ์˜ˆ๊ฐ, 90๋„๋Š” ์ง๊ฐ, 90๋„ ์ดˆ๊ณผ 180๋„ ๋ฏธ๋งŒ์€ ๋‘”๊ฐ 180๋„๋Š” ํ‰๊ฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ angle์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์˜ˆ๊ฐ์ผ ๋•Œ 1, ์ง๊ฐ์ผ ๋•Œ 2, ๋‘”๊ฐ์ผ ๋•Œ 3, ํ‰๊ฐ์ผ ๋•Œ 4๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

  • ์˜ˆ๊ฐ : 0 < angle < 90

  • ์ง๊ฐ : angle = 90

  • ๋‘”๊ฐ : 90 < angle < 180

  • ํ‰๊ฐ : angle = 180

์ œํ•œ์‚ฌํ•ญ

  • 0 < angle โ‰ค 180

  • angle์€ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

angle result
70 1
91 3
180 4

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • angle์ด 70์ด๋ฏ€๋กœ ์˜ˆ๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • angle์ด 91์ด๋ฏ€๋กœ ๋‘”๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 3์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • angle์ด 180์ด๋ฏ€๋กœ ํ‰๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

Description:

๊ฐ๋„๊ธฐ

Protractor

๊ฐ์—์„œ 0๋„ ์ดˆ๊ณผ 90๋„ ๋ฏธ๋งŒ์€ ์˜ˆ๊ฐ,

An angle greater than 0 degrees and less than 90 degrees is an acute angle,

90๋„๋Š” ์ง๊ฐ,

90 degrees is a right angle,

90๋„ ์ดˆ๊ณผ 180๋„ ๋ฏธ๋งŒ์€ ๋‘”๊ฐ

greater than 90 degrees and less than 180 degrees is an obtuse angle,

180๋„๋Š” ํ‰๊ฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.

and an angle equal to 180 degrees is a straight angle.

๊ฐ angle์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ

When angle is given as a parameter, return

์˜ˆ๊ฐ์ผ ๋•Œ 1,

1 for an acute angle,

์ง๊ฐ์ผ ๋•Œ 2,

2 for a right angle,

๋‘”๊ฐ์ผ ๋•Œ 3,

3 for an obtuse angle,

ํ‰๊ฐ์ผ ๋•Œ 4๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

and 4 for a straight angle to complete the solution function.

์˜ˆ๊ฐ : 0 < angle < 90

acute angle: 0 < angle < 90

์ง๊ฐ : angle = 90

right angle: angle = 90

๋‘”๊ฐ: 90 < angle < 180

obtuse angle: 90 < angle < 180

ํ‰๊ฐ : angle = 180

straight angle: angle = 180

์ œํ•œ์‚ฌํ•ญ (constraints):

The constraints section of the challenge informs us that angle is an integer (์ •์ˆ˜) and it can have values ranging from 0 to 180.

์ž…์ถœ๋ ฅ ์˜ˆ (input-output examples):

#1: When angle = 70 the result should be 1.

#2: When angle = 91 the result should be 3.

#3: When angle = 180 the result should be 4.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… (explanation of input-output examples):

Based on the example #1:

angle์ด 70์ด๋ฏ€๋กœ ์˜ˆ๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

Since angle is 70, it is an acute angle. So it returns 1.

Based on the example #2:

angle์ด 91์ด๋ฏ€๋กœ ๋‘”๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 3์„ returnํ•ฉ๋‹ˆ๋‹ค.

Since angle is 91, it is an obtuse angle. So it returns 3.

Based on the example #3:

angle์ด 180์ด๋ฏ€๋กœ ํ‰๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

Since angle is 180, it is a straight angle. So it returns 4.

JavaScript Solution

Here we will need two more else if statements added to our if... else statement. We also need to make sure we use relational operators (๊ด€๊ณ„์—ฐ ์‚ฐ์ž) correctly.

This can be also solved in two ways, more or less descriptive. The edge cases will also be handled differently.

The first solution provides more specific information on how the function is handling different angles within the range of 0 to 180. It breaks down the range into four distinct cases, including angles between 0 and 90, 90, angles between 90 and 180, and 180. This approach allows for more precise identification of the angle's location and how it's being handled, which can make debugging and maintenance easier in the long run.

The second solution is more concise and combines the second and fourth cases, resulting in only three cases. While this approach is still functional, it is slightly less descriptive because it doesn't provide as much information about how the function is handling different angles. As a result, it may be less clear to other developers or maintainers who are reading or working with the code.

Overall, the choice between these solutions comes down to a balance between the need for precise information and the desire for simplicity and conciseness. In general, it's best to strive for a balance between these two factors, creating code that is both readable and efficient.

// #1: more descriptive solution
function solution(angle) {
    if(0 < angle && angle < 90) return 1
    else if (angle === 90) return 2
    else if (90 < angle && angle < 180) return 3
    else if (angle === 180) return 4
    else return
}

// #2: a slightly less descriptive solution
function solution(angle) {
    if(0 < angle && angle < 90) return 1
    else if (angle === 90) return 2
    else if (90 < angle && angle < 180) return 3
    else return 4
}
Enter fullscreen mode Exit fullscreen mode

์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ

Description

์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‘ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด 1 ๋‹ค๋ฅด๋ฉด -1์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 0 โ‰ค num1 โ‰ค 10,000

  • 0 โ‰ค num2 โ‰ค 10,000

์ž…์ถœ๋ ฅ ์˜ˆ

num1 num2 result
2 3 -1
11 11 1
7 99 -1

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… #1

  • num1์ด 2์ด๊ณ  num2๊ฐ€ 3์ด๋ฏ€๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ -1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… #2

  • num1์ด 11์ด๊ณ  num2๊ฐ€ 11์ด๋ฏ€๋กœ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… #3

  • num1์ด 7์ด๊ณ  num2๊ฐ€ 99์ด๋ฏ€๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ -1์„ returnํ•ฉ๋‹ˆ๋‹ค.

Description:

์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ

Comparing numbers

์ •์ˆ˜ num1๊ณผ num2๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

Integers num1 and num2 are given as parameters.

๋‘ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด 1 ๋‹ค๋ฅด๋ฉด -1์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

Complete the solution function to return 1 if the two numbers are the same and -1 if they are different.

์ œํ•œ์‚ฌํ•ญ (constraints):

The constraints section of the challenge informs us that both num1 and num2 can have values ranging from 0 to 10,000.

์ž…์ถœ๋ ฅ ์˜ˆ (input-output examples):

#1: When num1 = 2 and num2 = 3 the result should be -1.

#2: When num1 = 11 and num2 = 11 the result should be 1.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… (explanation of input-output examples):

Based on the example #1:

num1์ด 2์ด๊ณ  num2๊ฐ€ 3์ด๋ฏ€๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ -1์„ returnํ•ฉ๋‹ˆ๋‹ค.

Return -1 because num1 is 2 and num2 is 3, which means they are not equal.

Based on the example #2:

num1์ด 11์ด๊ณ  num2๊ฐ€ 11์ด๋ฏ€๋กœ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

Return 1 because num1 is 11 and num2 is 11, which means they are equal.

JavaScript Solution

When it comes to comparing two numbers in JavaScript, we have a couple of options. We can use an if...else statement (if...else ์กฐ๊ฑด๋ฌธ) or a ternary operator (์‚ผํ•ญ ์—ฐ์‚ฐ์ž). Both of these methods will work just fine.

One important thing to keep in mind is that we want to use a strict equality operator === (์ผ์น˜ ์—ฐ์‚ฐ์ž) to compare the numbers. This ensures that we are comparing the numbers' values and types, so we don't run into any unexpected issues.

// if... else solution
function solution(num1, num2) {
    if (num1 === num2) {
        return 1
    } else {
        return -1
    }
}

// ternary operator solution
function solution(num1, num2) {
    return (num1 === num2) ? 1 : -1
}
Enter fullscreen mode Exit fullscreen mode

์˜ท๊ฐ€๊ฒŒ ํ• ์ธ ๋ฐ›๊ธฐ

Description

๋จธ์“ฑ์ด๋„ค ์˜ท๊ฐ€๊ฒŒ๋Š” 10๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 5%, 30๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 10%, 50๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 20%๋ฅผ ํ• ์ธํ•ด์ค๋‹ˆ๋‹ค.

๊ตฌ๋งคํ•œ ์˜ท์˜ ๊ฐ€๊ฒฉย price๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ง€๋ถˆํ•ด์•ผ ํ•  ๊ธˆ์•ก์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 10 โ‰ค price โ‰ค 1,000,000

    • price๋Š” 10์› ๋‹จ์œ„๋กœ(1์˜ ์ž๋ฆฌ๊ฐ€ 0) ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฐ ์ •์ˆ˜๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

price result
150,000 142,500
580,000 464,000

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • 150,000์›์—์„œ 5%๋ฅผ ํ• ์ธํ•œ 142,500์›์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • 580,000์›์—์„œ 20%๋ฅผ ํ• ์ธํ•œ 464,000์›์„ return ํ•ฉ๋‹ˆ๋‹ค.

Description:

์˜ท๊ฐ€๊ฒŒ ํ• ์ธ ๋ฐ›๊ธฐ

Clothing store discount

๋จธ์“ฑ์ด๋„ค ์˜ท๊ฐ€๊ฒŒ๋Š” 10๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 5%,

If ๋จธ์“ฑ์ด spends more than 100,000 won in the clothing store, a 5% discount is given.

30๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 10%,

A 10% discount is given above 300,000 won,

50๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 20%๋ฅผ ํ• ์ธํ•ด์ค๋‹ˆ๋‹ค.

and a 20% discount is given above 500,000 won.

๊ตฌ๋งคํ•œ ์˜ท์˜ ๊ฐ€๊ฒฉย price๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,

Given the price,

์ง€๋ถˆํ•ด์•ผ ํ•  ๊ธˆ์•ก์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

return the discounted price to complete the solution function.

์ œํ•œ์‚ฌํ•ญ (constraints):

The constraints section of the challenge informs us that price can have values ranging from 10 to 1,000,000.

price๋Š” 10์› ๋‹จ์œ„๋กœ(1์˜ ์ž๋ฆฌ๊ฐ€ 0) ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

price is given in increments of 10 won (starting from 0).

์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฐ ์ •์ˆ˜๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

Returns an integer without the decimal point. (10000 not 10,000)

์ž…์ถœ๋ ฅ ์˜ˆ (input-output examples):

#1: When price = 150,000 the result should be 142,500.

#2: When price = 580,000 the result should be 464,000.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… (explanation of input-output examples):

Based on the example #1:

150,000์›์—์„œ 5%๋ฅผ ํ• ์ธํ•œ 142,500์›์„ return ํ•ฉ๋‹ˆ๋‹ค.

Returns 142,500 won, which is a 5% discount from 150,000 won.

Based on the example #2:

580,000์›์—์„œ 20%๋ฅผ ํ• ์ธํ•œ 464,000์›์„ return ํ•ฉ๋‹ˆ๋‹ค.

Returns 464,000 won, a 20% discount from 580,000 won.

JavaScript Solution

With this example, let's talk about when to use ternary operators (์‚ผํ•ญ ์—ฐ์‚ฐ์ž) versus if...else statements (if...else ์กฐ๊ฑด๋ฌธ).

In this example, we apply discounts based on the original price in three different ranges. If the price falls within a certain range, a discount percentage is applied to it, and we get a new discounted price:

  • If price is between 100,000 and 299,999, the discount applied is 5% and the new discounted price is calculated by multiplying price by 0.95.

  • If price is between 300,000 and 499,999, the discount applied is 10% and the new discounted price is calculated by multiplying price by 0.9.

  • If price is greater than or equal to 500,000, the discount applied is 20% and the new discounted price is calculated by multiplying price by 0.8.

  • If price is less than 100,000, no discount is applied and the original price is returned.

At the end of the function, we also have to use the Math.floor() method to round down the discounted price to the nearest whole number.

We can write this function using an if...else statement (if...else ์กฐ๊ฑด๋ฌธ), which is easy to read and understand. However, if we try to use a ternary operator (์‚ผํ•ญ ์—ฐ์‚ฐ์ž) to write this function, we will need to chain multiple ternary operators together, resulting in a long and hard-to-read code (Have you ever heard of spaghetti code?).

// if... else statement solution
function solution(price) {
  let discount = 0

  if (price >= 100000 && price < 300000) {
    discount = price * 0.95
  } else if (price >= 300000 && price < 500000) {
    discount = price * 0.9
  } else if (price >= 500000) {
    discount = price * 0.8
  } else {
    discount = price
  }

  return Math.floor(discount)
}

// ternary operator solution
function solution(price) {
  let discount = (price >= 100000 && price < 300000) ? price * 0.95 : (price >= 300000 && price < 500000) ? price * 0.9 : (price >= 500000) ? price * 0.8 : price
  return Math.floor(discount)
}
Enter fullscreen mode Exit fullscreen mode

Wrap-up

Congratulations, you did it! ๋จธ์“ฑ์ด and I are both incredibly proud of you!

By learning the important vocabulary related to else...if statements, logical operators, and ternary operators, you've taken another step forward towards fluency in technical Korean.

And if you want to support this series and the work I put into it, you can do it by buying me a coffee here:

%%[bmc]

Thanks!

I hope you have an amazing weekend, and I look forward to seeing you next week!

Top comments (0)