WEB/JS

[JS] 5-2. 과제 - 타입 및 변수

개발자 만두 2021. 9. 19. 07:56
728x90
반응형

✅오늘의 과제

➰console.log()를 이용하여 객체 데이터 출력하기

 

➰코드

day5.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>day5</title>
</head>
<body>
    <button type="button" onclick="typeIs()">입력한 값의 타입을 알아보자 !</button>
    <script src="./day5.js"></script>
</body>
</html>

 

 

day5.js

function typeIs() {
    let s = prompt();
    if (s === "true" || s === "false") {
        console.log("boolean");
    } else if (s === "") {
        console.log("null");
    } else if ((s.indexOf('{') != -1) && (s.indexOf('}') != -1) && (s.indexOf(':') != -1)) {
        console.log("object");
    } else if (isNaN(s)) {
        console.log("string");
    } else if (!isNaN(s)){
        console.log("number");
    } else {
        console.log("undefined");
    }
}

 

 

코드 설명

button을 클릭하면 typeIs() 함수를 실행하게 했다. 이 함수는 prompt를 띄워 사용자의 입력을 받고 그 값을 변수 s에 저장한다. 사용자가 입력한 값의 타입을 알아볼 수 있는 if-else문을 구현했다.

 

 

💨현재 코드를 실행하면 undefined 값이 console에 출력되는 경우가 존재하지 않으므로 이는 추후에 수정할 계획이다.

 

 

➰코드실행화면(https://jsday5.netlify.app)

 

 

➰GitHub

https://github.com/minseon6371/javascript-study/tree/main/day5

 

GitHub - minseon6371/javascript-study

Contribute to minseon6371/javascript-study development by creating an account on GitHub.

github.com

 

728x90
반응형