学习教程:黑马程序员视频链接
运算符
自增运算符
let i = 1;
console.log(i++ + 1); //输出2,i=2
let i = 1;
console.log(++i + 1); //输出3,i=2
比较运算符
开发中,判断相等,推荐用===
比较小数会有精度问题
逻辑运算符
优先级:非>与>或
练习01
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let num = +prompt('please input a num');
// if(num%4 === 0 && num%100 !== 0) {
// alert('true');
// }
// else {
// alert('false');
// }
alert(num%4 === 0 && num%100 !== 0);
</script>
</body>
</html>
练习02 判断闰年
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let year = +prompt('year:');
if(year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
alert(`${year} is leap year`);
} else {
alert(`${year} is common year`);
}
</script>
</body>
</html>
三元运算符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let num1 = +prompt('please input num1:');
let num2 = +prompt('please input num2:');
alert(num1 > num2 ? num1 : num2);
console.log(num1 < 10 ? '0' + num1 : num1);
</script>
</body>
</html>
综合案例-简易取款机
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let balance = 0;
let flag = 1;
while(flag) {
let act = +prompt('please input num:1=save;2=withdraw;3=balance;4=quit');
switch(act) {
case 1:
let amountIn = +prompt('amountIn');
if(amountIn) {
balance += amountIn;
}else {
alert('err msg:NaN');
}
break;
case 2:
let amountOut = +prompt('amountOut');
if(amountOut <= balance) {
balance -=amountOut;
}else {
alert('err msg:your money is not enough');
}
break;
case 3:
alert(`balance:${balance}`);
break;
case 4:
flag = 0;
break;
default:
alert('err msg:wrong input');
break;
}
}
</script>
</body>
</html>
标签:02,prompt,num1,JavaScrip,js,运算符,let,year,alert
From: https://www.cnblogs.com/ayubene/p/17810277.html