SMALL
- 2.17 화살표 함수 기본
- 2.18 기본 문법 요약
- 3.1 Chrome 디버깅하기
- 3.2 코딩스타일
2.17 화살표 함수로 변경하기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>화살표 함수로 변경하기/title>
</head>
<body>
<script>
function ask(question, yes, no) {
if (confirm(question)) yes()
else no();
}
ask("동의하십니까?",
() => alert("동의하셨습니다."),
() => alert("취소 버튼을 누르셨습니다.")
);
</script>
</body>
</html>
3.2 좋지 않은 코드 스타일
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>좋지 않은 코드 스타일</title>
</head>
<body>
<script>
// 더 나은 코드로 바꾼 것!
function pow(x, n) {
let result = 1;
for (let i=0; i<n; i++) {
result *= x;
}
return result;
}
let x = prompt("x?", '');
let n = prompt("n?", '');
if (n <= 0) {
lert(`Power ${n} is not supported,
please enter an integer number greater than zero`);
} else {
alert( pow(x, n) )
}
</script>
</body>
</html>
728x90
'JavaScript' 카테고리의 다른 글
[모던 JavaScript] 4.1 ~ 4.4 과제 정리 (0) | 2022.11.22 |
---|---|
[모던 JavaScript] 3.3 ~ 3.6 과제 정리 (0) | 2022.11.20 |
[모던 JavaScript] 2.12 ~ 2.16 과제 정리 (0) | 2022.11.17 |
[모던 JavaScript] 2.7~2.11 과제 정리 (0) | 2022.11.16 |
[모던 JavaScript] 1.1 ~ 2.6 과제 정리! (0) | 2022.11.15 |
댓글