javascript 运算符,流程控制,四种变量,函数一级页面交互

每一种语言中都有自己的运算符,表现方式都有一些区别。

javascript运算符如下:

- 赋值运算符

前提:x=5,y=5

| 运算符 | 例子 | 等同于 | 运算结果 |
| :----- | :--- | ------ | -------- |
| =      | x=y  |        | 5        |
| +=     | x+=y | x=x+y  | 10       |
| -=     | x-=y | x=x-y  | 0        |
| *=     | x*=y | x=x*y  | 25       |
| /=     | x/=y | x=x/y  | 1        |
| %=     | x%=y | x=x%y  | 0        |

- 比较运算符

前提:x=5

| 运算符 | 描述       | 比较    | 结果  |
| ------ | ---------- | ------- | ----- |
| ==     | 等于       | x=="5"  | true  |
| ===    | 绝对等于   | x==="5" | false |
| !=     | 不等于     | x!="5"  | fales |
| !==    | 不绝对等于 | x!=="5" | true  |
| >      | 大于       | x>5     | false |
| <      | 小于       | x<5     | false |
| >=     | 大于等于   | x>=5    | true  |
| <=     | 小于等于   | x<=5    | true  |

- 逻辑运算符

前提:n=5

| 运算符 | 描述 | 例子          | 结果                |
| ------ | ---- | ------------- | ------------------- |
| &&     | 与   | x=n>10&&++n   | x=false,n=5(短路) |
| ||   | 或   | x=n<10||n-- | x=true,n=5(短路)  |
| !      | 非   | x=!n          | x=false,x=5         |

在这里数字类型和python中有点区别,这里数字类型部不分整型和小数类型。

当我们真的需要要整数的话可以用parseint 转一下。

    let n1 = 5;
    let n2 = 2;
    let res= n1/n2;
    console.log(res);

    // res = parseInt(res);
    res = parseFloat(res);
    console.log(res);
    console.log(parseInt('456asdad789'));
    console.log(parseFloat('222.5xxz'))

如果数字本身是正数的话,用parsefloat去转换的话是不会有.0出现的不会强行制造一个小数。

流程控制

首先是 if else流程控制:

随机数:

随机数 [0, 1) => [m, n]
[0, 1) * 11 => [0, 11) parseInt() => [0, 10] + 5 => [5, 15]

[5,15]就是[0,10]的整个区间,都加上了5 

[0,10]就是[0,11)的取整得到的

[0,11) 就是[0,1)区间* 11得到的

[m - m,n - m]  = [0,10]
[0,n - m +1 ] = [0,11)

[0,1) * (n - m +1) = [0,11)

parseint([0,1) * ((n- m +1) + n )) = [0,15]

  公式:parseInt(Math.random() * (max - min + 1)) + min
    let num = parseInt(Math.random() * (40 - 10 + 1)) + 10;
    console.log(num);

    if (num >= 30) {
        console.log('数字超过30');
    } else if (num >= 20) {
        console.log('数字超过20');
    } else {
        console.log('数字超过10');
    }