FreeCodeCamp: Basic JavaScript01 - zilongxuan001/LearnFreecode GitHub Wiki

回Basic JavaScript目录

Lesson: Comment your JavaScript Code

JavaScirpt的注释有两种

单行注释

举例

// This is an in-line comment.

多行注释

举例 /This is a multi-line commnet/

Lesson: Declare JavaScript Variables

介绍

在计算机科学中,数据data是非常有意义的。

JavaScript提供七种不同的数据类型data types,分别是undefinednullbooleanstringsymbolnumberobject

举例来说,计算机能够区别数字number(如12)和字符串string(如"12","dog",“123 cats”)。字符串是字符的集合。计算机能够对一个数字进行数学运算,却不能对字符串这样做。

变量Varibles,和数学的xy比较类似,能够代表我们想指代的数字。计算机的Varibles和数学变量区别就在于前者能够在不同时期储存不同的值values。

方法

JavaScript可以在变量前加一个var,从而创造或者说声称declare一个变量,像这样

var ourName;

这就创造一个variableourName

在JavaScript中,我们用英文分号结束一个语句。

变量Variable可以由数字、字母以及$或者_组成,但不能包含空格和用数字开头。

练习

创造一个变量

代码


// Example
var ourName;

// Define myName below this line
var myName;

来源

https://www.freecodecamp.org/challenges/declare-javascript-variables

Lesson: Storing Values with the Assignment Operator

介绍

在JavaScript,你可以通过赋值运算符(assignment operator)为变量储存值。

方法

例如

myVariable=5;

就是把Number5赋值给了myVariable.

赋值通常是从右边到左边。

myVar=5;
myNum=myVar;

5被赋值给myVar,又通过myVar分配给myNum

练习

把7赋值给a,

把a赋值给b

代码


// Setup
var a;
var b = 2;

// Only change code below this line
a=7;

b=a;


浏览器显示

image

来源

https://www.freecodecamp.org/challenges/storing-values-with-the-assignment-operator

Lesson: Initializing Variables with the Assignment Operator

介绍

初始化initialize一个变量就是在声称变量的同一行里给一个初始值。

方法

var myVar=0;

这样,就创造了一个新变量,名称叫myVar,分配制度初始值是0

练习

定义一个变量a,并初始化,值为9

代码

var a=9;

浏览器显示

image

来源

https://www.freecodecamp.org/challenges/initializing-variables-with-the-assignment-operator

Lesson: Understanding Uninitialized Variables

介绍

当JavaScript声称一个变量时,变量有一个初始值,为undefined

如果你对undefined变量做数学运算,结果则为NaN,意思为Not a Number

如果你用undefined变量连接一个字符串,你会得到一个undefined的文字字符串。

练习

给a赋值5,给b赋值6,给c赋值为"I am a"。

赋值前

代码为 image

浏览器显示 image

赋值后

代码为

// 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!";

浏览器显示

image

来源

https://www.freecodecamp.org/challenges/understanding-uninitialized-variables

Lesson: Understanding Case Sensitivity in Variables

介绍

在JavaScript中,所有的变量variables和功能名称functions names都是大小写敏感的。

变量MYVAR不是变量MyVar,也不是变量myvar

方法

我们使用驼峰命名法camelCase。在驼峰命名法中,多单词变量的第一个单词小写,其后的每个单词首字母大写。 举例

var someVariable;
var anotherVariableName;
var thisVariableNameIsTooLong;

练习

使用驼峰命名法修改变量名称。

代码


// Declarations
var studlyCapVar;
var properCamelCase;
var titleCaseOver;

// Assignments
studlyCapVar = 10;
properCamelCase = "A String";
titleCaseOver = 9000;

浏览器显示

image

来源

https://www.freecodecamp.org/challenges/add-two-numbers-with-javascript

Lesson: Add Two Numbers with JavaScript

介绍

Number是JavaScript的信息类型,表示为数字。

通过+符号可以实现数字的相加。

方法

myVar = 5+10; //assigned 15

练习

var sum = 10 + 10;

浏览器显示

image

来源

https://www.freecodecamp.org/challenges/add-two-numbers-with-javascript

Lesson: Subtract One Number from Another with JavaScript

介绍

使用-可以做数字的减法运算。 #163 加法运算

方法

myVar=12-6; //assigned 6

练习

var difference = 45 - 33;

浏览器显示

image

来源

https://www.freecodecamp.org/challenges/subtract-one-number-from-another-with-javascript

Lesson: Multiply Two Numbers with JavaScript

介绍

* 符号可以让数字相乘。

方法

myVar =13*13; //assigned 169

练习

var product = 8 * 10;

浏览器

image

来源

https://www.freecodecamp.org/challenges/multiply-two-numbers-with-javascript

Lesson: Divide One Number by Another with JavaScript

介绍

/作为除号。

方法

myVar =16/2; // assigned 8

练习


var quotient = 66 / 33;

浏览器

image

来源

https://www.freecodecamp.org/challenges/divide-one-number-by-another-with-javascript

Lesson: Increment a Number with Javascript

介绍

使用++可自动加1

方法

i++;相当于i=i+1;

练习

var myVar = 87;
// Only change code below this line
myVar++;

浏览器

image

来源

https://www.freecodecamp.org/challenges/increment-a-number-with-javascript

Lesson: Decrement a Number with Javascript

介绍

使用--可以减去1

方法

i--;相当于i=i-1;

练习

var myVar = 11;
// Only change code below this line
myVar--;

浏览器

image

来源

https://www.freecodecamp.org/challenges/decrement-a-number-with-javascript

Lesson: Create Decimal Numbers with JavaScript

介绍

可以用储存小数(decimal numbers)。小数有时被称为浮点数(floating point).

方法

var ourDecimal = 5.7;

练习

var myDecimal =6.8;

浏览器

image

来源

https://www.freecodecamp.org/challenges/create-decimal-numbers-with-javascript

Lesson: Multiply Two Decimals with JavaScript

介绍

小数相乘

练习

var product = 2.0 * 2.5;

浏览器

image

来源

https://www.freecodecamp.org/challenges/multiply-two-decimals-with-javascript

Lesson: Divide one Decimal by Another with JavaScript

介绍

小数可以相除。

练习

var quotient = 4.4 / 2.0;

浏览器

image

来源

https://www.freecodecamp.org/challenges/divide-one-decimal-by-another-with-javascript

Lesson: Finding a Remainder in Javascript

介绍

求余数的运算符%可以两个数相除的余数。

方法

比如

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;

浏览器

image

来源

https://www.freecodecamp.org/challenges/finding-a-remainder-in-javascript

Lesson: Compound Assignment With Augmented Addition

介绍

可以使用+=实现变量自加。

方法

myVar+=5;相当于myVar=myVar+5;

练习

···

var a = 3; var b = 17; var c = 12;

// Only modify code below this line

a +=12; b +=9; c +=7; ···

浏览器

image

来源

https://www.freecodecamp.org/challenges/compound-assignment-with-augmented-addition

Lesson: Compound Assignment With Augmented Subtraction

介绍

使用-=实现变量自减

方法

myVAR-=5;相当于myVar=myVar-5;

练习


var a = 11;
var b = 9;
var c = 3;

// Only modify code below this line

a -=6;
b -=15;
c -=1;

浏览器

image

来源

https://www.freecodecamp.org/challenges/compound-assignment-with-augmented-subtraction

Lesson: Compound Assignment With Augmented Multiplication

介绍

使用*=复合分配符,可以实现变量的自乘

方法

myVar*=5相当于myVar=myVar*5

练习

var a = 5;
var b = 12;
var c = 4.6;

// Only modify code below this line

a *=5;
b *=3;
c *=10;

浏览器

image

来源

https://www.freecodecamp.org/challenges/compound-assignment-with-augmented-multiplication

Lesson: Compound Assignment With Augment Division

介绍

使用/=可以使变量自相除。

方法

myVar/=5相当于myVar=myVar/5

练习


var a = 48;
var b = 108;
var c = 33;

// Only modify code below this line

a /=12;
b /=4;
c /=11;

浏览器

image

来源

https://www.freecodecamp.org/challenges/compound-assignment-with-augmented-division

Lesson Review: Convert Celsius to Fahrenheit

介绍

为测试已经学到的东西,这里有一个小测验——将摄氏度转化为华氏度。

方法

华氏度=(摄氏度*9/5)+32

练习


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);

浏览器

image

来源

https://www.freecodecamp.org/challenges/convert-celsius-to-fahrenheit