Basic JavaScript 030
01-Comment your JavaScript Code
// inline comment
/**
* block comment
*
*
*/
02-Declare JavaScript Variables
宣告變數
可以直接從七種資料型態宣告
操作指向數據的指針,而不是資料表身
或直接使用 var
// Example
var ourName;
var myName;
// Define myName below this line
03-Storing Values with the Assignment Operator
// Setup
var a;
var b = 2;
// Only change code below this line
a = 7;
b = a;
04-Initializing Variables with the Assignment Operator
使用
=
給變數一個初始值
// Example
var ourVar = 19;
// Only change code below this line
var a = 9;
05-Understanding Uninitialized Variables
只有宣告變數的話,初始值為 undefined
如果對 undefined 變數做數學運算,結果會是 NaN,代表 not a number
// Initialize these three variables
var a=5;
var b=10;
var c="I am a";
// Do not change code below this line
a = a + 1;
b = b + 5;
c = c + " String!";
06-Understanding Case Sensitivity in Variables
JS 的變數對大小寫敏感
但不要使用這個特性,會搞混
推薦駝峰命名法 camelCase
變數的第一個字母小寫,隨後單字的第一個字母大寫
// Declarations
var studlyCapVar;
var properCamelCase;
var titleCaseOver;
// Assignments
studlyCapVar = 10;
properCamelCase = "A String";
titleCaseOver = 9000;
07-Add Two Numbers with JavaScript
var sum = 10 + 10;
08-Subtract One Number from Another with JavaScript
var difference = 45 - 33;
09-Multiply Two Numbers with JavaScript
var product = 8 * 10;
10-Divide One Number by Another with JavaScript
var quotient = 66 / 33;
11-Increment a Number with JavaScript
i++
var myVar = 87;
// Only change code below this line
myVar++;
12-Decrement a Number with JavaScript
i--
var myVar = 11;
// Only change code below this line
myVar--;
13-Create Decimal Numbers with JavaScript
var ourDecimal = 5.7;
// Only change code below this line
var myDecimal = 5.7;
14-Multiply Two Decimals with JavaScript
var product = 2.0 * 2.5;
15-Divide one Decimal by Another with JavaScript
Divide one Decimal by Another with JavaScript
16-Finding a Remainder in JavaScript
Example 5 % 2 = 1 because Math.floor(5 / 2) = 2 (Quotient) 2 * 2 = 4 5 - 4 = 1 (Remainder)
// Only change code below this line
var remainder = 11 % 3 ;
17-Compound Assignment With Augmented Addition
var a = 3;
var b = 17;
var c = 12;
// Only modify code below this line
a += 12;
b += 9;
c += 7;
18-Compound Assignment With Augmented Subtraction
var a = 11;
var b = 9;
var c = 3;
// Only modify code below this line
a -= 6;
b -= 15;
c -= 1;
19-Compound Assignment With Augmented Multiplication
var a = 5;
var b = 12;
var c = 4.6;
// Only modify code below this line
a *= 5;
b *= 3 ;
c *= 10;
20-Compound Assignment With Augmented Division
var a = 48;
var b = 108;
var c = 33;
// Only modify code below this line
a /= 12;
b /= 4;
c /= 11;
21-Convert Celsius to Fahrenheit
function convertToF(celsius) {
var fahrenheit;
// Only change code below this line
fahrenheit = celsius * (9/5) + 32;
// Only change code above this line
return fahrenheit;
}
// Change the inputs below to test your code
convertToF(30);
22-Declare String Variables
// Example
var firstName = "Alan";
var lastName = "Turing";
// Only change code below this line
var myFirstName = "Chiu";
var myLastName = "Chi Wei";
23-Escaping Literal Quotes in Strings
var myStr; // Change this line
myStr = "I am a \"double quoted\" string inside \"double quotes\".";
24-Quoting Strings with Single Quotes
這功能好神奇,這樣不需要 back slash、反斜杠來跳脫字元了
var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
25-Escape Sequences in Strings
超詭異的用法 ==
Code Output
\' 单引号 single quote
\" 双引号 double quote
\\ 反斜杠符 backslash
\n 换行符 newline
\r 回车符 carriage return
\t 制表符 tab
\b 退格符 backspace
\f 换页符 form feed
var myStr; // Change this line
myStr = 'FirstLine\n\\SecondLine\\\rThirdLine';
26-Concatenating Strings with Plus Operator
// Example
var ourStr = "I come first. " + "I come second.";
// Only change code below this line
var myStr = "This is the start. " + "This is the end.";
27-Concatenating Strings with the Plus Equals Operator
// Example
var ourStr = "I come first. ";
ourStr += "I come second.";
// Only change code below this line
var myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";
28-Constructing Strings with Variables
// Example
var ourName = "Free Code Camp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
// Only change code below this line
var myName = "邱繼緯";
var myStr = "My name is" + myName + " and I am swell";
29-Appending Variables to Strings
// Example
var anAdjective = "awesome!";
var ourStr = "Free Code Camp is ";
ourStr += anAdjective;
// Only change code below this line
var someAdjective = "酷喔喔";
var myStr = "Learning to code is ";
myStr += someAdjective;
30-Find the Length of a String
// Example
var firstNameLength = 0;
var firstName = "Ada";
firstNameLength = firstName.length;
// Setup
var lastNameLength = 0;
var lastName = "Lovelace";
// Only change code below this line.
lastNameLength = lastName.length;
Last updated
Was this helpful?