当前位置:网站首页>js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
2022-07-02 09:45:00 【荻风溪畔】
又是i++和++i
let i = 1;
alert(i++ + ++i + i) //7
比较运算符
比较运算符的使用
alert(5 == "5") //true
alert(5 === "5") //false
alert(5 !== "5") //true
- 字符串比较,是比较的字符对应的ASCII码
从左往右依次比较
如果第一位一样再比较第二位,以此类推
比较的少,了解即可 - NaN不等于任何值,包括它本身
- 尽量不要比较小数,因为小数有精度问题
- 不同类型之间比较会发生隐式转换
最终把数据隐式转换转成number
类型再比较
所以开发中,如果进行准确的比较我们更喜欢 === 或者 !==
if语句
注意括号的使用。
三元运算符
判断两个数的最大值
let first = +prompt("第一个数:")
let second = +prompt("第二个数:")
alert(first > second ? first : second)
注意:这里的prompt
前面的+
号很有必要,隐式转换成Number
类型。(否则依次输入5,10输出最大结果是5
,因为是字符串比较)
switch语句
注意:switch
中的数据要和case
中的值是全等的
while语句
//while循环简易ATM机
let total = 0; //定义在script块级中的全局变量
let money = 0; //定义在script块级中的全局变量
while (true) {
let i = +prompt(`请选择您的操作: 1.存款 2.取款 3.查看余额 4.退出`)
if (i === 4) {
break //跳出while循环
}
switch (i) {
case 1:
money = +prompt("请输入您的存款金额:")
total += money
break //跳出switch循环
case 2:
money = +prompt("请输入您的取款金额:")
total -= money
break //跳出switch循环
case 3:
alert(total)
}
}
for循环语句乘法表
<style>
div {
display: inline-block;
height: 25px;
line-height: 25px;
margin: 5px;
background-color: pink;
padding: 0 10px;
border: 1px solid hotpink;
color: deeppink;
border-radius: 5px;
box-shadow: 2px 2px 2px rgba(0, 0, 0, .2);
text-align: center;
}
</style>
</head>
<body>
<script>
// 外层打印几行
for (let i = 1; i <= 9; i++) {
// 里层打印几个星星
for (let j = 1; j <= i; j++) {
// 只需要吧 * 换成 1 x 1 = 1
document.write(`<div> ${
j} x ${
i} = ${
j * i} </div>`)
}
document.write('<br>')
}
</script>
</body>
边栏推荐
- 2.6 using recursion and stack - [tower of Hanoi problem]
- 软件测试面试题-2022年大厂面试题合集
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- Drools executes string rules or executes a rule file
- Oracle从入门到精通(第4版)
- [ybtoj advanced training guide] similar string [string] [simulation]
- 浏览器node事件循环
- Typora+docsify quick start
- 中国交通标志检测数据集
- Anti shake throttle
猜你喜欢
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
JSON序列化 与 解析
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
线性DP AcWing 896. 最长上升子序列 II
随机推荐
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
IPhone 6 plus is listed in Apple's "retro products" list
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
Drools terminates the execution of other rules after executing one rule
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
基于STM32的OLED 屏幕驱动
Enhance network security of kubernetes with cilium
Floyd AcWing 854. Floyd求最短路
Redis sentinel mechanism and configuration
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
spfa AcWing 851. SPFA finding the shortest path
8A 同步降压稳压器 TPS568230RJER_规格信息
Is the neural network (pinn) with embedded physical knowledge a pit?
Introduction to CPU instruction set
bellman-ford AcWing 853. Shortest path with side limit
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
spfa AcWing 851. spfa求最短路
接口测试面试题目,你都会了吗?