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

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

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

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ์—ฐ์Šต๋ฌธ์ œ1

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

<์—ฐ์Šต๋ฌธ์ œ 1>

์—ฐ์Šต๋ฌธ์ œ1

  1. ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 4์น™์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•ด ๋ด…์‹œ๋‹ค.
  2. BMI๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค.
  3. ํ™”์”จ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์„ญ์”จ๋กœ ๋ฐ”๊พธ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค.
  4. ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค.

-> ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ๋งŒ ํ•จ๊ป˜ ํ’€๊ณ , ๋‚˜๋จธ์ง€๋Š” ํ˜ผ์ž์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ ๊ตฌ๊ธ€๋ง ๋“ฑ์„ ํ†ตํ•ด ํ’€์–ด๋ณด๊ธฐ.
-> ๋„์ €ํžˆ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์˜์ƒ์„ ๋ณธ ๋’ค, ๋‹ค ์ง€์šฐ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ํ•˜๊ธฐ.
-> ์™„์ „ํžˆ ๋‚ด ํž˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ธฐ.

๋‹ต์•ˆ ์˜ˆ์‹œ

1๋ฒˆ.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>์—ฐ์Šต๋ฌธ์ œ 1-1</h2>
<script>
    var s1 = prompt('์ˆซ์ž1์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.');
    var s2 = prompt('์ˆซ์ž2๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.');
    var n1 = Number(s1);
    var n2 = Number(s2);
    console.log(n1 + "+" + n2 + "=" + (n1+n2));
    console.log(n1 + "-" + n2 + "=" + (n1-n2));
    console.log(n1 + "*" + n2 + "=" + (n1*n2));
    console.log(n1 + "/" + n2 + "=" + (n1/n2));
</script>
</body>
</html>

 

(์•„๋ž˜๋Š” ๋‚ด๊ฐ€ ํ˜ผ์ž ํ‘ผ ์ฝ”๋“œ)

 

2๋ฒˆ.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>์—ฐ์Šต๋ฌธ์ œ 1-2</h2>
<script>
    var weight = prompt("์ฒด์ค‘์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”(kg)");
    var height = prompt("ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”(cm)");
    var wN = Number(weight);
    var hN = Number(height)*0.01;
    console.log(hN);
    console.log("๋‹น์‹ ์˜ BMI์ง€์ˆ˜๋Š”"+(wN/(hN*hN))+'์ž…๋‹ˆ๋‹ค');
</script>
</body>
</html>

3๋ฒˆ.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>์—ฐ์Šต๋ฌธ์ œ 1-3</h2>
<script>
    var ff = prompt('ํ™”์”จ ๋ช‡ ๋„ ์ธ๊ฐ€์š”?(°F)');
    var fN = Number(ff);
    var fTOc = (fN - 32) / 1.8;
    console.log('ํ™”์”จ(°F)'+fN+'๋„๋Š” ์„ญ์”จ(โ„ƒ)'+ fTOc +'๋„ ์ž…๋‹ˆ๋‹ค.');
</script>
</body>
</html>

4๋ฒˆ.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>์—ฐ์Šต๋ฌธ์ œ 1-4</h2>
<script>
    var str = prompt('๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!');
    console.log('์ž…๋ ฅํ•œ ๋ฌธ์ž์˜ ๊ธธ์ด๋Š”'+str.length+'์ž…๋‹ˆ๋‹ค.');
</script>
</body>
</html>

 

ํ’€์ด ์˜์ƒ

ํ•ด๋‹ต ๋ณด๊ธฐ

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

 

Online LV1 JS ex1

Online LV1 JS ex1. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

์ •๋‹ต๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋‚ด ์ฝ”๋“œ์™€ ๋‹ค๋ฅธ ์ 

.

//2๋ฒˆ//
  
  var height = Number(s1) / 100;
  // cm -> m ๋ณ€ํ™˜ํ•  ๋•Œ, * 0.01 ๋Œ€์‹  /100 ์œผ๋กœ ์ฒ˜๋ฆฌ
  
  var bmi = weight / (height * height);
  document.write("BMI๋Š” "  + bmi +"์ž…๋‹ˆ๋‹ค.");
  // console.log() ๋Œ€์‹  document.write() ์‚ฌ์šฉ.
  // ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ bmi๋ผ๋Š” ๋ณ€์ˆ˜์— ํ•œ๋ฒˆ ๋” ๋„ฃ์–ด์„œ ์‚ฌ์šฉ - document.write๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง.
  
//3๋ฒˆ//
 var c = (f - 32) * 5 / 9;
 document.write("ํ™”์”จ" + f  + "๋„๋Š” ์„ญ์”จ "  + c + "๋„์ž…๋‹ˆ๋‹ค.");
 // ๋‚˜์™€ ๋‹ค๋ฅธ ๊ณต์‹ ์‚ฌ์šฉ. *1.8 ๋Œ€์‹  *5/9 
 // console.log() ๋Œ€์‹  document.write() ์‚ฌ์šฉ.
 // ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ c๋ผ๋Š” ๋ณ€์ˆ˜์— ํ•œ๋ฒˆ ๋” ๋„ฃ์–ด์„œ ์‚ฌ์šฉ - document.write๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง.
  
//4๋ฒˆ//
   var s1 = prompt("์•„๋ฌด ๋ง์ด๋‚˜ ๋„ฃ์–ด ์ฃผ์„ธ์š”.");        
   var len = s1.length;
   document.write(s1 + "์˜ ๊ธธ์ด๋Š” "  + len + "์ž…๋‹ˆ๋‹ค.");
   // ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ len๋ผ๋Š” ๋ณ€์ˆ˜์— ํ•œ๋ฒˆ ๋” ๋„ฃ์–ด์„œ ์‚ฌ์šฉ - document.write๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง.