본문 바로가기
혼공학습단/자바스크립트

혼공학습단 9기_2주차 미션

by 노 코딩 노 라이프 2023. 1. 15.

- 2주차 미션

기본과제

 

- 실행화면

- 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <title>2주차 기본 과제</title>
    <script>
        const x = Number(prompt('숫자를 입력해주세요', ' '))
        if(x > 10 && x < 20){
            alert('조건에 맞습니다.')
        }
    </script>
</head>
<body>
    
</body>
</html>

- 코드 풀이 

두 조건이 모두 true 일 때는 &&연산자를 사용해줘야 한다

 

추가 과제

 

- 실행화면

- 코드

<!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>
    <script>
        const year = Number(prompt('태어난 해를 입력해주세요', ' '))
        const e = year % 12 //입력한 해를 12로 나눈 나머지 값 e
        
        let result
        if (e === 0) {result = '원숭이'}
        else if (e === 1) {result = '닭'}
        else if (e === 2) {result = '개'}
        else if (e === 3) {result = '돼지'}
        else if (e === 4) {result = '쥐'}
        else if (e === 5) {result = '소'}
        else if (e === 6) {result = '호랑이'}
        else if (e === 7) {result = '토끼'}
        else if (e === 8) {result = '용'}
        else if (e === 9) {result = '뱀'}
        else if (e === 10) {result = '말'}
        else if (e === 11) {result = '양'}
        alert(`${year}년에 태어났다면 ${result} 띠 입니다`)
    </script>
</head>
<body>
    
</body>
</html>

- 효율적인 코드 만들기

split로 문자열을 잘라 사용하기

+spilt메소드는 배열을 만들어내는 메소드

<!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>
    <script>
        const year = Number(prompt('태어난 해를 입력해주세요', ' '))
        const result = '원숭이, 닭, 개, 돼지, 쥐, 소, 호랑이, 토끼, 용, 뱀, 말, 양'.split(',')

        alert(`${year}년에 태어났다면 ${result[year%12]} 띠 입니다`)
    </script>
</head>
<body>
    
</body>
</html>

 

Chapter 03 필기

https://rei050r.tistory.com/53

 

Chapter 03

03장 03-1 if 조건문 🎈 conditional statement 조건에 따라 코드를 실행하거나 실행하지 않도록 할 때 사용하는 구문. 코드가 실행되는 흐름을 변경하는 것을 조건 분기라고 함 if 조건문 : 괄호 안의 불

rei050r.tistory.com

 

'혼공학습단 > 자바스크립트' 카테고리의 다른 글

혼공학습단 9기_3주차 미션  (0) 2023.01.20
Chapter 04  (0) 2023.01.19
Chapter 03  (0) 2023.01.15
Chapter 02  (2) 2023.01.08
혼공학습단 9기_1주차 미션  (0) 2023.01.07