Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NCC-JavaScript-Week#3

Irene Chang
October 28, 2015

 NCC-JavaScript-Week#3

NTUST Coding Club JavaScript Course Week#3 (2015.11.06)
For self-study only.

Irene Chang

October 28, 2015
Tweet

More Decks by Irene Chang

Other Decks in Education

Transcript

  1. ‣ 醳绢㛇燊铃岁✳։♲ ‣ 箻绢♲GPS JGFMTF ˋ ‣ 㛇燊铃岁♲ Ը 崩玑䱾ⵖ鶬㕖XIJMF

    ‣ 箻绢㔋XIJMF GPS ‣ 䮋䨞겗 ˋ ‣ 㛇燊铃岁㔋 Ը ⴅ䒭GVODUJPO ˋ ‣ 㛇燊铃岁❀ Ը 须俲㘗ⴽ暟⟝ "HFOEB
  2. ‣ 䖎㢵匌銯佞㖈♧饱꧌⚥盘椚 ꤏ⴪"SSBZ 1 var names = ["JS","Python","Scratch"]; 2 var

    number = [1, 3.14, 7749, 0.001]; 3 var mixed = [1, "Irene", 2015, "Hi"]; 4 var empty = [];
  3. 1 var mixed = ["JS",123,"Scratch"]; 2 console.log(mixed[0] == "JS"); //

    true 3 console.log(mixed[1]); // 123 ꤏ⴪"SSBZ ‣ 嫦♧갪鿪剤♧⦐管贫*OEFY ‣ 管贫䖰Ꟛ㨥CBTFEJOEFYJOH ‣ 㶶⚮⛳剤JOEFY涸嚌䙂
  4. 1 var doubleArray =[[1,2],["a","b"]]; 2 console.log(doubleArray[0]);// [1,2] 3 console.log(doubleArray[1][1]); //

    "b" ꧱㾵ꤏ⴪UXPEJNFOTJPOBMBSSBZ ꤏ⴪"SSBZ 4 console.log(doubleArray[1][2]); // undefined
  5. 1 var fruits =[1, 3, 2, "Orange", "Apple"]; 2 fruits.sort();

    //按照數字⼤大⼩小、字⺟母順序排列 3 fruits.reverse(); //按照數字⼤大⼩小、字⺟母倒序排列 4PSUJOH3FWFSTJOH ꤏ⴪"SSBZ
  6. 1 var fruits =["Mango", "Orange", "Apple"]; 2 fruits.shift(); //刪除第⼀一項 "Mango"

    3 fruits.pop(); //刪除最後⼀一項 “Apple" 4 console.log(fruits); // ["Orange"] 4IJGUJOH1PQQJOH ꤏ⴪"SSBZ
  7. 1 var fruits =["Orange", "Apple"]; 2 fruits.unshift("Kiwi"); //在第⼀一項加⼊入"Kiwi" 3 fruits.push("Lemon");

    //在最後⼀一項加⼊入”Lemon" 4 console.log(fruits); 5 // ["Kiwi","Orange","Apple","Lemon"] 6OTIJGUJOH1VTIJOH ꤏ⴪"SSBZ
  8. JG FMTF 1 if(condition) 2 { 3 //若符合條件,就執⾏行這段{}裡⾯面的程式碼 4 }

    5 else{ 6 //不符合條件,就執⾏行這段{}裡⾯面的程式碼 7 }
  9. JG FMTFJG FMTF 1 if(condition) 2 { 3 //若符合條件,就執⾏行這段{}裡⾯面的程式碼 4

    } 5 else if(condition) 6 { 7 /*不符合上⼀一個條件但符合這⼀一個條件, 8 就執⾏行這段{}裡⾯面的程式碼*/ 9 } 10 else{ 11 //其他條件都不符合,就執⾏行這段{}裡⾯面的程式碼 12 }
  10. " 宠㔔侸 1. 請使⽤用者輸⼊入任意數字 2. 使⽤用 for 和 if 判斷該數字的因數


    hint: % 取餘數=0 3. 輸出該數字之因數(包含1與本⾝身) IUUQTKTCJODPNYJEPIJFEJU KT DPOTPMF
  11. # 宠Ⰽ侸剓㣐Ⱇ㔔侸 1. 請使⽤用者輸⼊入兩個數字,以空⽩白隔開 2. 轉換型態、建⽴立空 array 3. 使⽤用 for

    和 if 判斷兩數最⼤大公因數
 hint: Math.max(), .push() IUUQTKTCJODPN[FCJWJFEJU KT DPOTPMF
  12. $ 'J[[#V[[ 1. 輸出 1 到 100 2. 如果是 3

    的倍數就輸出 Fizz 3. 如果是 5 的倍數就輸出 Buzz 4. 如果同時是 3 和 5 的倍數就輸出 FizzBuzz
 hint:雙層迴圈 IUUQTKTCJODPNXPLVTJFEJU KT DPOTPMF
  13. 1 for (var ; ; ) { 2 var isPrime

    = true; 3 for(var ; ; ) { 4 if ( === ) { 5 isPrime = ; 6 break; 7 } 8 } 9 if ( ) { 10 console.log( ); 11 } 12 } 鱒ⴀ_涸颶侸
  14. 䒊用♧⦐ꤏ⴪ 佞Ⰵ_涸颶侸 鱒ⴀ痧_갪 1 var prime =[]; 2 for( ;

    ; ){ 3 for( ; ; ){ 4 if( ){ 5 break; 6 } 7 else{ 8 prime. ( ); 9 break; 10 } 11 } 12 } 13 14 for( ; ; ){ 15 console.log( ); 16 }
  15. var secret = Math.floor((Math.random()*100)+1); ( ){ var guess = (

    "A number between 1 and 100" ); if( == ){ console.log( 'You got it!' ); break; } ( ){ console.log( 'Smaller' ); } { console.log( 'Bigger' ); } } //noprotect
  16. 1 function fnName(x, y) { 2 // Do something 3

    }; Ⱘそⴅ侸 1 var fnName = function(x, y) { 2 // Do something 3 }; ⼣そⴅ侸 ⴅ䒭GVODUJPO
  17. 1 function add(x, y) { 2 var total = x

    + y; 3 console.log(total); 4 }; 5 6 add(3,4); // 輸出7 ㄎ〭 ⴅ䒭GVODUJPO
  18. 1 function add(x, y) { 2 var total = x

    + y; 3 return total ; // 回傳運算後的值 4 }; 5 add(3,4); // 直接呼叫沒有意義 6 var result = add(3,4); // 把值存起來 7 console.log(result * 10); // 輸出70 鸒䌢䧮⦛꨾銴GVODUJPO㔐⫄㛂遤䖕涸穡卓⢵⨞Ⱖ➮✲ ⴅ䒭GVODUJPO
  19. 1 function square(num) { 2 var result = num *

    num; 3 return result ; // 回傳運算後的值 4 }; 5 console.log(square(10)); // 輸出100 ⿮侸〳⟃⟤䠑ㄐそ⡎僽ⴽ䒚ⵌ鸮荈䊹鿪溏♶䥢 ⴅ䒭GVODUJPO
  20. 1 function countNumber() { 2 for(var i = 0; i

    <= 100; i++){ 3 console.log(i); 4 }; 5 }; 6 countNumber(); GVODUJPO♶♧㹁銴剤⿮侸《对⡹䟝銴麨䧭涸湡涸 ⴅ䒭GVODUJPO 參數的數量沒有實際上的限制,但還是盡量控制在 2~3 個左右
  21. 1 function areaBox(width, length) { 2 var result = width*length;

    3 return result; 4 // 在 {} 裡的每⼀一⾏行⼀一定要⽤用 ; 結尾 5 }; 6 console.log(areaBox(12,24)); //288 ⴕ贫䖎ꅾ銴 ⴅ䒭GVODUJPO
  22. 1 var objName = { 2 key: value, 3 key:

    value, 4 key: function(){ 5 //Do Something 6 }; 7 }; " 暟⟝0CKFDU
  23. 1 var objName = { 2 key: value, 3 key:

    value, 4 key: function(){ 5 //Do Something 6 }; 7 }; 〳⟃䪾㿂䚍QSPQFSUZ 䟝䧭僽꣡㿂倴鑪暟⟝涸 隶侸欽⢵䲾鶤鑪暟⟝ 涸暶颶 " 暟⟝0CKFDU
  24. 1 var objName = { 2 key: value, 3 key:

    value, 4 key: function(){ 5 //Do Something 6 }; 7 }; 㿂䚍QSPQFSUZ  㶶⚮♶欽䒸贫  侸㶶  倰岁.FUIPE 岤䠑ㄐそ鿪剤ⴕ㣐㼭㻨㿂䚍莅㿂䚍⛓꟦欽鸊럊ⴕꟚ " 暟⟝0CKFDU
  25. 1 var myself = { 2 name: "Irene", 3 age:

    19, 4 calling: function(){ 5 console.log('Calling to' + this.name + 'at' + this.phone); 6 }, 7 }; " 暟⟝0CKFDU
  26. 1 function test(){ 2 console.log("JavaScript"); 3 } 4 test(); //

    呼叫函式 5 var objTest = { 6 test: function(){ 7 console.log("JavaScript"); 8 } 9 }; 10 objTest.test(); //呼叫⽅方法 ⴅ䒭'VODUJPOWT倰岁.FUIPE