Bibi's DevLog ๐Ÿค“๐ŸŽ

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - 6,7๋‹จ ๊ตฌํ˜„(๋ฐ˜๋ณต๋ฌธ) & ์ฝ”๋”ฉ ๊ตฌํ˜„ ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Javascript ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - 6,7๋‹จ ๊ตฌํ˜„(๋ฐ˜๋ณต๋ฌธ) & ์ฝ”๋”ฉ ๊ตฌํ˜„

๋น„๋น„ bibi 2020. 10. 7. 17:58
* ์ด ๊ธ€์€ ์ธํ”„๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜ธ๋ˆ…์Šค๋‹˜์˜ ์œ ๋ฃŒ ๊ฐ•์˜ '์‰ฝ๊ณ  ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐ์›Œ๋ณด๋Š” Javascript ์ž…๋ฌธ - ์ฝ”๋“œ์Šค์ฟผ๋“œ ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ๋ ˆ๋ฒจ1'๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๊ฐ•์˜ ๋‚ด์šฉ์— ๋”ํ•ด, ์ œ๊ฐ€ ํ•„๊ธฐํ•˜๊ณ  ๊ตฌ๊ธ€๋งํ•œ ๋‚ด์šฉ์ด ์ •๋ฆฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๋ณด์ธ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋Œ€๋กœ ์ •๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ถ€์ •ํ™•ํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ์„ ๊ฐ์•ˆํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ธํ”„๋Ÿฐ ์ธก ๋‹ต๋ณ€์„ ๋ฐ›์€ ์ ์ด ์žˆ์œผ๋‚˜(https://bibi6666667.tistory.com/37), ์ €์ž‘๊ถŒ์ƒ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค๋ฉด ๋ณดํ˜ธ ๋ชจ๋“œ(๋น„๊ณต๊ฐœ)๋กœ ์ „ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ•™์Šต ๋ชฉํ‘œ

  • ๋ฐ˜๋ณต๋ฌธ์„ ๊ฒฝํ—˜ํ•œ๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ,์กฐ๊ฑด๋ฌธ(์ œ์–ด๋ฌธ)์€ ์ปดํ“จํ„ฐ์—๊ฒŒ ์—ฌ๋Ÿฌ ๋ณต์žกํ•œ ์ผ์„ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”, ๊ต‰์žฅํžˆ ์ค‘์š”ํ•œ ๋„๊ตฌ๋‹ค.
โœ…๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ์žˆ๋‹ค = ์ฝ”๋”ฉ์„ ์ž˜๋ชป ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค.
-> ๋ฐ˜๋ณต๋ฌธ, ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ˜๋ณต์„ ์ œ๊ฑฐํ•ด ๊น”๋”ํ•œ ์ฝ”๋”ฉ์„ ํ•˜๋Š” ์—ฐ์Šต์„ ํ•ด์•ผ ํ•œ๋‹ค.
** var === variable ์ฆ‰ ๋ณ€์ˆ˜๋ผ๋Š” ๋œป.

โœ…๋ณ€์ˆ˜๋ช…์€ ์ค‘์š”ํ•˜๋‹ค.
- ์˜๋ฏธ์žˆ๋Š” ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•˜๊ณ ,
- ๊ฐœ๋ฐœ์ž๋“ค๋ผ๋ฆฌ ์•ฝ์†ํ•œ ๊ทœ์น™์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค(= ์ปจ๋ฒค์…˜convention)

์š”๊ตฌ์‚ฌํ•ญ

  • ์ง€๊ธˆ๊นŒ์ง€ 2 ~ 5๋‹จ๊นŒ์ง€ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋‹จ์ˆœ, ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์ด ๋งŽ์•˜๋‹ค.
  • ์ด ๊ฐ™์€ ๋‹จ์ˆœ, ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ๋ณ€์ˆ˜์™€ ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•ด ์ œ๊ฑฐํ•˜๋ฉด์„œ 6๋‹จ๊ณผ 7๋‹จ์„ ๊ตฌํ˜„ํ•œ๋‹ค.

๊ฒฐ๊ณผ๋ฌผ

  • gugudan5.html
  • gugudan6.html
  • github ์—…๋กœ๋“œ(์ปค๋ฐ‹ / ํ‘ธ์‹œ) ๋„ ๊ผญ ํ•  ๊ฒƒ!

ํžŒํŠธ

  • while ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
var i = n ;  
  // 1. i๋ผ๋Š” ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”.
while(์กฐ๊ฑด์‹condition) {
  // 2. ์กฐ๊ฑด์‹์ด ์ฐธ์ธ์ง€ ๊ฒ€์‚ฌ
  ์ฝ”๋“œ ๋ธ”๋ก code block
  // 3. ์›ํ•˜๋Š” ์ฝ”๋“œ๋ธ”๋ก ์‹คํ–‰
  i++;
  // 4. ๊ฐ’์„ ์ฆ๊ฐ
}
var i = 1;
while (i < 10) {
  // ๊ณฑ์…ˆ ๊ฒฐ๊ณผ ์ถœ๋ ฅ 
  // i ๊ฐ’์„ 1 ์ฆ๊ฐ€ 
}

 

  • for ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
for (1.๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ; 2.์กฐ๊ฑด์‹ ; 4.๊ฐ’์„ ์ฆ๊ฐ ) {
 3. ์›ํ•˜๋Š” ์ฝ”๋“œ๋ธ”๋ก ์‹คํ–‰
}
for (var i = 0; i <10; i++) {
// ๊ณฑ์…ˆ ๊ฒฐ๊ณผ ์ถœ๋ ฅ 
}

 

ํ•™์Šต ์ฐธ๊ณ ์ž๋ฃŒ

๋ฐ˜๋ณต๋ฌธ

https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Loops_and_iteration

 

๋ฃจํ”„์™€ ๋ฐ˜๋ณต

๋ฃจํ”„๋Š” ์–ด๋–ค ๊ฒƒ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹œํ–‰ํ• ๋•Œ ๋น ๋ฅด๊ณ  ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. JavaScript Guide์˜ ์ด ์ณ…ํ„ฐ๋Š” JavaScript ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์„œ๋กœ๋‹ค๋ฅธ ๋ฐ˜๋ณต๋ฌธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

๋ณด๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ : bibi6666667.tistory.com/60

 

์ฐธ๊ณ  ์ž๋ฃŒ ) Javascript์˜ ๋ฐ˜๋ณต๋ฌธ(loop) - mozilla ๋ฌธ์„œ

โ€ป ์ด ๋ฌธ์„œ๋Š” bibi6666667.tistory.com/56์˜ ์ฐธ์กฐ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. [์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - 6,7๋‹จ ๊ตฌํ˜„(๋ฐ˜๋ณต๋ฌธ) & ์ฝ”๋”ฉ ๊ตฌํ˜„ ๋น„๋น„์˜ ์ดˆ์› Bibi's grasslandโ›บ [์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - 6,7๋‹จ ๊ตฌํ˜„(๋ฐ˜๋ณต๋ฌธ) & ์ฝ”๋”ฉ..

bibi6666667.tistory.com


 

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ

gugudan5.html (while๋ฌธ, ์ฝ˜์†” ๋ฒ„์ „)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>๊ตฌ๊ตฌ๋‹จ 6,7๋‹จ - ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ(while๋ฌธ), ์ฝ˜์†” ๋ฒ„์ „</h1>
    <script>
        var num = Number(prompt('๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”'));
        //๋ฐ›์€ ์ˆซ์ž์— 1~9๋ฅผ ๊ณฑํ•ด ์ถœ๋ ฅํ•œ๋‹ค.
        var one = 1;
        while (one <= 9) {
            console.log(num + "*" + one + "=" + (num * one));
            one++;
        }
    </script>
</body>
</html>

gugudan5.html (for ๋ฌธ, ์›น ๋ฒ„์ „)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ตฌ๊ตฌ๋‹จ 6,7๋‹จ - ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ(for๋ฌธ), ์›น ๋ฒ„์ „</h1>
<script>
    var num = Number(prompt('๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”'));
    //๋ฐ›์€ ์ˆซ์ž์— 1~9๋ฅผ ๊ณฑํ•ด ์ถœ๋ ฅํ•œ๋‹ค.
    for (var i = 1; i <= 9; i++ ) {
        document.write(num + "*" + i + "=" + (num * i) + "<br>");
    }
</script>
</body>
</html>

 

์˜ค๋‹ต๋…ธํŠธ

 

  • ๊ฐœ๋ฐœ์ž๋“ค์€ ๋ถ€๋“ฑ์‹์„ ํ‘œํ˜„ํ•  ๋•Œ,
    i <= 9 ๋ณด๋‹ค๋Š” 1 < 10 ์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ด๋‹ค.
  • ๋ณ€์ˆ˜๋ช…์€ one๋ณด๋‹ค i ๊ฐ™์€ ์•ฝ์ž๊ฐ€ ์ข‹๋‹ค.

++ ์‹ฌํ™”๊ณผ์ • : prompt() ๋Œ€์‹  <input>์œผ๋กœ ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ตฌ๊ตฌ๋‹จ 6,7๋‹จ - ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ(for๋ฌธ), ์›น ๋ฒ„์ „</h1>
<h2>html input์ฐฝ์„ ์ถ”๊ฐ€ํ•ด๋ณด๊ธฐ</h2>
์ถœ๋ ฅํ•  ๋‹จ์ˆ˜ : <input type="text" id="num" value=""> <br>
<button onclick="runGugudan()">์ถœ๋ ฅ</button>
<script>
    function runGugudan() {
        var strnum = document.getElementById("num").value;
        var num = Number(strnum);
        document.write('<h2>๊ตฌ๊ตฌ๋‹จ' + num + '๋‹จ</h2>');
        //๋ฐ›์€ ์ˆซ์ž์— 1~9๋ฅผ ๊ณฑํ•ด ์ถœ๋ ฅํ•œ๋‹ค.
        for (var i = 1; i <= 9; i++) {
            document.write(num + "*" + i + "=" + (num * i) + "<br>");
        }
    }
</script>
</body>
</html>
<input type="text" id="num" value="">
html์˜ input ํƒœ๊ทธ ์ค‘ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž….
id = ๊ณ ์œ ํ•œ ๊ฐ’.
<button onclick="runGugudan()">์ถœ๋ ฅ</button>
<button>๋ฒ„ํŠผ์ด๋ฆ„</button> : ๋ฒ„ํŠผ์„ ๋งŒ๋“œ๋Š” ํƒœ๊ทธ.
onclick="ํ•จ์ˆ˜๋ช…" : ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰.
function runGugudan() { }
runGugudan์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“  ๊ฒƒ.
๋งŒ๋“ค์–ด ๋‘์—ˆ๋˜ ์กฐ๊ฑด๋ฌธ์„ ํ•จ์ˆ˜ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ๋„ฃ๋Š”๋‹ค.
document.getElementById("num").value;
document. = html๋ฌธ์„œ์—์„œ.
getElementById("id๊ฐ’")= "id๊ฐ’"์ธ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์™€๋ผ.
.value : ๊ทธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€๋ผ.