Skip to content

Numbers and maths

JavaScript Lesson 3 2:08 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
JavaScript does arithmetic without being taught. JavaScript 不用教就会算术。
Plus and minus and star add, subtract, multiply — and the answers come straight back in the console. 加号、减号、星号分别做加、减、乘—— 结果直接出现在控制台里。
Then look at the division: six divided by four gives one point five. 再看那个除法:6 除以 4 得到 1.5。
If you have met another language you may be waiting for a whole-number answer, or for a conversion step. 如果你学过别的语言,可能正等着一个整数答案,或者等着做一次类型转换。
There is none: JavaScript has one kind of number, and it carries decimals whenever they are needed. 这里都没有:JavaScript 只有一种数字类型,需要小数时它就带小数。
Two more operators earn their place. 还有两个运算符很有用。
Percent gives what is left over after a division: twenty-three percent five is three, because twenty-three is four fives with three to spare. 百分号给出除法之后剩下的部分: 23 % 5 等于 3,因为 23 里有 4 个 5,还剩 3。
Double star raises to a power: two double-star five is thirty-two. 双星号表示乘方:2 ** 5 等于 32。
The remainder is the more useful of the two day to day — it is how you test whether a number is even or odd, and how you pick out every third item in a list. 日常用得更多的是取余——它可以用来判断一个数是偶数还是奇数, 也可以用来从一个列表里每隔两个挑一个。
Now the trap in the lesson's third task. 现在说这一课第三题里的陷阱。
You want the average of two numbers, where a is four and b is eight, so the answer should be six. 你要求两个数的平均值,a 是 4,b 是 8, 所以答案应该是 6。
Write a plus b divided by two with no brackets and you get eight — because the division runs before the addition, so it averages b alone and then adds a. 不加括号写成 a + b / 2,你会得到 8—— 因为除法先于加法执行,于是它只对 b 取了一半,然后再加上 a。
Put brackets round the addition and it is six. 把加法用括号括起来,结果就是 6。
This is the same order you learned in maths: powers, then times and divide, then plus and minus. 这和你在数学课上学的顺序是一样的:先乘方,再乘除,最后加减。
Four things to take with you. 带走四点。
One: plus, minus, star and slash do the arithmetic, and a slash can give you decimals. 第一:加号、减号、星号和斜杠负责算术,而斜杠可以给出小数。
Two: percent gives the remainder, which is your even-or-odd test. 第二:百分号给出余数,这就是你判断奇偶的办法。
Three: double star raises to a power. 第三:双星号表示乘方。
Four: brackets decide who goes first — when a line matters, add them even if you are sure. 第四:括号决定谁先算——遇到重要的一行,即使你有把握也把括号加上。
Now do the three tasks; the third one needs exactly that. 现在去做那三道题;第三题正需要这一点。

Log in or create account

IGCSE, A-Level & AP