JS基础2

JS基础第三天

自增自减运算符
	++  等同于 +=1
		前自增++a
			先自身加1, 再进行运算
		后自增a++
			先进行运算, 在自身加1
	--  等同于 -=
三元表达式
	?:
		语法 : 表达式1 ? 代码1 : 代码2
			表达式1可以是什么? 	
				关系表达式
				布尔类型类型的值
				其他类型的值, 要转换成布尔类型进行计算
数据类型转换
	显示转换
		转换成字符串
			String()(开发中常用)
				可以转换undefined 和null  开发中的多
			变量名.toString()
				这个方法不能转换undefined和null
			数据后面+ ""
		转换成数字
			parseInt()
				转成整数
					主要偏向于转换字符串
					特点
						从左到右解析,遇到非数字字符, 就停止解析, 输出已经解析出的数字
						如果第一字符就是非数字字符, 就无法转换, 得到NaN
			parseFloat()
				转成小数
					特点
						与parseInt特点一样, 唯一的区别, 就是可以解析小数
			Number()
				转换成数字
					偏向于转换布尔类型
					转换字符串的时候, 必须是纯数字字符串, 否则就是NaN
		转换成buer
			0
			-0
			false
			undefined
			null
			NaN
			""
			document.all
	隐式转换
		转换成字符串
			+ 作为字符串连接符的时候,  (+ 两边有一边是字符串)
		转换成数字
			所有的算术运算符(+作为字符串连接符除外)
		转换成布尔类型
			!
逻辑运算符的短路运算
	逻辑与的短路运算
		找假
			在逻辑与表达式中, 如果第一个式子的结果为false,  那么该逻辑表达式的结果就是第一个式子的值, 否则就是第二个式子的值
	逻辑或的短路运算
		找真
			在逻辑或表达式中, 如果第一个式子的结果为true, 那么那么该逻辑表达式的结果就是第一个式子的值, 否则就是第二个式子的值
	逻辑非没有短路运算

JS基础第四天

流程控制
	顺序结构
		代码从上到下依次执行
	分支结构
		if
		if-else
			他是比较两个互斥的条件
				成绩及格和不及格
		if - else if -else
			多条件判断
		switch -case
			固定值匹配
			匹配的值必须全等
			break
				作用  : 用来结束switch-case语句
				防止穿透
				合理利用穿透作用来简化代码
					春夏秋冬
					输出每个月份的天数
	循环结构
		while
			重复执行相同的代码
				结构解析
					判断条件是否成立, 如果成立, 重复执行循环体代码,  如果不成立,  就执行大括号外面的代码
		do-while
		for 循环
			循环嵌套
			break 与continue
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
		// var num = Math.random()*10;
		// document.write(num>5?num +=5:num -=5);

		// var age = prompt("请输入您的年龄!!!");
		// document.write(age>=18?"恭喜你,成年啦!!!":"别闹,你还是个小屁孩!!!");

		// var peopleNumber = prompt("请输入人数!!!");
		// document.write(peopleNumber == 5?"那咱们一人一个吧!!!":"那我还是一个人干掉它吧!!!")
		
		console.log (parseInt(".123abc"));			//NaN
		console.log (parseFloat(".123abc"));		//0.123
		console.log (parseInt(123));				//123
		console.log (parseFloat(123));				//123
		console.log (parseInt(123.25));				//123
		console.log (parseFloat(123.25));			//123.25


		console.log ( Number ( "123" ) );				//123
		console.log ( Number ( "123.1.1abc" ) );		//NaN
		console.log ( parseInt ( "123.1.1abc" ) );		//123
		console.log ( parseFloat ( "123.1.1abc" ) );	//123.1

		console.log ( Number ( false ) );				//0
		console.log ( Number ( undefined ) );			//NaN


		console.log(+null);					//0
		console.log(+"false");				//NaN
		console.log(+false); 				//0
		console.log(+undefined);			//NaN		
		console.log(+"number");				//NaN

		console.log (parseInt(""));			//NaN
		console.log (parseFloat(""));		//NaN
		console.log (Number(""));			//0
		console.log(+"");					//0

		var a = 5;
    	var b = 0;
    	console.log ( a+b );					//5
    	console.log ( typeof (a+b) );			//number
    	console.log ( a-b );					//5
    	console.log ( typeof (a-b) );			//number
    	console.log ( a-Boolean(b) );			//5
    	console.log ( typeof (a-Boolean(b)) );	//number
    	console.log ( Boolean(a)-b );			//1
    	console.log ( typeof (Boolean(a)-b) );	//number


   		console.log ( Boolean ( 0 ) );			//false
    	console.log ( Boolean ( 0.0 ) );		//false
    	console.log ( Boolean ( "0" ) );		//true
    	console.log ( Boolean ( null ) );		//false
    	console.log ( Boolean ( "null" ) );		//true
    	console.log ( Boolean ( false ) );		//false
    	console.log ( Boolean ( undefined ) );	//false
    	console.log ( Boolean ( ' ' ) );		//true
    	console.log ( Boolean ( NaN ) );		//false
    	console.log ( Boolean ( "false" ) );	//true
    	console.log ( Boolean ( "undefined" ) );//true

    	console.log(Boolean(0),Boolean(-0),Boolean(false),Boolean(null),Boolean(undefined),Boolean(NaN),Boolean(document.all),Boolean(""));

    	console.log ( 1 + "true" );		//1true
		console.log ( 1 + undefined );	//NaN
		console.log ( 1 + null ); 		//1
		console.log ( 1 + false ); 		//1
		console.log ( 1 + NaN ); 		//NaN
		console.log ( 1 + "null" ); 	//1null
		console.log ( 1 + "10" ); 		//110
		console.log ( "1" + 10 ); 		//110
		console.log ( "1" + (10 + 1) );	//111
		console.log ( "1" + 10 + 1 );	//1101
		console.log ( 10 + 1 + "1" );	//111
		console.log ( !10 );			//false
		console.log ( !"10" );			//false
		console.log ( !"0" );			//false
		console.log ( !0 );				//true
		console.log ( !"null" );		//false
		console.log ( !"" );			//true
		console.log ( !"  " );			//false

		var res = undefined || null || 0 || 10 || 1 || '';		//10
    	console.log ( res );

    	var res =  100 && null && 0 && 10 && 1 && '';   //null
    	console.log ( res );

		var age1 = 10;
		var age2 = 10;
		var res1 = typeof(age1) && age1 + age2;  //20
		var res2 = typeof(age1) || age1 + age2;  //number
		var res3 = (age1 - age2) && age1 +age2;  //0
		var res4 = (age1 - age2) || age1 +age2;  //20
		console.log (res1,res2,res3,res4);

		var  c = 12;
		var res1 = " " && c;			//12
		var res2 = "NaN" && c;			//12
		var res3 = undefined || c + "c";	//12c
		console.log (res1,res2,res3);

		//需求:交换两个变量的值 让n2变成20,n1变成10
		var n1 = 10,n2=20;
		[n1,n2]=[n2,n1];
		console.log(n1,n2);

		var n1 = 10;
  		var res =++ n1 + n1++;
  		console.log ( "n1:" + n1 );	//12
 		console.log ( "res:" + res );	//22

		var a = "234";
		console.log(typeof +a); 	//number

		var b = "234b5.88";
		console.log ( Number ( b ) );	//NaN
		console.log ( parseInt (b) );	//234
		console.log ( parseFloat (b) );	//234

		console.log ( 1 - "true" ); 	//NaN
		console.log ( 1 - undefined );  //NaN
		console.log ( 1 - "null" );  	//NaN
		console.log ( 1 + "null" ); 	//1null
		console.log ( 1 + false );	//1
		console.log ( 1 - NaN );  	//NaN
		console.log ( 1 + "null" );	//1null
		console.log ( 1 - "10" ); 	//-9
		console.log ( "1" + 10 ); 	//110
		console.log ( "1" + (10 - 1) ); //19
		console.log ( "1" - 10 + 1 ); 	//-8
		console.log ( 10 + 1 - "1" ); 	//10


  		var aaa;
    	console.log ( Boolean ( aaa ) );			//false
    	console.log ( Boolean ( aaa ) - 1 );    		//-1
    	console.log ( typeof (Boolean ( aaa ) - 1 ));    	//number
    	console.log ( Boolean ( aaa - 1) );			//false
    	console.log (typeof Boolean ( aaa - 1) );		//boolean


		var num1 = 1;
		var num2 = 0;
		console.log(String(num2) && String(num1));	//"1"
		console.log(num1 - !num2 || num1);		//1
		console.log(num2 || num1);			//1
		num1 = undefined;
		console.log(+num1 - num2 && num1 + num2);	//NaN
	</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
		//1、接受用户输入一个字符,判断并在控制台输入的是“一个英文字符吗?”,“一个数字吗?”,“一个汉字吗?”
		var str = prompt("请输入一个字符!!!");
		console.log(/^[0-9]+$/.test(str)?"是数字":(/^[a-zA-Z]+$/.test(str)?"是英文":(/^[\u4e00-\u9fa5]+$/.test(str)?"是汉字":"都不是")));

		//2、页面中弹出一个输入框,接受用户输入一个数字表示年份,控制台中输出这个年份是否为闰年。闰年的公式:1)年份能被4整除,且不能被100整除的是闰年;2)年份能被400整除的是闰年
		// var year = prompt("请输入一个年份!!!");
		//console.log(year%400==0?"是闰年":(year%100==0?"不是闰年":(year%4==0?"是闰年":"不是闰年")));
		// console.log(year%400==0||(year%4==0 && year%100!=0)?"是闰年":"不是闰年");

		//3、价格打折:请用户输入充值金额:如果用户输入的金额>=100元,则享受八折。输出实际应收金额
		//var money = prompt("请输入消费金额!!!");
		//console.log(money>=100?"应收金额"+0.8*money:"应收金额"+money);

		//4、请用户输入一个段留言,如果输入了,就输出留言的内容,否则就输出“主人很懒,什么也没有留下。”要求:写出两种(逻辑运算符和三目运算符)
		//var words = prompt("请输入留言!!!");
		//console.log(words?words:"主人很懒,什么也没留下。");
		//console.log(words || "主人很懒,什么也没留下。");

		//5、弹出一个输入提示框,请用户输入一个表示工资的数字,按如下规则输出该工资金额的级别:1)大于等于20000,输出“高工资”2)小于等于20000,大于等于8000,输出“中高工资”3)小于8000,输出“普通工资”
		//var wages = prompt("请输入工资!!!");
		//console.log(wages>=20000?"高工资":(wages>=8000?"中高工资":"普通工资"));

		//6、接受录入身高和体重,计算指数 指数= 体重(kg)/身高(m)*身高(m)
		//根据指数做出如下输出: 
		//指数:>25 偏重
		//指数:<20 偏瘦
		//指数:>20 <25 正好
		//要求:写出两种(逻辑运算符和三目运算符)
		// var height = prompt("请输入身高!!!") , weight = prompt("请输入体重!!!");
		// var index = weight/Math.pow(height,2);
		// console.log(index>=25?"偏重":(index>=20?"正好":"偏瘦"));
		// console.log((index>=25 && "偏重") || (index>=20 && "正好") || "偏瘦");

	</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
		// var year = parseInt(prompt("请输入年份!!!"));
		// if (year%400==0 || (year%4==0 && year%100!=0)) {
		// 	console.log(year+"是闰年");
		// }else{
		// 	console.log(year+"是平年");
		// }

		// var month = parseInt(prompt("请输入月份!!!"));
		// if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
		// 	console.log(year+"年"+month+"月有31天");
		// } else if (month==4||month==6||month==9||month==11) {
		// 	console.log(year+"年"+month+"月有30天");
		// } else if (month==2 && (year%400==0 || (year%4==0 && year%100!=0))) {
		// 	console.log(year+"年2月有29天");
		// } else if (month==2) {
		// 	console.log(year+"年2月有28天");
		// } else{
		// 	console.log("请输入正确的月份");
		// }
		
		// switch(((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))&&(month == 2)) * 100 + month){
		// 	case 1:
		// 	case 3: 
		// 	case 5:
		// 	case 7:
		// 	case 8:
		// 	case 10:
		// 	case 12:
		// 		alert(year + "年" + month + "月有31天");
		// 		break;
		// 	case 4:
		// 	case 6:
		// 	case 9:
		// 	case 11:
		// 		alert(year + "年" + month + "月有30天");
		// 		break;
		// 	case 2:
		// 		alert(year + "年" + month + "月有28天");
		// 		break;
		// 	case 102:
		// 		alert(year + "年" + month + "月有29天");
		// 		break;
		// 	default:
		// 		alert("年或月错误!!!");
		// 		break;
		// }
		 
		
		// var week = +prompt("请输入星期数!!!");
		// switch(week){
		// 	case 1:
		// 		alert("今天是Monday");
		// 		break;
		// 	case 2:
		// 		alert("今天是Tuesday");
		// 		break;
		// 	case 3:
		// 		alert("今天是Wednesday");
		// 		break;
		// 	case 4:
		// 		alert("今天是Thursday");
		// 		break;
		// 	case 5:
		// 		alert("今天是Friday");
		// 		break;
		// 	case 6:
		// 		alert("今天是Saturday");
		// 		break;
		// 	case 7:
		// 		alert("今天是Sunday");
		// 		break;
		// 	default:
		// 		alert("大傻蛋");
		// 		break;
		// }
		
		// var userInt = parseInt(prompt("请输入一个整数!!!"));
		// if (userInt > 0) {
		// 	userInt += 100;
		// 	alert(userInt);
		// } else if (userInt == 0) {
		// 	alert(userInt);
		// }

		// var a = +prompt("请输入a值!!!"),b = +prompt("请输入b值!!!");
		// if (a % b == 0 || a + b >= 100) {
		// 	alert("a:" + a);
		// } else{
		// 	alert("b:" + b);
		// }
		 
		// var num1 = +prompt("请输入第一个值!!!"),num2 = +prompt("请输入第二个值!!!"),num3 = +prompt("请输入第三个值!!!");
		// if (num1 >= num2 && num1 >= num3) {
		// 	alert("最大值为"+num1);
		// } else if (num2 >= num1 && num2 >= num3) {
		// 	alert("最大值为"+num2);
		// } else {
		// 	alert("最大值为"+num3);
		// }

		// switch((num1 >= num2) * 100 + (num1 >= num3) * 10 + (num2 >= num3)){
		// 	case 110:
		// 	case 111:alert("最大值为" + num1);break;
		// 	case 11:
		// 	case 1:alert("最大值为" + num2);break;
		// 	case 100:
		// 	case 0:alert("最大值为" + num3);break;
		// }
		 
		var num = 0;
		while(num < 10){
			console.log("holle world");
			num++;
		}

		var luckyNum = Math.ceil(Math.random()*100);
		var num1;
		while(true){
			num1 = +prompt("请输入1-100之间的整数!!!");
			if (num1 > luckyNum) {
				alert("猜大了");
			} else if (num1 < luckyNum) {
				alert("猜小了");
			} else{
				alert("恭喜你猜对了!!!");
				break;
			}
		}
	</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
		//1、电话银行反复操作系统
		//  1)分别定义四个含糊是 ,模拟执行查询余额,取款,转账,退出系统四个功能
		//  2)定义一个电话银行函数,接收一个整数参数,根据参数值得不同,执行不同的子函数
		//  -- 输入1,执行的查询余额函数,输出“余额查询中....”
		//  -- 输入2,执行取款函数,输出“取款进行中.....”
		//  -- 输入3,执行转账函数,输出“转账进行中....”
		//  -- 输入0,执行退出系统函数,输出“退出系统,谢谢使用。
		//  3)假设临时系统维护中, 取款功能和转账功能占时不能使用,统一输出“系统维护中....”
		
		// function QueryBalance(){
		// 	alert("余额查询中....");
		// }
		// function Withdraw(Maintain){
		// 	if (Maintain) {
		// 		alert("取款进行中.....");
		// 	} else{
		// 		alert("系统维护中....");
		// 	}
		// }
		// function Transfer(Maintain){
		// 	if (Maintain) {
		// 		alert("转账进行中....");
		// 	} else{
		// 		alert("系统维护中....");
		// 	}
		// }
		// function ExitSystem(){
		// 	alert("退出系统,谢谢使用。");
		// }
		// function TelephoneBanking(){
		// 	while(true){
		// 		var Operation = prompt("欢迎使用手机银行系统:\n功能1.查询余额\n功能2.取款\n功能3.转账\n功能0.退出系统\n请输入功能序号使用!!!");
		// 		if (Operation !=0 ) {
		// 			switch(+Operation){
		// 				case 1:
		// 					QueryBalance();
		// 					break
		// 				case 2:
		// 					Withdraw(0);
		// 					break;
		// 				case 3:
		// 					Transfer(0);
		// 					break;
		// 				default:
		// 					alert("请输入正确的功能序号!!!");
		// 					break;
		// 			}
		// 		} else{
		// 			ExitSystem();
		// 			break;
		// 		}
		// 	}
		// }
		// TelephoneBanking();
		
		// 3、计算1-100之间所有奇数的和,偶数的和,3的倍数的和
		// var num = 1 , oddSum = 0 , evenSum = 0 , multipleOf3Sum = 0;
		// while(num <= 100){
		// 	if(num % 2 == 0){
		// 		evenSum += num;
		// 	} else {
		// 		oddSum += num;
		// 	}
		// 	if (num % 3 ==0) {
		// 		multipleOf3Sum += num;
		// 	}
		// 	num++;
		// }
		// alert("1-100之间所有奇数的和是:"+oddSum+",偶数的和是:"+evenSum+",3的倍数的和是:"+multipleOf3Sum);
		
		// 4、猜数字游戏
		//程序随机生成或任意存储一个0-9范围内的整数;(随机数:(Math.random() * 10) | 0)
		// 用户输入一个整数,若比生成的随机数大则在控制台中输出“猜大了”,若比生成的随机数小则在控制台中输出“猜小了”,直到用户才对数字为止
		// 输入范围外的字符,提示“输入有误”
		// [ 提升要求:输入 “exit”...退出游戏]
		// var luckyNum = Math.floor(Math.random()*10);
		// var myNum;
		// while(true){
		// 	myNum = prompt("请输入0-9之间的整数!!!\n输入exit退出游戏");
		// 	if(myNum == "exit"){
		// 		break;
		// 	} else if (+myNum > 9 || +myNum < 0 || isNaN(+myNum)) {
		// 		alert("输入有误");
		// 	} else if (myNum > luckyNum) {
		// 		alert("猜大了");
		// 	} else if (myNum < luckyNum) {
		// 		alert("猜小了");
		// 	} else {
		// 		alert("恭喜你猜对了!!!");
		// 		break;
		// 	}
		// }
		
		// 5. 输入月份, 输出对应的季节3, 4, 5 春天        6, 7, 8  夏天     9, 10, 11 秋天     12, 1, 2  冬天
		// var month = prompt("请输入月份!!!");
		// switch(+month){
		// 	case 3:
		// 	case 4:
		// 	case 5:
		// 		alert("春天");
		// 		break;
		// 	case 6:
		// 	case 7:
		// 	case 8:
		// 		alert("夏天");
		// 		break;
		// 	case 9:
		// 	case 10:
		// 	case 11:
		// 		alert("秋天");
		// 		break;
		// 	case 12:
		// 	case 1:
		// 	case 2:
		// 		alert("冬天");
		// 		break;
		// 	default:
		// 		alert("输入有误");
		// 		break;
		// }
		
		// 6. 生成一个1-100之间的随机数  判断  如果小于20, 就加30之后输出计算结果, 如果大于等于20, 小于50就直接输出这个数字,如果大于等于50, 就减去20之后输出结果
		// var num1 = Math.random()*99+1;
		// if (num1 < 20) {
		// 	alert(num1+30);
		// } else if (num1 < 50) {
		// 	alert(num1);
		// } else {
		// 	alert(num1 - 20);
		// }
	</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值