当前位置:网站首页>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>

边栏推荐
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- Anti shake throttle
- Redis sentinel mechanism and configuration
- 模块化 CommonJS ES Module
- BOM DOM
- Use sqoop to export ads layer data to MySQL
- 线性DP AcWing 897. 最长公共子序列
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
猜你喜欢
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

Linear DP acwing 896 Longest ascending subsequence II

C#修饰符

Distributed machine learning framework and high-dimensional real-time recommendation system

染色法判定二分图 AcWing 860. 染色法判定二分图

In development, why do you find someone who is paid more than you but doesn't write any code?

spfa AcWing 852. spfa判断负环

防抖 节流
随机推荐
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Introduction to CPU instruction set
上手报告|今天聊聊腾讯目前在用的微服务架构
移动式布局(流式布局)
模块化 CommonJS ES Module
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
获取文件版权信息
线性DP AcWing 895. 最长上升子序列
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
bellman-ford AcWing 853. Shortest path with side limit
Linear DP acwing 898 Number triangle
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Typora+docsify quick start
bellman-ford AcWing 853. 有边数限制的最短路
浏览器node事件循环
C#运算符
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
JDBC prevent SQL injection problems and solutions [preparedstatement]
Fluent fluent library encapsulation
2.6 using recursion and stack - [tower of Hanoi problem]