Chapter 9 Use of javascript mathematical objects (Math)

1. Classification of objects (understanding)

  1. Host object: an object provided by the JavaScript running platform
    • window: Parasitic on the browser, BOM, provides browser-related operations
    • document: Parasitic on the document, DOM, providing web page related operations
  2. Built-in objects: objects defined by ECMAScript that can be used in any ECMAScript environment
    • Local objects: String, Number, Boolean, Array, Object, Function, Date, Promise,XMLHttpRequest
    • Built-in objects:Math
  3. Custom object: an object defined by yourself
    • var obj = {}

2. Introduction

  1. js provides us with some methods for manipulating numbers. They are packaged into a built-in object Math. Math is also called a mathematical object.
  2. We don’t need to pay attention to the calculation methods or logic of certain mathematical operations. We only need to know which method implements this mathematical operation on the Math object.
  3. Use of Math objects: Math.xxx()

3. Math methods and attributes

  1. **Math.random():** Generate a random number between 0 ~ 1, including 0 and excluding 1, the range is: [0, 1)
var num = Math.random();
console.log(num);  // 一个随机数
  1. **Math.round():** Round a decimal to an integer
var num1 = 10.1;
console.log(Math.round(num1));  // 10

var num2 = 10.6;
console.log(Math.round(num2));  // 11
  1. **Math.abs():**Returns the absolute value of the specified number
var num = -10;
console.log(math.abs(num));  // 10
  1. **Math.ceil(): **Round up the specified decimal
var num = 10.1;
console.log(Math.ceil(num));  // 11

var num2 = 10.9;
console.log(Math.ceil(num2));  // 11
  1. **Math.floor():** Round the specified decimal down to the nearest integer
var num = 10.1;
console.log(Math.floor(num));  // 10

var num2 = 10.9;
console.log(Math.floor(num2));  // 10
  1. **Math.max():** The parameters are multiple numbers, and the return value is the largest number
console.log(Math.max(1, 2, 3, 4, 5));  // 5
  1. **Math.min():** The parameters are multiple numbers, and the return value is the smallest number
console.log(Math.min(1, 2, 3, 4, 5));  // 1
  1. **Math.sqrt():** Calculate the square root of the specified number
var res = Math.sqrt(4)
console.log(res);		// 2
  1. **Math.pow(base, exponent):** Calculate the result based on the specified base and exponent passed in
var res = Math.pow(2, 4)
console.log(res);		// 16
  1. **Math.PI:** Get the value of π, 3.1415926…
console.log(Math.PI) // 3.141592653589793
  • Because of the calculation accuracy of the computer, we can only get 15 digits after the decimal point.
  • Math.PI is not a method, it is an attribute, so you don’t need to add parentheses when using it.
  1. Math.sin() : Calculate sine. Note that the parameters are radians: interactively convert to radians formula:Math.PI / 180 * deg
  2. Math.cos() : Calculate cosine. Note that the parameter is radians: interactively convert the radians formula:Math.PI / 180 * deg

4. Range random numbers

  • Using rounding and random number methods, random numbers in a specified range can be calculated
function random(min, max){
    
    
	return Math.round( Math.random()*(max-min)+min );
}

5. Base conversion

  1. What is hexadecimal system: Hexadecimal system is to carry forward one digit when reaching the specified position.
  2. Common bases
    • Binary: 0 1 10 11 100 101 110 111 1000
    • Octal: 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21
    • Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 … 99 100 101
    • Hexadecimal: 0 1 2 3 4 5 6 7 8 9 abcdef 10 … 19 … 1a 1b 1c 1d 1e 1f 20 21 …
  3. Convert decimal to other bases
    • num.toString()Method can give a base number when converting numbers to strings
      • grammar:num.toString(你要转换的进制)
      • Return value: the number after conversion.
      • The converted number is of string type
var num = 100
console.log(num.toString(2)) // 1100100
console.log(num.toString(8)) // 144
console.log(num.toString(16)) // 64
  1. Convert other bases to decimal
    • parseInt()The method can convert the string as a base into decimal when converting the string into a number.
      • grammar:parseInt(要转换的字符串,当作几进制来转换)
      • Return value: converted number. You use the number as a decimal system and convert it to decimal system.
      • The result is a numeric type
var str = 100
console.log(parseInt(str, 8)) // 64 把 100 当作一个 八进制 的数字转换成 十进制 以后得到的
console.log(parseInt(str, 16)) // 256 把 100 当作 十六进制 的数字转换成 十进制 以后得到的
console.log(parseInt(str, 2)) // 4 把 100 当作 二进制 的数字转换成 十进制 以后得到的

6. Practice

  1. Encapsulate range random integer function
  2. A function that encapsulates a random 4-digit verification code
    • Such as: 0287
  3. A function that encapsulates a random 4-digit alphanumeric verification code
    • Such as: 6a4h
  4. Function that encapsulates color values ​​in random rgb format
    • Such as: rgb(23,53,126)
  5. Function that encapsulates a random hexadecimal color value
    • Such as: #61f135
  6. random roll call

Guess you like

Origin blog.csdn.net/weixin_41636483/article/details/135272366