Javascript flow control - if statement exercise

First, the flow control judgment statement if

                                                switch


                        while loop statement

                                                do while

                                                for 

Sequence structure, selection structure, loop structure

Second, if statement (the statement in parentheses is true when the braces are executed)

	<script type="text/javascript">
		var age=20;
		if(age>18){
			console.log("成年人");
		}
		else{
			console.log("小孩");
		}
	</script>

Exercise 1: Exam Results

	<script type="text/javascript">
		var cj=80;
		if(cj>=60){
			console.log("Pass");
		}
		else if(cj>=80){
			console.log("良好");	
		}
		else if(cj>=90){
			console.log("Excellent");	
		}
		else {
			console.log("Failed");	
		}
	</script>

Exercise 2: Input a number from the keyboard to determine whether it is an even number and output yes otherwise no

	<script type="text/javascript">
		/ / Input a number from the keyboard, to determine whether it is an even number is to output yes or no
		//Define the input data storage in the receiving keyboard
		var num;
		//Use the prompt() function to receive the integer input by the user
		num=+(window.prompt("Please enter a number"));
		//validity verification
		if(isNaN(num)){
			alert("The input is not a number")
		}
		else{
			// Determine if the user input is an even number
			if(num%2===0){
				alert("yes");
			}
			else{
				alert("no");
				
			}
		}
	</script>

Optimized:

	<script type="text/javascript">
		/ / Input a number from the keyboard, to determine whether it is an even number is to output yes or no
		//Define the input data storage in the receiving keyboard
		var num;
		//Use the prompt() function to receive the integer input by the user
		num=+(window.prompt("Please enter a number"));
		//validity verification
		if(isNaN(num)){
			alert("The input is not a number")
		}
		else{
			(num%2===0)?alert("yes"):alert("no");
		}
	</script>

Exercise 3: Receive an integer input from the user and output it is the day of the week

	<script type="text/javascript">
		//Receive the integer input by the user and output it is the day of the week
		//Define the receiving data variable
		var day;		
		//Receive data
		day=+(window.prompt("Please enter an integer between 1 and 7"));
		// Determine the day of the week
		if(day===1){
			console.log("Week 1");
		}
		else if(day===2){
			console.log("Week 2");
		}
		else if(day===3){
			console.log("Week 3");
		}
		else if(day===4){
			console.log("Week 4");
		}
		else if(day===5){
			console.log("Week 5");
		}
		else if(day===6){
			console.log("Week 6");
		}
		else if(day===7){
			console.log("Week 7");
		}
		else{
			console.log("未知");
		}
	</script>

Exercise 4: Receive the integer month input by the user, and output the season according to the month

	<script type="text/javascript">
		//1, define variables
		var month;
		//2, receive input
		month=+(window.prompt("Please enter 1-12 numbers"));
		//3, verify the data
		if(isNaN(month)){
			alert("input error");
			
		}
		else{
			//4, judge the season  
			if(month>=3 && month<=5){
				alert("春");
			}			
			else if(month>=6 && month<=8){
				alert("夏");
			}
			else if(month>=9 && month<=11){
				alert("秋");
			}
			else if(month===12 || month===1 || month===2) {
				alert("冬");
			}
			else{
				alert("error");
			}
		}
		
	</script>

Exercise 5: Input 2 integers from the keyboard, sort and output

	<script type="text/javascript">
		/ / Input 2 integers from the keyboard, sort and output
		//define variable
		var num1, num2;
		//Receive user input
		num1=+(window.prompt("Please enter the first number"));
		num2=+(window.prompt("Please enter the second number"));
		// compare two numbers
		if(num1>num2){
			console.log(num1);
		}
		else if(num1===num2){
			console.log("equal");
		}
		else{
			console.log(num2);
		}
		
	</script>

Optimized:

	<script type="text/javascript">
		/ / Input 2 integers from the keyboard, sort and output
		//define variable
		var num1, num2;
		//Receive user input
		num1=+(window.prompt("Please enter the first number"));
		num2=+(window.prompt("Please enter the second number"));
		// compare two numbers	
		console.log(num1>num2?num1:num2);		
	</script>

Advantages of ternary operator: Simplified code. Disadvantage: must output deterministic results

Exercise 6: Enter three numbers on the keyboard, sort them from largest to smallest and then output

	<script type="text/javascript">
		//Enter three numbers in the keyboard, sort them from large to small and then output
		//define three variables
		var num1, num2, num3;
		// receive three variables
		num1=+(window.prompt("Enter the first number"));
		num2=+(window.prompt("Enter the second number"));
		num3=+(window.prompt("Enter the third number"));
		//sort three numbers
		if(num1>=num2 && num2>=num3){
			console.log(num1,num2,num3);
		}
		else if(num1>=num3 && num3>=num3){
			console.log(num1,num3,num2);			
		}
		else if(num2>=num1 && num1>=num3){
			console.log(num2,num1,num3);			
		}
		else if(num2>=num3 && num3>=num1){
			console.log(num2,num3,num1);			
		}
		else if(num3>=num1 && num1>=num2){
			console.log(num3,num1,num2);			
		}
		else if(num3>=num2 && num2>=num1){
			console.log(num3,num2,num1);			
		}
	</script>

This comparison is the most primitive we want to optimize:

selection sort

	<script type="text/javascript">
		//Enter three numbers in the keyboard, sort them from large to small and then output
		//define three variables
		var num1, num2, num3;
		// receive three variables
		num1=+(window.prompt("Enter the first number"));
		num2=+(window.prompt("Enter the second number"));
		num3=+(window.prompt("Enter the third number"));
		//Sort three numbers using selection sort
		var temp;
		if(num1>num2){
			temp=num1;
			num1=num2;
			num2=temp;
			
		}
		if(num1>num3){
			temp=num1;
			num1=num3;
			num3=temp;
			
		}
		if(num2>num3){
			temp=num2;
			num2=num3;
			num3=temp;
			
		}
		console.log(num3,num2,num1);
	</script>
bubble sort
	<script type="text/javascript">
		//Enter three numbers in the keyboard, sort them from large to small and then output
		//define three variables
		var num1, num2, num3;
		// receive three variables
		num1=+(window.prompt("Enter the first number"));
		num2=+(window.prompt("Enter the second number"));
		num3=+(window.prompt("Enter the third number"));
		//Sort three numbers using bubble sort
		var temp;
		if(num1>num2){
			temp=num1;
			num1=num2;
			num2=temp;
			
		}		
		if(num2>num3){
			temp=num2;
			num2=num3;
			num3=temp;
			
		}
		if(num1>num2){
			temp=num1;
			num1=num2;
			num2=temp;		
		}
		console.log(num3,num2,num1);
	</script>

Exercise 7: The realization of rock-paper-scissors

	<script type="text/javascript">
		//The implementation of rock paper scissors defines scissors 0 rock 1 paper 2
		//1, define variables, players and computers
		var com,play;
		//2. The computer punches random() to generate random numbers between 0 and 1
		com=Math.floor(Math.random()*3);
		//3, the player punches
		play=+(window.prompt("Please input: 0 rock scissors 1 cloth 2"));
		//4, judge the winner and loser
		if(play<0 || play>2){
			window.prompt("Please re-enter: scissors 0 rock 1 cloth 2");
		}
		else{
			if( play===0 && com===2 ||
				play===1 && com===0 ||
				play===2 && com===1 )
			{
				alert("Congratulations you won");
			}
			else if(
				play===0 && com===1 ||
				play===1 && com===2 ||
				play===2 && com===0
			){
				alert("Sorry you lost");
				
			}
			else{
				alert("平");
			}
		}
	</script>






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642235&siteId=291194637