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

[์ธํ”„๋Ÿฐ] Javascript ์ž…๋ฌธ - ๋ฐ˜๋ณต๋ฌธ1 ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ] Javascript ์ž…๋ฌธ - ๋ฐ˜๋ณต๋ฌธ1

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

 

  • ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์„ ํ•ฉ์ณ '์ œ์–ด๋ฌธ Control Statement'์ด๋ผ๊ณ  ํ•˜๋ฉฐ,
    ์ œ์–ด๋ฌธ์€ ๋กœ์ง, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์งค ๋•Œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ตฌ๋ฌธ์ด๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์€ ๋น„์Šทํ•œ ์ผ์€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น๋ฌธ์ž…๋‹ˆ๋‹ค.

๋ฐ˜๋ณต๋ฌธ = loop 
์กฐ๊ฑด๋ฌธ = conditional statement

while ๋ฐ˜๋ณต๋ฌธ

while(์กฐ๊ฑด) { console.log("์ฐธ์ผ๋•Œ ์‹คํ–‰๋จ"); }

  • while ์กฐ๊ฑด๋ฌธ์ด ์ฐธtrue์ผ ๋•Œ๋งŒ ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋จ.
  • ๊ฑฐ์ง“false์ด๋ฉด while์กฐ๊ฑด๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ด.
  • ์ฆ‰, ์กฐ๊ฑด๋ฌธ์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ while์กฐ๊ฑด๋ฌธ์„ ๊ณ„์† ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•œ๋‹ค.
    while ์กฐ๊ฑด๋ฌธ ๊ฒ€์ฆ-์ฐธ-์‹คํ–‰-๊ฒ€์ฆ-์ฐธ-์‹คํ–‰-๊ฒ€์ฆ-์ฐธ-์‹คํ–‰..-๊ฒ€์ฆ-๊ฑฐ์ง“-์ข…๋ฃŒ.

 

while(์กฐ๊ฑด) { 
console.log("์ฐธ์ผ๋•Œ ์‹คํ–‰๋จ");
}

โ€ป ๋ฌดํ•œ๋ฃจํ”„ 

<script>
var n = 1;
while (n<10){
    console.log(n);
}
</script>

n=1์ด๊ณ , n์€ ํ•ญ์ƒ 10๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ, ์œ„์˜ while๋ฌธ์€ ์ข…๋ฃŒ๋˜์ง€ ๋ชปํ•˜๊ณ  ๋ฌดํ•œํžˆ ๋ฐ˜๋ณต๋˜๊ฒŒ ๋œ๋‹ค.
-> ์ž˜๋ชป ์งœ์ง„ ์ฝ”๋“œ์ด๋‹ค!


var n = 1; while(n <= 100) { console.log("Hi " + n); n++; }

var n = 1;
while(n <= 100) {
  console.log("Hi " + n);
  n++; 
}

-> Hi 1, Hi 2, Hi 3 .. Hi 100 ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ.


์˜ˆ์ œ

while ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ 1์—์„œ 100๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ด ๋ด…์‹œ๋‹ค. -> ์Šค์Šค๋กœ ํ•ด ๋ณด๊ธฐ!

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ (๊ฒฐ๊ณผ = 5151)

// ์˜ˆ์ œ : while ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ 1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ธฐ
    var n = 0;
    var num = 0;
    while (n <= 100) {
        n++;
        num = num + n;
    }
    console.log(num);

๋‹ต์•ˆ (=5050)

var sum = 0;
var n = 1;
while (n<=100){
  sum = sum + n;
  console.log(n,sum);
  n++;
}

document.write(); 
-> () ๋‚ด์˜ ๊ฐ’์„ ์›นํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜.

 


์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด

var i = 0; //(1)
while (i <= 100) { //(2)
  console.log(i); //(3)
  i++; //(4) 
}

var i = 0; //(1)
-> ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

while (i <= 100) { //(2)
-> ์กฐ๊ฑด ๊ฒ€์‚ฌ

console.log(i); //(3)
i++; //(4)
-> ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ

}

=> ์œ„ while๋ฌธ์„ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•ด์„œ, ์ด๋ฅผ ์งง๊ฒŒ ์ค„์ด๊ณ ์ž for๋ฌธ์ด ๋“ฑ์žฅํ•จ.

 

๊ทธ๋ž˜์„œ for๊ฐ€ ๋“ฑ์žฅํ•จ

์œ„์˜ while ์ฝ”๋“œ์™€ ์™„์ „ํžˆ ๋˜‘๊ฐ™์Œ.

for (var i = 0; i <= 100; i++) { 
	console.log(i); 
}

for (var i = 0;
-> ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

i <= 100;
-> ์กฐ๊ฑด ๊ฒ€์‚ฌ

i++)
-> ๋ณ€์ˆ˜์— ๋ฐ˜๋ณต ์‹คํ–‰ํ•  ๋ช…๋ น

{ console.log(i); }
->  ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ

์ฆ‰..

for ( โ‘  ; โ‘ก ; โ‘ฃ ) {
  โ‘ข;

ํ˜•ํƒœ์ผ ๋•Œ โ‘ -โ‘ก-โ‘ข-โ‘ฃ-โ‘ก-โ‘ข-โ‘ฃ-โ‘ก-โ‘ข-โ‘ฃ.. ์ˆœ์œผ๋กœ ๋ฐ˜๋ณต๋˜๋‹ค๊ฐ€ โ‘ก ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์„ ๋•Œ ์ข…๋ฃŒ๋จ.

์œ„์˜ while์˜ˆ์ œ ์ฝ”๋“œ๋Š” for๋ฌธ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ

var sum = 0;
for (var n = 1; n <= 100; n++){
  sum += n;
  console.log(n,sum);
}
document.write('1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์€' +sum+'์ž…๋‹ˆ๋‹ค.');

 

โ€ป ๋ณตํ•ฉ๋Œ€์ž…์—ฐ์‚ฐ์ž
(์ฐธ๊ณ  : m.blog.naver.com/PostView.nhn?blogId=magnking&logNo=220972187134&proxyReferer=https:%2F%2Fwww.google.com%2F)

+=, -=, *=, /= ๋“ฑ์œผ๋กœ ์‚ฌ์šฉ (+=๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ)

value += 10;
value = value + 10;
// ์œ„์˜ ์ฝ”๋“œ์™€ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ฐ™์€ ์˜๋ฏธ์ด๋‹ค.

// ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ..

value -= 10;
value = value - 10;

value *= 10 ;
value = value * 10;

value /= 10;
value = value / 10;

// ๋‘ ์ค„์”ฉ ๊ฐ๊ฐ ๊ฐ™์€ ์˜๋ฏธ๋กœ ์“ฐ์ธ๋‹ค.

 

์ถ”๊ฐ€ ์„ค๋ช…

๋ฐ˜๋ณต๋ฌธ์€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. n ํšŒ ์ฝ”๋“œ ์‹คํ–‰
  2. ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ฌ ๋•Œ๊นŒ์ง€ ์ฝ”๋“œ ์‹คํ–‰
  3. ๋ฐฐ์—ด ์•ˆ์˜ ์›์†Œ๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ฝ”๋“œ ์‹คํ–‰

 

์˜ˆ์ œ

  1. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆ˜๋“ค์˜ ๊ฐฏ์ˆ˜์™€ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค. (๋‹จ 0์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ž…๋ ฅ ์ข…๋ฃŒ)
  2. 1์—์„œ 100๊นŒ์ง€ ์ง์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•ด ๋ด…์‹œ๋‹ค.
  3. 100, 99, 98, .. 0 ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค.

๋‚ด๊ฐ€ ํ‘ผ ์ฝ”๋“œ

1.

์‚ฌ์‹ค ๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ดํ•ด ๋ชป ํ–ˆ๋‹ค.. ;;
์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ์ด๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ1</h2>
<script>
    var n = Number(prompt('์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”'));
    var sum = 0;
    if (n === 0) {
        alert('์ž…๋ ฅ ์ข…๋ฃŒ');
    } else if (n != 0) {
        console.log('์ˆซ์ž์˜ ๊ธธ์ด๋Š” ' + n.length + '์ž…๋‹ˆ๋‹ค.')
        for (var i = 0; i <= n.length; i++) {
            console.log(n.length);
            each_num = n.slice(i, i + 1);
            console.log(each_num)
            sum += each_num
            console.log('์ž…๋ ฅํ•œ ์ˆซ์ž๋Š”'+each_num+'ํ•ฉ๊ณ„๋Š”'+sum);
        }
    }
</script>
</body>
</html>

2.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ2</h2>
<script>
    var sum = 0;
    for (var i = 1; i<=100; i++) {
        if (i%2===0) {
            sum += i;
            console.log(i, sum);
        }
    };
    document.write('1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ง์ˆ˜์˜ ํ•ฉ์€'+sum+'์ž…๋‹ˆ๋‹ค.');
</script>
</body>
</html>

 

3.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ3</h2>
<script>
    for (var i = 100; i>=0 ; i--){
        console.log(i)
    }
</script>
</body>
</html>

์˜ˆ์ œ ํ’€์ด

https://gist.github.com/honux77/eff05449a401304aed7b95d8d91b18db

 

JS loop example

JS loop example. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

1๋ฒˆ ์˜ค๋‹ต๋…ธํŠธ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ1</h2>
<script>
    var sum = 0;
    var n = -1;
    // <- n=0์€ ์ข…๋ฃŒ์กฐ๊ฑด์œผ๋กœ while ์กฐ๊ฑด๋ฌธ์— false๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, prompt์ฐฝ์ด ๋œจ์ง€ ์•Š๋Š”๋‹ค.
    // ๊ทธ๋ž˜์„œ n=0์ด ์•„๋‹Œ ์ž„์˜์˜ ์ˆ˜๋กœ ์‹œ์ž‘.
    var count = -1;
    // <- 0์ž…๋ ฅ์€ ์ข…๋ฃŒ์˜ ์—ญํ• ์ด๊ธฐ ๋•Œ๋ฌธ์— 0์ž…๋ ฅ๋ถ„์€ ์ œ์™ธํ•ด์•ผ. ๊ทธ๋ž˜์„œ -1๋ถ€ํ„ฐ ์‹œ์ž‘
    while(n != 0) {
        n = Number(prompt("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”(0 = ์ข…๋ฃŒ)"));
        sum += n;
        count++;
        console.log(count, n, sum);
    }
    document.write("์ž…๋ ฅํ•œ " +  count + "์ˆ˜์˜ ํ•ฉ์€ " +  sum + "์ž…๋‹ˆ๋‹ค.");
</script>
</body>
</html>

2๋ฒˆ ์˜ค๋‹ต๋…ธํŠธ

-> ์ฝ”๋“œ๊ฐ€ ์™„์ „ํžˆ ๋˜‘๊ฐ™๋‹ค! ์˜ˆ์—~๐Ÿ‘๐Ÿ‘

3๋ฒˆ ์˜ค๋‹ต๋…ธํŠธ

-> ํ•ด๋‹ต ์ฝ”๋“œ๊ฐ€ ์—†๋‹ค! ๐Ÿ˜