๐2022-08-04๐
ex7-01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฐ์ฒด ์์ฑ ๋ฐ ํ์ฉ</title>
</head>
<body>
<h3>๊ฐ์ฒด ์์ฑ ๋ฐ ํ์ฉ</h3>
<hr>
<script>
// Date ๊ฐ์ฒด ์์ฑ
let today = new Date();
// Date ๊ฐ์ฒด์ toLocaleString() ๋ฉ์๋ ํธ์ถ
document.write("ํ์ฌ ์๊ฐ : " + today.toLocaleString() + "<br>");
// String ๊ฐ์ฒด ์์ฑ
let mystr= new String("์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถํ๊ธฐ");
document.write("mystr์ ๋ด์ฉ : " + mystr + "<br>");
document.write("mystr์ ๊ธธ์ด : " + mystr.length + "<br>");
// mystr.length=10; // ์ด ๋ฌธ์ฅ์ ์ค๋ฅ์ด๋ค.
</script>
</body>
</html>
ex7-02.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>[]๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</title>
</head>
<body>
<h3>[]๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</h3>
<hr>
<script>
let plots = [20, 5, 8, 15, 20]; // ์์ 5๊ฐ์ ๋ฐฐ์ด ์์ฑ
document.write("var plots = [20, 5, 8, 15, 20]<br>");
for(let i=0; i<5; i++) { // 5 ๋์ plots.length๋ก ํด๋ ๋จ
let size = plots[i]; // plots ๋ฐฐ์ด์ i๋ฒ์งธ ์์
while(size>0) {
document.write("*");
size--;
}
document.write(plots[i] + "<br>");
}
</script>
</body>
</html>
ex7-03.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Array ๊ฐ์ฒด๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</title>
</head>
<body>
<h3>Array ๊ฐ์ฒด๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</h3>
<hr>
<script>
let degrees = new Array(); // ๋น ๋ฐฐ์ด ์์ฑ
degrees[0] = 15.1;
degrees[1] = 15.4;
degrees[2] = 16.1;
degrees[3] = 17.5;
degrees[4] = 19.2;
degrees[5] = 21.4;
let sum = 0;
// for(let i=0; i<degrees.length; i++)
// sum += degrees[i];
for(let i of degrees){
sum += i;
}
document.write("ํ๊ท ์จ๋๋ " + sum/degrees.length + "<br>");
</script>
</body>
</html>
ex7-04.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>[]๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</title>
</head>
<body>
<h3>[]๋ก ๋ฐฐ์ด ๋ง๋ค๊ธฐ</h3>
<hr>
<script>
let plots = [20, 5, 8, 15, 20]; // ์์ 5๊ฐ์ ๋ฐฐ์ด ์์ฑ
document.write("var plots = [20, 5, 8, 15, 20]<br>");
for(let i=0; i<5; i++) { // 5 ๋์ plots.length๋ก ํด๋ ๋จ
let size = plots[i]; // plots ๋ฐฐ์ด์ i๋ฒ์งธ ์์
while(size>0) {
document.write("*");
size--;
}
document.write(plots[i] + "<br>");
}
</script>
</body>
</html>
ex7-07.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>String ๊ฐ์ฒด์ ๋ฉ์๋ ํ์ฉ</title>
</head>
<body>
<h3>String ๊ฐ์ฒด์ ๋ฉ์๋ ํ์ฉ</h3>
<hr>
<script>
let a = new String("Boys and Girls");
let b = "!!";
document.write("a : " + a + "<br>");
document.write("b : " + b + "<br><hr>");
document.write(a.charAt(0) + "<br>");
document.write(a.concat(b, "์
๋๋ค") + "<br>");
document.write(a.indexOf("s") + "<br>");
document.write(a.indexOf("And") + "<br>");
document.write(a.slice(5, 8) + "<br>");
document.write(a.substr(5, 3) + "<br>");
document.write(a.toUpperCase() + "<br>");
document.write(a.replace("and", "or") + "<br>");
document.write(" kitae ".trim() + "<br><hr>");
let sub = a.split(" ");
document.write("a๋ฅผ ๋น์นธ์ผ๋ก ๋ถ๋ฆฌ<br>");
for(let i=0; i<sub.length; i++)
document.write("sub" + i + "=" + sub[i] + "<br>");
document.write("<hr>String ๋ฉ์๋๋ฅผ ์คํ ํ a์ b ๋ณํจ ์์<br>");
document.write("a : " + a + "<br>");
document.write("b : " + b + "<br>");
</script>
</body>
</html>
ex7-10.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋ฆฌํฐ๋ด ํ๊ธฐ๋ฒ์ผ๋ก ๊ฐ์ฒด ๋ง๋ค๊ธฐ</title>
<script>
//์ฌ์ฉ์ ๊ฐ์ฒด ๋ง๋ค๊ธฐ
let account = {
// ํ๋กํผํฐ ์์ฑ ๋ฐ ์ด๊ธฐํ
owner : "ํฉ๊ธฐํ", // ๊ณ์ข ์ฃผ์ธ
code : "111", // ๊ณ์ข ์ฝ๋
balance : 35000, // ์์ก ํ๋กํผํฐ
// ๋ฉ์๋ ์์ฑ
inquiry : function () { return this.balance; }, // ์๊ธ ์กฐํ
deposit : function(money) { this.balance += money; }, // ์ ๊ธ. money ๋งํผ ์ ๊ธ
withdraw : function (money) { // ์๊ธ ์ธ์ถ, money๋ ์ธ์ถํ๊ณ ์ ํ๋ ์ก์
// money๊ฐ balance๋ณด๋ค ์๋ค๊ณ ๊ฐ์
this.balance -= money;
return money;
}
};
</script>
</head>
<body>
<h3>๋ฆฌํฐ๋ด ํ๊ธฐ๋ฒ์ผ๋ก ์ฌ์ฉ์ ๊ฐ์ฒด ๋ง๋ค๊ธฐ</h3>
<hr>
<script>
document.write("account : ");
document.write(account.owner + ", ");
document.write(account.code + ", ");
document.write(account.balance + "<br>");
account.deposit(10000); // 10000์ ์ ๊ธ
document.write("10000์ ์ ๊ธ ํ ์์ก์ " + account.inquiry() + "<br>");
account.withdraw(5000); // 5000์ ์ธ์ถ
document.write("5000์ ์ธ์ถ ํ ์์ก์ " + account.inquiry() + "<br>");
</script>
</body>
</html>
ex7-11.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ๋กํ ํ์
์ผ๋ก ๊ฐ์ฒด ๋ง๋ค๊ธฐ</title>
<script>
// ํ๋กํ ํ์
๋ง๋ค๊ธฐ : ์์ฑ์ ํจ์ ์์ฑ
function Account(owner, code, balance) {
// ํ๋กํผํฐ ๋ง๋ค๊ธฐ
this.owner = owner; // ๊ณ์ข ์ฃผ์ธ ํ๋กํผํฐ ๋ง๋ค๊ธฐ
this.code = code; // ๊ณ์ข ์ฝ๋ ํ๋กํผํฐ ๋ง๋ค๊ธฐ
this.balance = balance; // ์์ก ํ๋กํผํฐ ๋ง๋ค๊ธฐ
// ๋ฉ์๋ ๋ง๋ค๊ธฐ
this.inquiry = function () { return this.balance; }
this.deposit = function (money) { this.balance += money; }
this.withdraw = function (money) { // ์๊ธ ์ธ์ถ, money๋ ์ธ์ถํ๋ ์ก์
// money๊ฐ balance๋ณด๋ค ์๋ค๊ณ ๊ฐ์
this.balance -= money;
return money;
}
}
</script>
</head>
<body>
<h3>Account ํ๋กํ ํ์
๋ง๋ค๊ธฐ</h3>
<hr>
<script>
// new ์ฐ์ฐ์ ์ด์ฉํ์ฌ ๊ณ์ข ๊ฐ์ฒด ์์ฑ
let account = new Account("ํฉ๊ธฐํ", "111", 35000);
// ๊ฐ์ฒด ํ์ฉ
document.write("account : ");
document.write(account.owner + ", ");
document.write(account.code + ", ");
document.write(account.balance + "<br>");
account.deposit(10000); // 10000์ ์ ๊ธ
document.write("10000์ ์ ๊ธ ํ ์์ก์ " + account.inquiry() + "<br>");
account.withdraw(5000); // 5000์ ์ธ์ถ
document.write("5000์ ์ธ์ถ ํ ์์ก์ " + account.inquiry() + "<br>");
</script>
</body>
</html>