JavaScript语句

JavaScript语句

if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码

<script type="text/javascript">
		
		if(5>3){
			console.log(`5大于3`)
		}
	</script>

在这里插入图片描述
if…else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码

<script type="text/javascript">
	var sex= ``
	if (sex == '男') {
		console.log('去男澡堂')
	} else {
		console.log('去女澡堂')
	}
	
	</script>

在这里插入图片描述

if…else if…else 语句- 使用该语句来选择多个代码块之一来执行

<script type="text/javascript">
	var chengji = 90
	if (chengji>=90){
		console.log(`A-优秀`)
	}else if(chengji>=80){
		console.log(`B-良好`)
	}else if(chengji>=60){
		 console.log(`C-合格`)
	}else if(chengji>=59){
		console.log(`D-不及格`)
	}
	
	
	</script>

在这里插入图片描述
if简易计算器

<style type="text/css">
			.input{
				width: 100px;
				height: 30px;
				background: #BFBFBF;
				outline: none;
				border: 0;
				font-size: 15px;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<input type="number" id="box1" class="input">
		<select name="" id="box2" class="input">
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select>
		<input type="number" id="box3" class="input">
		<input type="button" id="box4" class="input" value="=">
		<input type="number" id="box6" class="input">

	</body>
	<script type="text/javascript">
		var box1 = document.getElementById(`box1`)
		var box3 = document.getElementById(`box3`)
		var box6 = document.getElementById(`box6`)
		var box2 = document.getElementById(`box2`)
		var box4 = document.getElementById(`box4`)
		box4.onclick = function() {
			var box1Value = box1.value * 1
			var box3Value = box3.value * 1
			console.log(box1Value);
			var box2Value = box2.value
			if (box2Value == '+') {
				box6.value = box1Value + box3Value
			} else if (box2Value == '-') {
				box6.value = box1Value - box3Value
				console.log(box6.value)
			} else if (box2Value == '*') {
				box6.value = box1Value * box3Value
				console.log(box6.value)
			} else if (box2Value == '/') {
				box6.value = box1Value / box3Value
				console.log(box6.value)
			}
		}
	</script>

在这里插入图片描述
switch
简单计算器

<style type="text/css">
			.input {
				width: 100px;
				height: 30px;
				background: #BFBFBF;
				outline: none;
				border: 0;
				font-size: 15px;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<input type="number" id="box1" class="input">
		<select name="" id="box2" class="input">
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select>
		<input type="number" id="box3" class="input">
		<input type="button" id="box4" class="input" value="=">
		<input type="number" id="box6" class="input">

	</body>
	<script type="text/javascript">
		var box1 = document.getElementById(`box1`)
		var box3 = document.getElementById(`box3`)
		var box6 = document.getElementById(`box6`)
		var box2 = document.getElementById(`box2`)
		var box4 = document.getElementById(`box4`)
		box4.onclick = function() {
			var box1Value = box1.value * 1
			var box3Value = box3.value * 1
			console.log(box1Value);
			var box2Value = box2.value
			switch (box2Value) {
				case `+`:
				box6.value = box1Value + box3Value
				break
				case `-`:
				box6.value = box1Value - box3Value
				break
				case `*`:
				box6.value = box1Value * box3Value
				break
				case `/`:
				box6.value = box1Value / box3Value
				case `+`:
				box6.value = box1Value + box3Value
				break

			}
		}
	</script>
</html>

</head>
<body>
	<input type="number" id="box1" class="input">
	<select name="" id="box2" class="input">
		<option value="+">+</option>
		<option value="-">-</option>
		<option value="*">*</option>
		<option value="/">/</option>
	</select>
	<input type="number" id="box3" class="input">
	<input type="button" id="box4" class="input" value="=">
	<input type="number" id="box6" class="input">

</body>
<script type="text/javascript">
	var box1 = document.getElementById(`box1`)
	var box3 = document.getElementById(`box3`)
	var box6 = document.getElementById(`box6`)
	var box2 = document.getElementById(`box2`)
	var box4 = document.getElementById(`box4`)
	box4.onclick = function() {
		var box1Value = box1.value * 1
		var box3Value = box3.value * 1
		console.log(box1Value);
		var box2Value = box2.value
		switch (box2Value) {
			case `+`:
			box6.value = box1Value + box3Value
			break
			case `-`:
			box6.value = box1Value - box3Value
			break
			case `*`:
			box6.value = box1Value * box3Value
			break
			case `/`:
			box6.value = box1Value / box3Value
			case `+`:
			box6.value = box1Value + box3Value
			break

		}
	}
</script>

在这里插入图片描述

for循环

<script type="text/javascript">
		for(var i = 0;i<10;i++){
			console.log(i)
		}
	</script>

在这里插入图片描述
99乘法表

<script type="text/javascript">
		var stt = " "
		for (var i = 1; i <= 9; i++) {
			for (var k = 1; k <= i; k++) {
				var a = i + '*' + k + '=' + i * k + '   '
				stt += a
			}
			stt += '<br />'
		}
		console.log(stt)
		document.write(stt)
	</script>

在这里插入图片描述

while

<script type="text/javascript">
		var i = 0
		while(i<100){
			console.log('hello world')
			i++
		}
		
		var a = 100
		while(a>0){
			console.log('hi')
			a--
		}
	</script>

在这里插入图片描述

<script type="text/javascript">
		var a = 0
		do {
			console.log('1000')
			a++
		} while (a < 100)
	</script>

在这里插入图片描述
将可能会报错的代码放在try中
catch块中就是我们对错误的处理方式
finally块中的代码不管是否产生错误都会执行

<script type="text/javascript">
		
		try {
			console.log(a)
		} catch (err) {
			console.log(err)
		}finally{
			console.log(1000)
		}
	</script>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值