javascript: 객체(배열객체, 숫자객체), 실습- 요일 구하기, 달력 만들기

2020. 12. 30. 14:07프론트엔드/JavaScript

반응형

 

 

Array 배열객체

 

배열 선언

1. var 배열명 = new Array();

배열명[0]=데이터1;

배열명[1]=데이터2;

배열명[2]=데이터3;

2. var 배열명 = new Array(데이터1, 데이터2,데이터3);

3. var 배열명 = [데이터1, 데이터2];

 

        var fruits = new Array();
        fruits[0] = "apple";
        fruits[1] = "grape";
        fruits[2] = "pear";
        console.log(fruits);

        var fruits2 = new Array("melon", "banana", "orange");
        console.log(fruits2);

        var fruits3 = ["kiwi", "strawberry"];
        console.log(fruits3);
        
        console.log(fruits2[1]);

 

속성과 메서드

        var fruits = new Array("banana", "apple", "orange");
        var veg = new Array("potato", "cabbage", "tomato");
        console.log(fruits);

        //배열의 마지막에 데이터 추가
        fruits.push("pineapple");
        console.log(fruits);

        //배열의 마지막 데이터 삭제
        fruits.pop();
        console.log(fruits);

        //배열의 처음에 데이터 추가
        fruits.unshift("kiwi");
        console.log(fruits);

        //배열의 처음 데이터 삭제
        fruits.shift();
        console.log(fruits);

        //배열의 2번째 위치 이후부터 데이터 2개 추가
        fruits.splice(2, 0, "peach", "grape"); //(시작포인트,잘라내는갯수,추가 데이터)
        console.log(fruits);

        //배열의 3번째 데이터 삭제하고 새 데이터 추가
        fruits.splice(2, 1, "strawberry");
        console.log(fruits);

        //2번째 위치부터 데이터 삭제
        fruits.splice(2, 2);
        console.log(fruits);

        //배열 데이터 정렬
        fruits.sort();
        console.log(fruits);

        //배열 데이터 역순 정렬
        fruits.reverse();
        console.log(fruits);

        //배열 2개 합치기
        var result = fruits.concat(veg);
        console.log(result);

        //잘라내기
        var result2 = result.slice(3, 5); //(시작점,끝점)
        console.log(result2);
        console.log(result); //원래의 데이터 배열에 변화x

        //result의 데이터를 *로 연결
        var sp = result.join("*");
        console.log(sp);

        //배열의 데이터 갯수 확인
        console.log(result.length);

 

 

실습

오늘의 요일 구하기

        var today = new Date();
        var week = ["sun", "mon", "tue", "wends", "thurs", "fri", "sat"];
        var theDay = today.getDay(); //0sun 1mon 2tue
        console.log(theDay);

        document.write("today is " + week[theDay] + "day!");

 

랜덤 추천메뉴

        var menu=["생선구이","알탕","된장찌개","라면","햄버거","짜장면"];
        
        var num= Math.floor(Math.random()*menu.length);
        console.log(num);
        document.write("오늘의 추천 메뉴는 "+menu[num]+"입니다");

 

달력 만들기

        var today = new Date();

        var year = today.getFullYear();
        var month = today.getMonth();
        var date = today.getDate();

        //이번 달 1일에 대한 정보
        var day1 = new Date(year, month, 1);

        //이번 달 1일의 요일 정보
        var weekday1 = day1.getDay();
        console.log(weekday1);

        //각 달의 마지막 날짜 정보
        var lastDate = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        //js는 순서를 0부터 체크, month,lastDate 모두 0부터 순서 시작

        //줄수 계산
        //마지막 날짜 정보, 이달 1일의 요일정보(weekday1) 
        //ex, weekday1=2, 2+31=33/7=4.*** 올림
        var row = Math.ceil((weekday1 + lastDate[month]) / 7);
        console.log(row);

        //날짜
        var num = 1;

        //달력
        var cal = "<table>";
        cal += "<tr>";
        cal += "<th>S</th>";
        cal += "<th>M</th>";
        cal += "<th>T</th>";
        cal += "<th>W</th>";
        cal += "<th>T</th>";
        cal += "<th>F</th>";
        cal += "<th>S</th>";
        cal += "</tr>";
        //줄
        for (i = 1; i <= row; i++) {
            cal += "<tr>";
            //칸
            for (j = 1; j <= 7; j++) {
                if (j <= weekday1 && i == 1 || num > lastDate[month]) {
                    cal += "<td></td>";
                } else {
                    cal += "<td>" + num + "</td>";
                    num++;
                }
            }
            cal += "</tr>";
        }
        cal += "</table>";
        document.write(cal);

 

Number 숫자객체

        var num1="10.5555";
        var num2="3.3333";
        var num3=124.5678;
        var num4="string"
        
        //문자를 숫자로
        document.write(Number(num1)+10,"<Br>");
        
        //문자를 정수로
        document.write(parseInt(num1)+10,"<Br>");
        
        //문자를 실수로
        document.write(parseFloat(num2)+10,"<br>");
        
        //소숫점 둘째자리까지만
        document.write(num3.toFixed(2),"<br>");
        
        //소숫점 포함 전체 자릿수 출력
        document.write(num3.toPrecision(4),"<br>");
        
        //문자인지 확인
        document.write(isNaN(num1),"<br>");
        document.write(isNaN(num3),"<br>");
        document.write(isNaN(num4));

 

 

반응형