# 作業2 JavaScript 基礎練習 ### D0971395 池間祐貴 # 1.from Python to JavaScript ## 變數宣告(Declarations) javaScript有三種宣告方式 var:宣告一個可隨意更改其內容的變數 let:宣告一個可隨意更改其內容的區塊區域變數 const:宣告一個只可讀取的不可變常數 javaScript: ![](https://i.imgur.com/U8S6OPj.png) Python: ![](https://i.imgur.com/JjgQP9v.png) 基本上在javaScript code的最後面要寫「;」。 ## 輸出 javaScript的輸出方式是「console.log()」 javaScript: ![](https://i.imgur.com/dXuinBm.png) Python: ![](https://i.imgur.com/yhoxy8E.png) ## 流程控制 - 判斷式 javaScript用 if,else if 和 else。 javaScript: ![](https://i.imgur.com/ZRhARX3.png) Python: ![](https://i.imgur.com/jszsmBp.png) ## 流程控制 - 迴圈 while,for while寫法的話,大部分一樣。 但是for寫法的話,有差異。 javaScript: ![](https://i.imgur.com/NuKnW5m.png) Python: ![](https://i.imgur.com/6A0ADZc.png) javaScript與python可以用 continue 和 break。 ## 流程控制 - switch Python沒有switch javaScript: ![](https://i.imgur.com/CnM8tmI.png) # 2. Quiz 30 題 ### (1) Create a variable called carName, assign the value Volvo to it. ``` var carName = Volvo; ``` ### (2) Create a variable called x, assign the value 50 to it. ``` var x = 50; ``` ### (3) Display the sum of 5 + 10, using two variables: x and y. ``` var x = 5; var y = 10; document.getElementById("demo").innerHTML = x + y; ``` ### (4) Create a variable called z, assign x + y to it, and display the result in an alert box. ``` var x = 5; var y = 10; var z = x + y; alert(z); ``` ### (5) On one single line, declare three variables with the following names and values: ### firstName = "John" , lastName = "Doe" , age = 35 ``` var firstName = "John", lastName = "Doe" , age = 35; ``` ### (6) Multiply 10 with 5, and alert the result: ``` alert = (10 * 5); ``` ### (7) Divide 10 by 2, and alert the result: ``` alert = (10 / 2); ``` ### (8) Alert the remainder when 15 is divided by 9. ``` alert = (15 % 9); ``` ### (9) Use the correct assignment operator that will result in x being 15 (same as x = x + y). ``` x = 10; y = 5; x += y; ``` ### (10) Use the correct assignment operator that will result in x being 50 (same as x = x * y). ``` x = 10; y = 5; x *= y; ``` ### (11) Use comments to describe the correct data type of the following variables: ``` let length = 16; // Number let lastName = "Johnson"; // String const x = { firstName: "John", lastName: "Doe" }; // Object ``` ### (12) Execute the function named myFunction. ``` function myFunction() { alert("Hello World!"); } myFunction(); ``` ### (13) Create a function called "myFunction". ``` function myFunction(){ alert("Hello World!"); } ``` ### (14) Make the function return "Hello". ``` function myFunction() { return "Hello"; } document.getElementById("demo").innerHTML = myFunction(); ``` ### (15) Make the function display "Hello" in the inner HTML of an element with the ID "demo". ``` function myFunction() { document.getElementById("demo").innerHTML = "Hello"; } ``` ### (16) Alert "John" by extracting information from the person object. ``` const person = { firstName: "John", lastName: "Doe" }; alert(person.firstName); ``` ### (17) Add the following property and value to the person object: country: Norway. ``` const person = { firstName: "John", lastName: "Doe", country: "Noeway": }; ``` ### (18) Create an object called person with name = John, age = 50. Then, access the object to alert("John is 50"). ``` const person = { name: "John", age: 50 }; alert(person.name + " is " + person.age); ``` ### (19) The <button> element should do something when someone clicks on it. Try to fix it! ``` <button onclick = "alert('Hello')">Click me.</button> ``` ### (20) When the button is clicked, the function "myFunction" should be executed. ``` <button onclickn = "myFunction">Click me.</button> ``` ### (21) he <div> element should turn red when someone moves the mouse over it. ``` <div onmouseover = "this.style.backgroundColor='red'">myDIV.</div> ``` ### (22) Use the length property to alert the length of txt. ``` let txt = "Hello World!"; let x = txt.length; alert(x); ``` ### (23) Use escape characters to alert We are "Vikings". ``` let txt = "We are \"Vikings\""; alert(txt); ``` ### (24) Concatenate the two strings to alert "Hello World!". ``` let str1 = "Hello "; let str2 = "World!"; alert(str1 + str2); ``` ### (25) Convert the text into an UPPERCASE text: ``` let txt = "Hello World!"; txt = txt.toUpperCase(); ``` ### (26) Use the slice method to return the word "bananas". ``` let txt = "I can eat bananas all day"; let x = txt.slice(10,17); ``` ### (27) Use the correct String method to replace the word "Hello" with the word "Welcome". ``` let txt = "Hello World"; txt = txt.replace("Hello", "Welcome"); ``` ### (28) Convert the value of txt to upper case. ``` let txt = "Hello World"; txt = txt.toUpperCase(); ``` ### (29) Convert the value of txt to lower case. ``` let txt = "Hello World"; txt = txt.toLowerCase(); ``` ### (30) Get the value "Volvo" from the cars array. ``` const cars = ["Saab", "Volvo", "BMW"]; let x = cars[1]; ```