iT邦幫忙

0

Day 23 (Js)

  • 分享至 

  • twitterImage
  •  

1.is 開頭的大部分回傳值是booling

ex:isNaN()
https://www.w3schools.com/jsref/jsref_obj_global.asp


2.月份

  • 推算+1、指定-1

(1)取得(get)此月月份
To get the correct month, you must add 1
因為電腦從0開始計算

   console.log(d.getMonth() + 1);

(2)取得(get)星期,月份的表達
預設電腦月份會先+1
故月份的部分要減1,寫2021/6/27程式會變成2021/7/27

ex:2021.6.27

      var temp = new Date(2021, 5, 27);
      console.log(temp);
      console.log(temp.getDay());

(3)但讀取(set)皆不需要

 var d = new Date();
 d.setDate(28);  //Tue Jun 28 2021 ,用new Date()的方式輸入28顯示
 d.setDate(d.getDate() + 1); //Tue Jun 29 2021  但想知道明天,直接今天+1
 d.setMonth(d.getMonth() + 1); 若日期物件生成後,需要再加一個月時

3.錯誤的表達方式

似:烤箱沒拿出來又再烤一次
setDate(29)輸出的數值,又被在讀取一次,是讀取"過程"的感覺

      console.log(d.setDate(29)); //1624941833211

正確:
只是要知道d是甚麼

      d.setDate(29);
      console.log(d);

4.亂數

怎麼取個數 ex1-100

      var temp = Math.random(); // 0 ~ 不滿1 ex:0.7852629073191515
      temp = Math.floor(temp * 101); // 個數 看區間 就放多少
      console.log(temp); // 0 1 2 3 4 ...100

不是從1開始 ex:2-5
+從幾開始 2 3 4 5 +2

      var b = Math.random(); // 0 ~ 不滿1 ex:0.7852629073191515
      b = Math.floor(a * 4) + 2; // + 從幾開始
      console.log(b);

5.prompt輸入字串 轉 數值

使用parseInt,否則會出現字串ex:11+1=111 XD

        var x = prompt("請輸入X:");
        var y = prompt("請輸入Y:");
        var result = parseInt(x) + parseInt(y); //parseInt字串轉數字
        alert(result);
        var x = parseInt(prompt("請輸入X:")); //或是輸入時就轉數字
        var y = parseInt(prompt("請輸入Y:")); //或是輸入時就轉數字
        var result = x + y;
        alert(result);

6.function跑法

      var cnt = 0;

      function add() {
        cnt += 1; //cnt = cnt+1
        console.log(`** add() **==>  ${cnt} `);
      }

      add(); //呼叫function
      add(); //呼叫function

7.setInterval()

handle、Handle大部分都是要一個function
https://ithelp.ithome.com.tw/upload/images/20210628/20137684RUwW4HTObc.png


8.setInterval():add v.s add()

function add(){//執行很多事情}
1.add: 函式名稱
2.add():執行add

      var cnt = 0;

      function add() {
        cnt += 1; //cnt = cnt+1
        console.log(`** add() **==>  ${cnt} `);
      }
      setInterval(add, 1000);    //OK 每秒執行一次add
      setInterval(add(), 1000);  //不OK?! 執行add function 沒return 故只執行一次

9.function的跑法 影片Js03 1:53


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言