当前位置:网站首页>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>
边栏推荐
- Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
- bellman-ford AcWing 853. Shortest path with side limit
- Introduction to CPU instruction set
- Sweetheart leader: Wang Xinling
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- Use MySQL events to regularly perform post seven world line tasks
- spfa AcWing 852. SPFA judgement negative ring
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- C#修饰符
猜你喜欢
Is the neural network (pinn) with embedded physical knowledge a pit?
spfa AcWing 852. SPFA judgement negative ring
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
JDBC 预防sql注入问题与解决方法[PreparedStatement]
通过反射执行任意类的任意方法
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
深拷贝 事件总线
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
线性DP AcWing 897. 最长公共子序列
随机推荐
FBX import under ue4/ue5 runtime
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
BOM DOM
Simple use of drools decision table
China traffic sign detection data set
How to write a pleasing English mathematical paper
H5 to app
JSON序列化 与 解析
Linear DP acwing 896 Longest ascending subsequence II
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Leetcode - Sword finger offer 59 - I, 59 - II
Redis bloom filter
Record the range of data that MySQL update will lock
Sweetheart leader: Wang Xinling
Execute any method of any class through reflection
High performance erasure code coding
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?