Numbers and maths · 数字与数学
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Maths like a calculator
- JavaScript does maths with
+-*/. - There is only one kind of number, so
/can give a decimal:7 / 2is3.5.
中文
像计算器一样做数学
- JavaScript 用
+-*/做数学运算。 - 它只有一种数字类型,所以
/可以得到小数:7 / 2是3.5。
console.log(7 + 2); // → 9
console.log(7 / 2); // → 3.5
English
Remainder and powers
%gives the remainder after dividing:23 % 5is3(5 goes in 4 times, 3 left over).**raises to a power:2 ** 5is2 × 2 × 2 × 2 × 2.
中文
余数与幂
%给出相除后的余数:23 % 5是3(5 进去 4 次,剩下 3)。**求幂:2 ** 5就是2 × 2 × 2 × 2 × 2。
console.log(23 % 5); // → 3
console.log(2 ** 5); // → 32
English
Order of operations
*/%happen before+and-— the same rules as in maths class.- Use brackets
( )to force what should happen first.
中文
运算顺序
*/%在+和-之前进行 —— 和数学课上的规则一样。- 用括号
( )强制让某部分先算。
Explore · 探索
Order of operations, step by step · 运算顺序,一步一步看
× and ÷ happen before · 在...之前 + and −. Brackets go first of all. · × 和 ÷ 在 + 和 − 之前进行。括号最先算。
English
中文
console.log(2 + 3 * 4); // → 14 (先算 3 * 4)
console.log((2 + 3) * 4); // → 20 (先算括号)
English
Common mistakes
%is the remainder operator;/can give a decimal."3" + 4gives"34"(a string), not7.
中文
常见错误
%是取余运算符;/可能得到小数。"3" + 4得到"34"(字符串),不是7。
English
Now you try
- Use the maths operators.
- Press Check answer to test it.
中文
现在轮到你
- 使用这些数学运算符。
- 按检查答案来测试它。
Log 6 times 7 (it is 42). · 打印 6 乘以 7(是 42)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Use % to log the remainder when 23 is divided by 5 (it is 3). · 用 % 打印 23 除以 5 的余数(是 3)。
Click Run to see the output here. · 点击“运行”查看此处输出。
a is 4 and b is 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first. · a 是 4,b 是 8。打印它们的平均值 (a + b) / 2(是 6)。用括号让加法先进行。
Click Run to see the output here. · 点击“运行”查看此处输出。