JAVASCRIPT

[JAVASCRIPT] ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ๊ฐ์ฒด - Math()

Arr_ 2021. 10. 31. 14:54

 

  • Math()์— ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ

 

     - .PI : ํŒŒ์ด

     - .E : ์ž์—ฐ ์ƒ์ˆ˜ e

     - .floor() : ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆผ

     - .ceil() : ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ์˜ฌ๋ฆผ

     - .abs() : ์ ˆ๋Œ€๊ฐ’ 

     - .max() : ์ตœ๋Œ€๊ฐ’

     - .min() : ์ตœ๊ณ ๊ฐ’

     - .random() : 0.0 <= x < 1 ์˜ ์ž„์˜์˜ ์ˆซ์ž

     - .sqrt() : ์ œ๊ณฑ๊ทผ

     - .random() * ์ˆซ์ž : 0 ~ ์ˆซ์ž์‚ฌ์ด์˜ ์ž„์˜์˜ ์ˆซ์ž 

 

  <body>
    <script>
      document.write(Math.PI, "<br>");
      document.write(Math.E, "<br>");
      document.write(Math.floor(3.14159), "<br>");
      document.write(Math.ceil(2.123), "<br>");
      document.write(Math.abs(-3), "<br>");
      document.write(Math.max(5, 6, 9, 7), "<br>");
      document.write(Math.min(5, 6, 7, 9), "<br>");
      document.write(Math.random(), "<br>"); //0.0 <= x < 1
      document.write(Math.sqrt(36), "<br>");
      document.write("<br>");
      document.write(Math.random() * 10, "<br>");
      document.write(Math.floor(Math.random() * 11), "<br>"); //0~10 ์ž„์˜์˜ ์ˆซ์ž
      document.write(Math.floor(Math.random() * 31), "<br>"); //0~30 ์ž„์˜์˜ ์ˆซ์ž
      document.write(Math.floor(Math.random() * 31) + 120, "<br>"); //120~150 ์ž„์˜์˜ ์ˆซ์ž
    </script>
  </body>

 

 

- ๋ธŒ๋ผ์šฐ์ € ์ถœ๋ ฅ

 

 


 

  • Math() ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ ๊ตฌํ•˜๊ธฐ

 

  <body>
    <script>
      // ์ด๋ฒˆ์ฃผ ๋กœ๋˜ ๋ฒˆํ˜ธ
      // ๋กœ๋˜ ๋ฒˆํ˜ธ 6๊ฐœ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ฐฐ์—ด์— ๋‹ด์€ ํ›„ ์ถœ๋ ฅํ•˜๊ธฐ
      // 1~45
      
      let lottoArray = new Array();

      while (true) {
        let num = Math.floor(Math.random() * 45) + 1;
        lottoArray.push(num);
        if (lottoArray.length > 5) break;
      }
      document.write("์ด๋ฒˆ ์ฃผ ๋กœ๋˜ ๋ฒˆํ˜ธ : ", lottoArray);
    </script>
  </body>

 

- floor๋กœ ํ•œ๋ฒˆ ๊ฐ์‹ธ์•ผ์ง€ ์†Œ์ˆ˜์ ์ด ๋‚˜์˜ค์ง€ ์•Š์Œ

 

 

 

- ๋ธŒ๋ผ์šฐ์ € ์ถœ๋ ฅ