-
명품 웹 프로그래밍 7장 과제웹프로그래밍 2019. 5. 14. 18:47
2.
<!DOCTYPE html> <html> <head> <title>정수 5개 입력받아 역순으로 출력</title> </head> <body> <h3>정수 5개 입력받아 역순으로 출력</h3> <hr> <script type="text/javascript"> var n=[]; for(var i=0 ; i<5 ; ++i) { n[i]=prompt("정수 입력"); } document.write("입력된 수의 배열<br>"); for(var i=0 ; i<5 ; ++i) { document.write(n[i] + " "); } document.write("<hr>"); document.write("역순으로 재정렬된 배열<br>"); n.reverse(); for(var i=0 ; i<5 ; ++i) { document.write(n[i] + " "); } </script> </body> </html>
4.
<!DOCTYPE html> <html> <head> <title>방문 시간에 따라 변하는 배경색</title> </head> <body> <h3>일요일은 pink, 다른 요일은 gold 배경</h3> <hr> <script type="text/javascript"> var current=new Date(); if(current.getDay()==0) document.body.style.backgroundColor="pink"; else document.body.style.backgroundColor="gold"; document.write("오늘 : "); document.write(current.getDate() +"일,"); switch(current.getDay()) { case 0: document.write("일요일<br>"); break; case 1: document.write("월요일<br>"); break; case 2: document.write("화요일<br>"); break; case 3: document.write("수요일<br>"); break; case 4: document.write("목요일<br>"); break; case 5: document.write("금요일<br>"); break; case 6: document.write("토요일<br>"); break; } </script> </body> </html>
8.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Math.random()으로 랜덤한 색 만들기</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <script src="main.js"></script> <style> div { display: inline-block; width: 150px; padding: 10px;} </style> </head> <body> <h3>16개의 랜덤한 색 만들기</h3> <hr> <script> for(var i=0 ; i<4 ; ++i) { for(var j=0 ; j<4 ; ++j) { var x=Math.random()*255; x=Math.floor(x); var y = Math.random() * 255; y = Math.floor(y); var z = Math.random() * 255; z = Math.floor(z); document.write("<div style='background-color : "); document.write("rgb(" + x+","+y+","+z+")'>"); document.write("rgb(" + x + "," + y + "," + z + ")"); document.write("</div>"); } document.write("<br>"); } </script> </body> </html>
7-10Literal
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>book 객체 배열 만들기</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <script src="main.js"></script> <script> var a = -999; var bookArray = new Array(); for (var i = 0; i < 5; ++i) { var data = prompt("콤마(,)으로 분리하면서 책제목 저자 가격 순으로 입력"); var sup = data.split(","); var book = { title : sup[0], author : sup[1], price : sup[2], max: function () { for (var i = 0; i < bookArray.length; ++i) { if (bookArray[i].price > a) { a = bookArray[i].price; var maxTitle = bookArray[i].title; } } return maxTitle; } } bookArray[i] = book; } </script> </head> <body> <h3>book 객체 배열 만들기</h3> <hr> <script> for (var i = 0; i < bookArray.length; ++i) { document.write(bookArray[i].title + ", "); document.write(bookArray[i].author + ", "); document.write(bookArray[i].price + "<br>"); } document.write("<hr>"); document.write("가장 가격이 비싼 책은" + book.max()); </script> </body> </html>
7-10new Object
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>book 객체 배열 만들기</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <script src="main.js"></script> <script> var a=-999; function max() { for(var i=0 ; i<bookArray.length ; ++i) { if(bookArray[i].price>a) { a=bookArray[i].price; var maxTitle=bookArray[i].title; } } return maxTitle; } var bookArray = new Array(); for(var i=0 ; i<5 ; ++i) { var data = prompt("콤마(,)으로 분리하면서 책제목 저자 가격 순으로 입력"); var sup = data.split(","); var book = new Object(); book.title = sup[0]; book.author = sup[1]; book.price = sup[2]; book.max = max; bookArray[i] = book; } </script> </head> <body> <h3>book 객체 배열 만들기</h3> <hr> <script> for (var i = 0; i < bookArray.length; ++i) { document.write(bookArray[i].title + ", "); document.write(bookArray[i].author + ", "); document.write(bookArray[i].price + "<br>"); } document.write("<hr>"); document.write("가장 가격이 비싼 책은"+book.max()); </script> </body> </html>
7-10Object Constructor
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>book 객체 배열 만들기</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <script src="main.js"></script> <script> var a = -999; function Book(title, author,price) { this.title=title; this.author=author; this.price=price; this.max= function() { for (var i = 0; i < bookArray.length; ++i) { if (bookArray[i].price > a) { a = bookArray[i].price; var maxTitle = bookArray[i].title; } } return maxTitle; } } var bookArray = new Array(); for (var i = 0; i < 5; ++i) { var data = prompt("콤마(,)으로 분리하면서 책제목 저자 가격 순으로 입력"); var sup = data.split(","); var book = new Book(sup[0], sup[1], sup[2]) bookArray[i] = book; } </script> </head> <body> <h3>book 객체 배열 만들기</h3> <hr> <script> for (var i = 0; i < bookArray.length; ++i) { document.write(bookArray[i].title + ", "); document.write(bookArray[i].author + ", "); document.write(bookArray[i].price + "<br>"); } document.write("<hr>"); document.write("가장 가격이 비싼 책은" + book.max()); </script> </body> </html>
'웹프로그래밍' 카테고리의 다른 글
명품 웹 프로그래밍 9장 과제 (1) 2019.05.14 명품 웹 프로그래밍 8장 과제 (0) 2019.05.14 명품 웹 프로그래밍 6장 과제 (0) 2019.05.14 명품 웹 프로그래밍 5장 과제 (0) 2019.05.14 명품 웹 프로그래밍 4장 과제 (0) 2019.05.14