JS 연습문제

연습문제.

오늘 날짜 출력

오늘 0000년00월00일 요일 시:분:초 순으로 날짜 객체로부터 받아와서 콘솔창에 출력하라. (요일정보는 배열데이터를 활용할 것)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth()+1;
const date = today.getDate();
const dayLabel = today.getDay();
const hours = today.getHours();
const minutes = today.getMinutes();
const seconds = today.getSeconds();

function getTodayLabel() {
const week = new Array('일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일');
const today = new Date().getDay();
const todayLabel = week[today];
return todayLabel;
}

console.log(`${year}${month}${date}${getTodayLabel()} ${hours}:${minutes}:${seconds}`);
1
2
3
4
5
6
7
8
9
10
const $today = new Date();
const $cur_year = $today.getFullYear();
const $cur_month = $today.getMonth() + 1;
const $cur_date = $today.getDate();
const $cur_day = $today.getDay(); //0 ~ 6
const $yoil = ["일", "월", "화", "수", "목", "금", "토"];
const $cur_hour = $today.getHours();
const $cur_min = $today.getMinutes();
const $cur_sec = $today.getSeconds();
console.log($cur_year + "년 " + $cur_month + "월 " + $cur_date + "일 " + $yoil[$cur_day] + "요일 " + $cur_hour + " : " + $cur_min + " : " + $cur_sec);

아기 돼지 삼형제

객체 배열을 활용하여 문장을 완성하시오.

  • 첫째 아기 돼지, 움막, 둘째 아기 돼지, 나무집, 막내 아기 돼지, 벽돌집 각 데이터로 구성할 것
  • 해당 항목은 key를 만들어서 각각 value값으로 넣어서 활용할 것
1
첫째 아기 돼지는 움막을 지었고, 둘째 아기 돼지는 나무집을 지었고, 막내 아기 돼지는 벽돌집을 지었습니다.
1
2
3
4
5
6
7
const $brother = [
{$pig : "첫째 아기 돼지", $house : "움막"},
{$pig : "둘째 아기 돼지", $house : "나무집"},
{$pig : "막내 아기 돼지", $house : "벽돌집"},
];

console.log(`${$brother[0].$pig}${$brother[0].$house}을 지었고, ${$brother[1].$pig}${$brother[1].$house}을 지었고, ${$brother[2].$pig}${$brother[2].$house}을 지었습니다.`);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const $brother = [
{$pig : "첫째 아기 돼지", $house : "움막"},
{$pig : "둘째 아기 돼지", $house : "나무집"},
{$pig : "막내 아기 돼지", $house : "벽돌집"},
];

console.log($brother[0].$pig+"는 "+$brother[0].$house+"을 지었고, "+$brother[1].$pig+"는 "+$brother[1].$house+"을 지었고, "+$brother[2].$pig+"는 "+$brother[2].$house+"을 지었습니다.");

let ex_02 = "";
for(i=0; i<$brother.length; i++){
if(i<$brother.length - 1){ //i = 0, 1
ex_02 += $brother[i].$pig+"는 "+$brother[i].$house+"을 지었고, ";
}else{ //i = 2
ex_02 += $brother[i].$pig+"는 "+$brother[i].$house+"을 지었습니다.";
}
}
console.log(ex_02);

좋아하는 과일

  • 빈 배열 데이터를 만드시오.
  • 순차적으로 배열 데이터 내에 좋아하는 과일 5개를 for문을 활용하여 하나씩 추가해 주시기 바랍니다.
  • for문을 반복하는 과정에서 console.log($fav_arr)로 출력하되 하나씩 추가되는 과정을 모두 출력하시오.
1
2
3
4
5
6
7
8
const $fav_arr = [];
const $fruit = ["사과", "오렌지", "바나나", "포도", "자두"];

for (i=0; i<$fruit.length; i++) {
console.log($fruit[i]);
$fav_arr.push($fruit[i]);
}
console.log($fav_arr);
1
2
3
4
for(i=0; i<$fruit.length; i++){
$fav_arr.push($fruit[i]);
console.log($fav_arr);
}

데이터 변경

다음과 같이 데이터를 변경하시오. (splice 메서드 활용)

1
["HTML", "CSS", "JAVASCRIPT", "JQUERY"];
1
["HTML", "WEB", "JAVASCRIPT", "JQUERY"];
1
2
3
const ex_04 = ["HTML", "CSS", "JAVASCRIPT", "JQUERY"];
ex_04.splice(1, 1, 'WEB');
console.log(ex_04);
1
2
ex_04.splice(1, 1 , "Bootstrap", "mySQL", "...");  //지정한 위치에 두개 이상의 데이터를 넣을 때
console.log(ex_04);

데이터 삽입

다음과 같이 배열 사이에 새로운 데이터를 넣으세요. (splice 메서드 활용 - 힌트 : 개수는 0개)

1
["red", "yellow", "green", "blue"];
1
["red", "orange", "yellow", "green", "blue"];
1
2
3
const ex_05 = ["red", "yellow", "green", "blue"];
ex_05.splice(1, 0, 'orange');
console.log(ex_05);

여러 데이터 삽입

다음과 같이 배열 데이터를 변경하세요. (splice 메서드만 활용 - 힌트 : 개수는 0개)

1
["korea", "USA", "Brazil", "France"];
1
["korea", "Japan", "USA", "Brazil", "France", "China"]
1
2
3
4
const ex_06 = ["korea", "USA", "Brazil", "France"];
ex_06.splice(1, 0, 'Japan');
ex_06.splice(5, 0, 'China');
console.log(ex_06);

배열 데이터 추출

다음과 같이 배열 데이터를 추출하시오 (slice 메서드 활용)

1
["red", "yellow", "green", "blue"];
1
["yellow", "green"]
1
2
3
const ex_07 = ["red", "yellow", "green", "blue"];
const result = ex_07.slice(1, 3);
console.log(result);

배열 데이터 추출2

다음과 같이 배열 데이터를 추출하시오.

1
["red", "yellow", "green", "blue"];
1
["yellow", "blue"]
1
2
3
4
5
6
7
8
9
const $color = ["red", "yellow", "green", "blue"];
const ex_08 = [];
const result1 = $color.slice(1, 2);
const result2 = $color.slice(3, 4);

ex_08.push(result1[0]);
ex_08.push(result2[0]);

console.log(ex_08);
1
2
3
4
const $c_1 = $color.slice(1, 2);
const $c_2 = $color.slice(3, 4);
const ex_08 = ex_08.concat($c_1, $c_2);
console.log(ex_08);
1
2
3
4
5
6
7
for(i=0; i<$color.length; i++){  //0,1,2,3
if(i % 2 != 0){ //1, 3
ex_08.push($color.slice(i, i+1).join());
console.log(ex_08);
}
}
console.log(ex_08);

문자 데이터 변경

문자 데이터 “2021-08-23”에서 “20210823”으로 변경하시오. (아래 방법 중에서 선택하여 적용하시오)

  • 방법1 : split() 메서드 활용하여 join() 메서드 적용하기
  • 방법2 : replace() 메서드 활용하기
1
2
3
4
const $date_str = "2021-08-23";
const date = $date_str.split("-");
const result = date.join("");
console.log(result);

방법2

1
2
const $replace_date = $date_str.replace(/-/g, "");
console.log($replace_date); //20210823

REFERENCE
뷰(Vue.js) 프로그래밍 과정 _ 하이미디어 아카데미

  • © 2020-2025 404 Not Found
  • Powered by Hexo Theme Ayer