# 章序号/节序号/节/笔记序号 codecademy, Variable # 概念阐释 ## 语法 在JavaScript中,可以使用一些数学操作符来操作数字,或者赋值`number`的变量。 ## 语义 ### 数学操作符 `+` ,`-`, `*`, `/ `, `%`:取余有的时候也叫modulo(取模),一组数中可被除尽的部分为0,剩下不够除的部分叫做**余数** ### 递增和递减 `++`, `--` ### 修改变量中的数字 `+=` , `-=`, `*=`, `/=` ## 语法 ```js n%d ``` - n: 被除数,结果的*符号与n一致*,当n= ±0 或 ±∞时 -》结果等于*NaN* - d:除数,当d= ±0 或 ±∞时 -》结果等于*n* # 举例子 ### 数学运算符 ```js let subtotal = (13+1)*5; let shipping = 0.5*(13+1); let total = subtotal + shipping; console.log(total); ``` #### 取余当分子大于分母时 例如:3/2,余数为1;其计算公式为:3/2 = (2+1)/2 = 2/2+1/2 = 0+1 = 1 。 上述公式中:2除2是可以除尽的,没有余数,所以余数为0;因为1小于2,1除2是不够除的,有余数,且余数是1。 例如:-13%5, -(10+3)/5= (10/5)+ -(3/5)= 除尽0,除不尽的是3,所以结果为-3 #### 取余当分子小于分母时 取分子的数值 ### 递增和递减 ```js let a = 10; a++; console.log(a); // Output: 11 let b = 20; b--; console.log(b); // Output: 19 ``` ### 修改变量中的数字 ```js let w = 4; w += 1; console.log(w); // Output: 5 let x = 20; x -= 5; // Can be written as x = x - 5 console.log(x); // Output: 15 let y = 50; y *= 2; // Can be written as y = y * 2 console.log(y); // Output: 100 let z = 8; z /= 2; // Can be written as z = z / 2 console.log(z); // Output: 4 ``` # 类比、比较与对比 将w重新赋值的写法: ```js let w = 4; w = w + 1; console.log(w); // Output: 5 ``` # 问题 **1.** 宣称一组变量,并通过`+=`等形式得出以下结果: - levelUp: 10+5=? - powerLevel: 9001-100=? - multiplyMe: 32* 11=? - quarterMe: 1152/4=? **2.** 计算这一变量的结果 ```js let number1 = 4; let number = 2; number1 -= number2; number1-= number2; let result = number1+number2; ``` # 问题答案 **1.** ```js let levelUp = 10; let powerLevel = 9001; let multiplyMe = 32; let quarterMe = 1152; // Use the mathematical assignments in the space below: levelUp += 5; powerLevel -= 100; multiplyMe *= 11; quarterMe /= 4; // These console.log() statements below will help you check the values of the variables. // You do not need to edit these statements. console.log('The value of levelUp:', levelUp); console.log('The value of powerLevel:', powerLevel); console.log('The value of multiplyMe:', multiplyMe); console.log('The value of quarterMe:', quarterMe); ``` **2.** 结果为2 # 备注(经验集/错误集) [取余MDN](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Remainder)