Silver Library (Archived)
Conditional statement: if-else 본문
hackerrank, 10days of JS.
Title: Day 2: conditional statement if-else
Personal note: This is for the record to see how I figured out the algorithm.
Initially,
function getGrade(score) {
let grade;
is given. Then the question is; how to implement the following condition.
- If (25 < score <= 30), then grade = A.
- If (20 < score <= 25), then grade = B.
- If (15 < score <= 20), then grade = C.
- If (10 < score <= 15), then grade = D.
- If (5 < score <= 10), then grade = E.
- If (0 < score <= 5), then grade = F.
To figure out this issue, I will have to recall the way how I made this from Python.
function getGrade(scroe) {
let grade;
// write your code here
if (score <= 5) {
return grade = "F";
}
Although the condition presented scary, the actual concern of this issue is to focus on 'how let JS to print the result of each designated grade value.
To do so, 'limit the range' is the key to this problem. the condition just human language.
if (score <= 5) // well wrapped with parenthess.
{ return grade ="F"; } // return, and grade will return with value of string "F";
// as I can see, this curly bracket belongs to (score <= 5) .
if (score <= 5) {
return grade = "F";
} else if (score <= 10) {
return grade = "E";
} else if (score = 15) {
return grade = "D"
} else if (score = 20) {
return grade = "C";
} else if (score = 25) {
return grade = "B";
} else {
return "A";
}
The final one should be closed. So, do not use else if. Just finalise it with else. No need to designate any special form. Just return the value I wish to do so.
Yes, this is still JS. Let's see whether console.log can be used as print in Python...
'F2. Problem & Solving > Solving' 카테고리의 다른 글
Loops - JS algorithm (0) | 2021.06.12 |
---|---|
Conditional statements: switch (0) | 2021.06.12 |
[JS] Day 1 : let and const (0) | 2021.06.11 |
Plan of learning the algorithm (0) | 2021.05.01 |
Baekjoon algorithm - 10430 , python (map) (0) | 2021.02.15 |