Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- visibility : hidden
- 배열의 내림차순
- map()
- filter()
- disabled
- for..of
- 일반 형제 선택자 결합
- 쌍방향 연결리스트
- 객체
- Sort
- 범용 선택자
- invalid assignment left-hand side
- Em
- innerhtml
- 배열의 오름차순
- 등차수열의 항 찾기
- CSS
- 양방향 연결리스트
- 백준알고리즘
- 가상 요소 선택자
- 배열과 연결리스트의 차이
- indexOf
- 고차함수
- 단방향 연결리스트
- 인접 형제 선택자 결합
- Array.from()
- display : none
- classList.contains(string)
- Link
- nth-child()
Archives
- Today
- Total
프론트엔드 센트럴파크 (☞゚ヮ゚)☞
조건문(if, else)를 이용한 ATM 인출 본문
ATM기기는 5만원권만 인출 가능하다.
인출 시 0.5의 수수료가 필요하다.
단, 정상적인 인출이 불가능할 경우 돈은 출금되지 않고, 통장의 원래 금액을 반환한다.
정답1
function answer(withdraw, total) {
let result;
if(withdraw == total) {
result = total;
} else if(withdraw%5 == 0) {
result = (total - withdraw) -0.5;
} else if(withdraw%5 != 0) {
result = total;
}
return result;
};
let input = [[40,130.0],[33,130.0],[300,300.0]];
for(let i = 0; i < input.length; i++) {
console.log(`#${i + 1} ${answer(input[i][0], input[i][1])}`);
}
처음 if문을 맨 마지막에 넣었더니 [300, 300.0]테스트 케이스가 5의 배수이기 때문에 출력이 -0.5가 나왔다.
이걸 막기위해 제일 처음 조건문 부터 withdraw와 total의 값이 같으면 total 값을 출력하게 만들었더니 올바르게 출력되었다.
정답2
function answer(withdraw, total) {
let result;
if(withdraw % 5 != 0) {
result = total;
} else if (withdraw + 0.5 > total) {
result = total;
} else {
result = total - withdraw - 0.5;
}
return result;
};
let input = [[40,130.0],[33,130.0],[300,300.0]];
for(let i = 0; i < input.length; i++) {
console.log(`#${i + 1} ${answer(input[i][0], input[i][1])}`);
}
먼저 if문을 통해 [33, 130.0] 테스트 케이스의 결과를 도출하고
withdraw + 0.5 의 값이 total 보다 크면 total 값을 출력하게 하였다([300, 300.0] 테스트 케이스)
'Algorism' 카테고리의 다른 글
제곱값 구하기 (0) | 2022.07.09 |
---|---|
최솟값 구하기 (0) | 2022.07.09 |
조건문(if, else)를 이용한 윤년 판별기 (0) | 2022.07.04 |
조건문(if,else if)를 이용한 나누기와 대소비교 (0) | 2022.07.04 |
조건문(if, else if)을 이용한 대소비교 (0) | 2022.07.04 |
Comments