# from C to JavaScript ## 運算子 ### C ``` #include<stdio.h> int main(void){ int x = 4; int y = 3; int a = x + y; int b = x - y; int c = x * y; int d = x / y; int e = x % y; printf("%d\n",a); printf("%d\n",b); printf("%d\n",c); printf("%d\n",d); printf("%d\n",e); } ``` C語言需要寫#include<stdio.h> javascript不用寫 int・・・變數的宣告的時候使用.只有輸入的數字是整數的時候可以用. (int x = 4、int y = 3 等等) \n・・・新增一行的意思 printf・・・顯示在螢幕上()內的文字的時候使用. +・・・加法    -・・・減法    *・・・乘法 /・・・除法     %・・・模數除法 javascript 也一樣的方法 ### javascript ``` var x = 4; var y = 3; var a = x + y; var b = x - y; var c = x * y; var d = x / y; var e = x % y; console.log(a); console.log(b); console.log(c); console.log(d); console.log(e); ``` var・・・變數的宣告的時候使用.輸入的是都可以. (var a = x + y、var x = 4、var z = "Hello,World!" 等等) console.log()・・・表示()內的文字,變數的值 後面需要「;」、C語言也需要 # 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"; } ``` ### (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: "Norway" }; ``` ### (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 onclick = "myFunction()">Click me.</button> ``` ### (21)The < 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]; ```