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

NCC-JavaScript-Week#2

Irene Chang
October 26, 2015

 NCC-JavaScript-Week#2

NTUST Coding Club JavaScript Course Week#2 (2015.10.23)
For self-study only.

Modified on 2015/10/26

Irene Chang

October 26, 2015
Tweet

More Decks by Irene Chang

Other Decks in Education

Transcript

  1. ‣ 醳绢㛇燊铃岁♧ ˋ ‣ 㛇燊铃岁✳  须俲㘗䢀ꤏ⴪  崩玑䱾ⵖ哭⟝䒭JGFMTF FMTFJG

     簮䱖ㄤ⼦㝆 ‣ 箻绢✳JGFMTF ˋ ‣ 㛇燊铃岁♲  崩玑䱾ⵖ鶬㕖GPS ‣ 箻绢♲GPS  崩玑䱾ⵖ鶬㕖XIJMF ‣ 箻绢♲XIJMF GPS ‣ 䮋䨞겗 "HFOEB
  2. 1 var name = prompt(); 2 console.log(name); 3 var age

    = prompt("What’s your age?"); 4 console.log("Your age is " + age); 鱒Ⰵ 鱒Ⰵ涸⧩鿪僽㶶⚮ 銴⨞Ⱖ➮欽鸁㽠꨾銴鱲䳖㘗䢀
  3. 1 var myName = "Irene"; 2 console.log("I am " +

    myName); 3 var myName = "Ethan"; 4 console.log("I am " + myName); ꅾ醳㹒デ僽搂㹳涸剚⟃剓倞涸捀⚺ "I am Irene" "I am Ethan"
  4. 1 var a = 1; //數字 2 var b =

    0.01; //數字 3 var c = 'JavaScript'; //字串 4 var d = "JavaScript"; 5 //單引號、雙引號都可以表⽰示字串 6 var e = true; //布林值 7 var f = false; //布林值 8 var g = null; //無值 9 var h = undefined; //未定義
  5. 1 typeof "John" // Returns 'string' 2 typeof 3.14 //

    Returns 'number' 3 typeof false // Returns 'boolean' 4 typeof [1,2,3,4] // Returns 'object' 5 typeof {name:'John', age:34} 6 // Returns 'object' 㘗ⴽ焷钢 ‣ typeof
  6. 皿遯麌皿㶩"SJUINFUJD0QFSBUPST ꤑ✫㛇劥涸麌皿㶩⛓㢫+BWB4DSJQUⰉ䒊.BUI 暟⟝暶䚍涸GVODUJPO䲿⣘⡹刿䓽㣐涸侸㷸麌皿  1 Math.pow(2,3); //Returns 8, 2的3次⽅方 2

    Math.round(0.6); //Returns 1, 四捨五⼊入 3 Math.abs(-10); //Returns 10, 取絕對值 4 Math.max(1,20,30); //Returns 30, 取最⼤大值 5 Math.min(1,20,30) //Returns 1, 取最⼩小值 6 Math.random() //取介於0到1之間的亂數
  7. 䭸崣麌皿㶩"TTJHONFOU0QFSBUPST &YQSFTTJPO &YBNQMF 4BNF"T x = y x = y

    += x += y x = x + y -= x -= y x = x - y *= x *= y x = x * y /= x /= y x = x / y %= x %= y x = x % y =
  8. 齅鱀麌皿㶩-PHJDBM0QFSBUPST ⚂"OE 䧴0S 1 false || false; /* is false

    2 其餘皆是 true */ や/PU 1 !true; // is false 2 !false; // is true 1 true && true; /* is true 2 其餘皆是 false
  9. 鏽鍑$PNNFOU ⴕ䧭㋲遤鏽鍑 // ㄤ㢵遤鏽鍑 /* */  1 // 我是單⾏行註解

    2 /* 我是多⾏行註解 3 我可以有好幾⾏行 4 可以有⾮非常多⾏行 5 要記得結尾唷 */
  10. ‣ 䖎㢵匌銯佞㖈♧饱꧌⚥盘椚 ꤏ⴪"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 = [];
  11. 1 var mixed = ["JS",123,"Scratch"]; 2 console.log(mixed[0] == "JS"); //

    true 3 console.log(mixed[1]); // 123 ꤏ⴪"SSBZ ‣ 嫦♧갪鿪剤♧⦐管贫*OEFY ‣ 管贫䖰Ꟛ㨥CBTFEJOEFYJOH
  12. 1 var names = "JavaScript"; 2 console.log(names[2] == "v"); //

    true 3 console.log(names[4]); // "S" 酢⯏ 㶶⚮⛳剤JOEFY涸嚌䙂
  13. 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
  14. 1 var fruits =[1, 3, 2, "Orange", "Apple"]; 2 fruits.sort();

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

    3 fruits.pop(); //刪除最後⼀一項 “Apple" 4 console.log(fruits); // ["Orange"] 4IJGUJOH1PQQJOH ꤏ⴪"SSBZ
  16. 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
  17. JG FMTF 1 if(condition) 2 { 3 //若符合條件,就執⾏行這段{}裡⾯面的程式碼 4 }

    5 else{ 6 //不符合條件,就執⾏行這段{}裡⾯面的程式碼 7 }
  18. JG FMTF 1 var total = Number(prompt("數⼊入總⾦金額:")); 2 if(total >

    1000){ 3 console.log("恭喜你獲得9折優惠!你所要⽀支付的 ⾦金額為:" + total*0.9); 4 } 5 else{ 6 console.log("你所要⽀支付的⾦金額為:" + total); 7 }
  19. JG FMTFJG FMTF 1 if(condition) 2 { 3 //若符合條件,就執⾏行這段{}裡⾯面的程式碼 4

    } 5 else if(condition) 6 { 7 /*不符合上⼀一個條件但符合這⼀一個條件, 8 就執⾏行這段{}裡⾯面的程式碼*/ 9 } 10 else{ 11 //其他條件都不符合,就執⾏行這段{}裡⾯面的程式碼 12 }
  20. var total = (“數⼊入原⾦金額:”); if(total >= 1000){ console.log("恭喜你獲得7折優惠!你所要⽀支付的⾦金額為:" + total

    ); } (total < 1000 total >= 700){ console.log("恭喜你獲得8折優惠!你所要⽀支付的⾦金額為: " + total ); } (total < 700 total >= 400){ console.log("恭喜你獲得9折優惠!你所要⽀支付的⾦金額為: " + total ); } { console.log("你所要⽀支付的⾦金額為: " + total); }
  21. 1 var total =Number((prompt(“數⼊入總⾦金額:”)); 2 if(total >= 1000){ 3 console.log("恭喜你獲得7折優惠!你所要⽀支付的⾦金額為:"

    + 4 total*0.7); 5 } 6 else if(total < 1000 && total >= 700){ 7 console.log("恭喜你獲得8折優惠!你所要⽀支付的⾦金額為: " + 8 total*0.8); 9 } 10 else if(total < 700 && total >= 400){ 11 console.log("恭喜你獲得9折優惠!你所要⽀支付的⾦金額為: " + 12 total*0.9); 13 } 14 else{ 15 console.log("你所要⽀支付的⾦金額為: " + total); 16 }
  22. 1 var total =(prompt("數⼊入總⾦金額:"); 2 if(total >= 1000){ 3 console.log("恭喜你獲得7折優惠!你所要⽀支付的⾦金額為:"

    + 4 total*0.7); 5 } 6 else if(total < 1000 && total >= 700){ 7 console.log("恭喜你獲得8折優惠!你所要⽀支付的⾦金額為: " + 8 total*0.8); 9 } 10 else if(total < 700 && total >= 400){ 11 console.log("恭喜你獲得9折優惠!你所要⽀支付的⾦金額為: " + 12 total*0.9); 13 } 14 else{ 15 console.log("你所要⽀支付的⾦金額為: " + total); 16 } 縮排⽅方便閱讀,流程控制、function... Tab 或 Space,不要混⽤用
  23. 1 var total =(prompt("數⼊入總⾦金額:"); 2 if(total >= 1000){ 3 console.log("恭喜你獲得7折優惠!你所要⽀支付的⾦金額為:"

    + 4 total*0.7); 5 } 6 else if(total < 1000 && total >= 700){ 7 console.log("恭喜你獲得8折優惠!你所要⽀支付的⾦金額為: " + 8 total*0.8); 9 } 10 else if(total < 700 && total >= 400){ 11 console.log("恭喜你獲得9折優惠!你所要⽀支付的⾦金額為: " + 12 total*0.9); 13 } 14 else{ 15 console.log("你所要⽀支付的⾦金額為: " + total); 16 } 區塊定義作⽤用域,流程控制、function... 記得⼤大括號要成對
  24. " 宠#.*⧩鹎ꥣ晝 1. 請使⽤用者輸⼊入⾝身⾼高和體重 2. 輸出 BMI 值
 hint: BMI=體重(kg)

    / ⾝身⾼高2(m2). 3. 判斷使⽤用者過輕、標準或過重
 hint: 過輕<18.5<=標準<24<=過重 IUUQTKTCJODPNGPNPZPFEJU KT DPOTPMF
  25. # ⶩⴉ瀖걧䋒 1. 請玩家輸⼊入剪⼑刀、⽯石頭或布 2. 設定電腦隨機出拳
 hint: var computer =

    Math.random(); 3. 判斷玩家勝負(贏、輸、平⼿手) IUUQTKTCJODPNGVIJYPFEJU KT DPOTPMF
  26. $ ⴼ倬♲錬䕎 1. 設定三⾓角形的三個邊 2. ⽐比較邊與邊的關係,判斷三⾓角形是銳⾓角、 鈍⾓角還是直⾓角三⾓角形
 hint: Math.pow(x,y)、雙層判斷 var

    number = prompt("輸⼊入三個數字,⽤用空⽩白隔開:").split(" "); for (var i = 0; i < number.length; i++){ number = number.sort(function(a, b){return a-b;}); 
 // 修正 bug } number.sort(); console.log(number); IUUQTKTCJODPNCJCVFEJU KT DPOTPMF
  27. GPS 1 for(var num = 1; num <= 100; num++){

    2 console.log(num); 3 } 1 for(var num = 1; num <= 100; num = num + 1){ 3 console.log(num); 4 }
  28. GPS 1 for(var num = 1; num <= 100; num

    += 2){ 2 console.log(num); 3 } 1 for(var num = 100; num >= 1; num--){ 2 console.log(num); 3 }
  29. var result = []; for( ; ; ){ var count

    = []; var sum = 0; for( ; ; ){ if( === ){ count. ( ); } } for( ; < count.length; ){ sum count[ ]; } if( === ){ result. ( ); } } console.log('500以內的完全數為:', );
  30. 1 var result = []; 2 for( var i =

    1; i <= 500; i++){ 3 var count = []; 4 var sum = 0; 5 6 for( var j = 1; j < i ; j++){ 7 if( i % j === 0 ){ 8 count.push(j); 9 } 10 } 11 12 for( var n = 0; n < count.length; n++ ){ 13 sum += count[n]; 14 } 15 16 if( i === sum){ 17 result.push(i); 18 } 19 } 20 console.log('500以內的完全數為:', result);
  31. " 宠㔔侸 1. 請使⽤用者輸⼊入任意數字 2. 使⽤用 for 和 if 判斷該數字的因數


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

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

    的倍數就輸出 Fizz 3. 如果是 5 的倍數就輸出 Buzz 4. 如果同時是 3 和 5 的倍數就輸出 FizzBuzz
 hint:雙層迴圈 IUUQTKTCJODPNXPLVTJFEJU KT DPOTPMF
  34. var secret = Math.floor((Math.random()*100)+1); ( ){ var guess = (

    "猜⼀一個介於1~100的數字:" ); if( == ){ console.log( '猜對了!' ); break; } ( ){ console.log( '猜太⼤大了!' ); } { console.log( '猜太⼩小了!' ); } } //noprotect