Homework for the fourth and fifth days of learning JS

Posted by JeremyTiki on Thu, 20 Jan 2022 14:36:13 +0100

Assignment 1

The following functions are implemented as required and tested:

Implement a join function, which can connect the parameters according to the specified characters.

Function name: join.

Parameters: s (symbol), SRC1 (character 1), src2 (character 2)

Return value: string after connection

function join(s,src1,src2){
    ///Self realization
    return .....
}

Call effect:

var str = join("-","wate are you","What are you doing"); 
// STR - > water are you - what are you doing

code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			function join(s,src1,src2){
				return src1 + " " + s + " " +src2;
			}
			var str = join("-","what are you","What are you doing");
			console.log(str);
		</script>
	</body>
</html>

Assignment 2

Implement the following functions as required, and test the functions

Function 1:

Function name : floor
 Parameters: x  ((one number)
Return value: x  Return less than x The maximum integer of a number, that is, the value of a number rounded down.

explain:

The function returns the largest integer whose number is less than x.

For example, floor(10) returns 10, floor (10.2) returns 10, and floor (- 10.2) returns - 11
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			function floor(num){
				var result;
				if(num%1 === 0){
					return num;
				}else{
					result = num >= 0 ? parseInt(num) : parseInt(num-1);
					return result;
				}
			}
			console.log(floor(10));
			console.log(floor(10.2));
			console.log(floor(-10.2));
		</script>
	</body>
</html>

Function 2:

Function name: pow
 Parameters: x(Cardinality), y(Index)
Return value: function return cardinality( x)Index of( y)Power. that is  x of  y Power.

code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			function pow(x,y){
				var result = 1;
				for(var i=0;i<y;i++){
					result *= x;
				}
				return result;
			}
			console.log(pow(2,3));
		</script>
	</body>
</html>

Assignment 3

Create the following objects in two ways:

There are the following objects:

Object name: Custom
 Properties: name= Arthur, intelligence=8,power=15,agile=10, skill = A function whose content is defined by itself

Create using {} and Object respectively. And call properties and methods respectively.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var hero1 = {name:"Arthur",intelligence:8,power:15,agile:10,skill:function(){console.log("Holy sword ruling");}};
			var hero2 = new Object();
			hero2.name = "Li Bai";
			hero2.intelligence = 10;
			hero2.power = 20;
			hero2.agile = 9;
			hero2.skill = function(){
				console.log("Kill in ten steps");	
			}
			console.log(hero1.name);
			console.log(hero1.intelligence);
			console.log(hero1.power);
			console.log(hero1.agile);
			hero1.skill();
			console.log(hero2.name);
			console.log(hero2.intelligence);
			console.log(hero2.power);
			console.log(hero2.agile);
			hero2.skill();
		</script>
	</body>
</html>

Assignment 4

Implement an object formatting function.

There are the following objects:

Properties: name,age,tel,gender
 method: eat

Create this object yourself.

Implement the following functions:

Function name: objectFormat
 Parameters: p((one object)
Return value: None

Function: if the object's name If the property does not exist or is an empty string, it is set to the default name. The default name is"bakabaka". If the subject's age is no longer 18~50 Between, it is set to "age does not meet the requirements", if gender Is 1, then gender Set to male,If it is 2, it is set to "female", otherwise it is set to "unknown"

Create objects and test functions.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			function objectFormat(p){
				if(!p.name || p.name == null){
					p.name = "bakabaka";
				}
				if(p.age<18 || p.age>50){
					p.age = "Age does not meet the requirements";
				}
				if(p.gender == 1){
					p.gender = "male";
				}else if(p.gender == 2){
					p.gender = "female";
				}else{
					p.gender = "Unknown";
				}
			}
			var per1 = {name:"Li Bai",age:25,gender:1} // Normal case
			var per2 = {age:25,gender:1}			 // The name attribute does not exist
			var per3 = {name:"",age:25,gender:1}     // name is an empty string
			var per4 = {name:"Li Bai",age:10,gender:1}	 // Age does not meet the requirements
			var per5 = {name:"Li Bai",age:25,gender:2}	 // Gender is female
			var per6 = {name:"Li Bai",age:25,gender:3} // Gender unknown
			objectFormat(per1);
			objectFormat(per2);
			objectFormat(per3);
			objectFormat(per4);
			objectFormat(per5);
			objectFormat(per6);
			console.log(per1);
			console.log(per2);
			console.log(per3);
			console.log(per4);
			console.log(per5);
			console.log(per6);
		</script>
	</body>
</html>

Assignment 5

Achieve the following requirements:

tips: the following program provides some implemented functions for you. There are some functions that you need to implement yourself.

Page:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<p>
			your birthday:<input type="text" size="4">year
			<input type="text" id="year" size="2">month
			<input type="text" size="2">day
			<input type="button" value="Go you" onclick="getDays()">
			<input type="text" id="result">
		</p>
		<script type="text/javascript">
			function getDays(){
				var year = document.getElementById("year").value;
				var month = document.getElementById("month").value;
				var date = document.getElementById("date").value;
				// Verify that the data format is correct
				if(!checkBirth(year,month,date)){
					return;
				}
				
				var total = 0;
				total += eval(getDaysOfBirthYear(year,month,date))+eval(getDaysOfYears(year))+eval(getDaysOfNow());
				document.getElementById("result").value="You're alive"+total+"Oh, my God";
			}
			// ----It has been implemented and provided to you-----
			// Gets the current year
			function getNowYear(){
				return new Date().getFullYear();
			}
			// Gets the current month
			function getNowMonth(){
				return new Date().getMonth()+1;
			}
			// Get current date
			function getNowDate(){
				return new Date().getDate();
			}
			// -------Here are the functions you want to implement-------------
			// Inspection data format
			function checkBirth(year,month,date){
				// Check here whether the year is after 1900. If it's not return false
				// Check whether the month is between 1 and 12. If not, return false
				// Check whether the date is within the date of the current month (big month 1 ~ 31, small month 1 ~ 30, February 1 ~ 28 or 1 ~ 29). If not, return false
				return true;
			}
			// Get the total number of days of the current month according to the incoming year and month
			function getDaysOfMonth(year,month){
				// Refer to yesterday's job implementation
				// Total days of final return
			}
			// Calculate the number of days left in the year according to the incoming date
			function getDaysOfBirthYear(year,month,date){
				var days = 0;
				// Determine whether year is a leap year
				// Calculate the number of days left in month according to the data. And add it to days.
				// Cycle to add all the days of the remaining months after month.
				// Finally, return to days.
			}
			// Calculate the total number of days from the incoming year to this year according to the incoming year
			function getDaysOfYears(year){
				var days = 0
				// The loop starts with year and ends with getNowYear().
				for(year ; year < getNowYear();year ++){
					if(leap year){
						+366;
					}else{
						+365;
					}
				}
				// Finally, return to days
			}
			// Calculate how many days have passed this year
			function getDaysOfNow(){
				// Requirements, for example.
			}
		</script>
	</body>
</html>

code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		your birthday:
		<input type="text" id="year" size="4" />year
		<input type="text" id="month" size="2" />month
		<input type="text" id="date" size="2" />day
		<input type="button" value="Go you" onclick="getDays()" />
		<input type="text" id="result" />
		<script type="text/javascript">
			function getDays(){
				var year = eval(document.getElementById("year").value);
				var month = eval(document.getElementById("month").value);
				var date = eval(document.getElementById("date").value);
				if(!checkBirth(year,month,date)){
					console.log("error");
					return;
				}
				var total = 0;
				total += eval(getDaysOfBirthYear(year,month,date))+eval(getDaysOfYear(year))+eval(getDaysOfNow());
				document.getElementById("result").value="You're alive"+total+"Oh, my God";
			}
			function getNowYear(){
				return new Date().getFullYear();
			}
			function getNowMonth(){
				return new Date().getMonth();
			}
			function getNowDate(){
				return new Date().getDate();
			}
			function checkBirth(year,month,date){
				var day;
				var res = ((year % 400) == 0)||((year % 100) != 0)&&((year % 4) == 0);
				switch(month){
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						day=31;
						break;	
					case 4:
					case 6:
					case 9:
					case 11:
						day=30;
						break;
					case 2:
						if(res)
							day=29;															
						else
							day=28;
						break;	
						}
				if(year<1900){
					return false;
				}else if(month<1||month>12){
					return false;
				}else if(date>day||date<1){
					return false;
				}else{
					return true;
				}
			}
			function getDaysOfMonth(year,month){
				var days = 31;
				var res = ((year % 400) == 0)||((year % 100) != 0)&&((year % 4) == 0)
				switch(month){
					case 4:
					case 6:
					case 9:
					case 11:
						day = 30;
						break;
					case 2:
						if(res == true){
							days = 29;
						}else{
							days = 28;
						}
						break;
				}
				return days;
			}
			function getDaysOfBirthYear(year,month,date){
				var days;
				days = getDaysOfMonth(year,month) - date - 2;
				for(var i=12; i>month;i--){
					days += getDaysOfMonth(year,i);
				}
				console.log(days);
				return days;
			}
			function getDaysOfYear(year){
				var days = 0;
				for(var i=year+1; i<getNowYear(); i++){
					var a=i%400==0 || i%100!=0 && i%4==0;
					if(a){
						days += 366;
					}else{
						days += 365;
					}
				}
				console.log(days);
				return days;
			}
			function getDaysOfNow(){
				var days = 0;
				var y = getNowYear();
				var m = getNowMonth();
				var d = getNowDate();
				for(var i=1;i<m;i++){
					days += getDaysOfMonth(y,i);
				}
				days += d;
				console.log(days);
				return days;
			}
		</script>
	</body>
</html>

Assignment 6

Complete the following tasks as required

[1] Define the array names with a length of 5.

[2] Array contents: Tang Changlao, Qi Tiansheng, marshal Tianpeng, general roller shutter

[3] Change the second element of the array to monkey king

[4] Output the fifth element of the array

[5] Set the seventh element of the array to Tathagata Buddha

[6] Length of output array

[7] Traverse the array and print each element of the array one by one on the console.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var names = new Array(5);
			names[0] = "Elder Tang";
			names[1] = "Qi Tian Da Sheng";
			names[2] = "Marshal Tianpeng";
			names[3] = "Roller shutter general";
			for(var x=0;x<names.length;x++){
				console.log(names[x]);
			}
			console.log("--------------------");
			names[2] = "Sun WuKong";
			for(var x=0;x<names.length;x++){
				console.log(names[x]);
			}
			console.log("--------------------");
			console.log(names[4]);
			console.log("--------------------");
			names[6] = "Tathagata Buddha"
			console.log(names.length);
			console.log("--------------------");
			for(var x=0;x<names.length;x++){
				console.log(names[x]);
			}
		</script>
	</body>
</html>

Assignment 7

Complete the following as required

There are the following arrays: [98,56,89,25,68,69];

The writer finds the largest number in the array.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var nums = [98,56,89,25,68,69];
			var max = nums[0];
			for(var x=1;x<nums.length;x++){
				if(nums[x]>max){
					max = num[x];
				}
			}
			console.log(max);
		</script>
	</body>
</html>

Assignment 8

There are the following arrays: [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]

It is required to remove the 0 items in the array, store the values that are not 0 into a new array, and generate a new array
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var nums1 = [4,0,7,9,0,0,2,6,0,3,1,0];
			var nums2 = [];
			for(var x=0,y=0;x<nums1.length;x++){
				if(nums1[x]!=0){
					nums2[y] = nums1[x];
					y++;
				}
			}
			for(var i=0;i<y;i++){
				console.log(nums2[i]);
			}
		</script>
	</body>
</html>

Assignment 9

The following pages are implemented:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-b3lmn1k0-1642381692529) (operation. Assets / 164213672088. PNG)]

When clicking the Add button, add the array to be added to an array. When adding, check whether the array already exists in the array. If it exists, it will prompt that the number already exists and do not add. If it does not exist, add the array and prompt that the addition is successful.

When you click the print array button, print the elements of the array on the console one by one.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		Number of to add:
		<input type="text" size="2" id="num" />
		<input type="button" value="add to" onclick="addNum()" />
		<input type="button" value="Print array" onclick="printNum()" />
		<script type="text/javascript">
			var nums = [];
			var x = 1;
			function addNum(){				
				var num = eval(document.getElementById("num").value);
				if(nums.length == 0){
					nums[0]=num;
					document.getElementById("num").value = null;
					alert("Join successfully");
					return;
				}					
				else{
					for(var i= 0 ;i< nums.length;i++){
						if(eval(nums[i]) == num){
							alert("Already exists");
							document.getElementById("num").value = null;
							console.log('1');
							return;
						}
					}
					nums[x++]=num;
					document.getElementById("num").value = null;
					alert("Join successfully");
					console.log('2');
					return;
				}		
			}
			function printNum(){
				// for(var i=0;i<nums.length;i++){
					console.log(nums);
				// }
			}
		</script>
	</body>
</html>

Teacher's code:

<script>
	var nums = [];
	function add1(){
		var v = document.getElementById("num").value;
		// Prepare a flag to indicate that the number does not exist
		var flag = true;
		// Determine whether this v is already in the array
		//  Traverse the array and check whether the number already exists
		for(var i = 0; i < nums.length;i ++){
			if(v == nums[i]){
				flag = false;
				break;// 
			}
		}
		if(flag){
			nums[nums.length] = v;
			alert("Join successfully");
		}else{
			alert("The number already exists");
		}
	}
</script>

Assignment 10

Implement the following functions as required:

/**
* Connect the elements of the passed in array arr into a string using op, and return
*/
function joinArr(op,arr){
    
}

For example:

var ages = [23,24,25,26]
joinArr("&",args) -> 23&24&25&26

code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var ages = [23,24,25,26];
			function joinArr(op,arr){
				var result = ages[0];
				for(var i=1;i<arr.length;i++){
					result += op + ages[i];
				}
				return result;
			}
			console.log(joinArr("&",ages));
		</script>
	</body>
</html>

Assignment 11

Define the following object array

[

{no:9527,name: "Kakashi", age:28,skill: "Millennium kill", tel: "11010"},

{no:9528,name: "Sasuke", age:18,skill: "write wheel eye"},

{no:9529,name: "Naruto", age:22,skill: "mouth escape"},

{no:9530,name: "Luffy", age:23,skill: "rubber pistol", tel: "13813838"},

{no:9531,name: "small meatballs", age:22,skill: "beef meatballs"},

{no:9532,name: "Qi Tian Da Sheng", age:25,skill: "72 changes", tel: "15858"},

{no:9533,name: "Tang Changlao", age:29,skill: "hoop curse"}

]

[1] Find out those over 25 years old (including 25 years old) in the array, and output their name and skill to the console.

[2] Find the elements in the array whose tel attribute is undefined, and output their name and skill to the console.

[3] Set the tel attribute of all elements whose tel is undefined to "00000000000".

[4] Try sorting the elements of the array in order of age.
code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var heros = [ {no:9527,name:"Kakashi",age:28,skill:"Millennium kill",tel:"11011011010"}, {no:9528,name:"Sasuke",age:18,skill:"Write wheel eye"}, {no:9529,name:"Naruto",age:22,skill:"Mouth escape"}, {no:9530,name:"Monkey D Luffy",age:23,skill:"Rubber pistol",tel:"13813813838"}, {no:9531,name:"Meatball",age:22,skill:"Beef balls"}, {no:9532,name:"Qi Tian Da Sheng",age:25,skill:"72 change",tel:"15815815858"}, {no:9533,name:"Elder Tang",age:29,skill:"Hoop spell"} ];
			for(var i=0;i<heros.length;i++){
				if(heros[i].age>=25){
					console.log(heros[i].name);
					console.log(heros[i].skill);
				}
			}
			console.log("-------------------");
			for(var i=0;i<heros.length;i++){
				if(heros[i].tel==undefined){
					console.log(heros[i].name);
					console.log(heros[i].skill);
					heros[i].tel = "00000000000";
				}
			}
			for(var i=0;i<heros.length;i++){
					console.log(heros[i]);
			}
			console.log("-------------------");
			for(var i=0; i<(heros.length-1); ++i){
			    for(var j=0; j<(heros.length-1-i); ++j){
			        if(heros[j].age < heros[j+1].age){
			            var temp = heros[j];
			            heros[j] = heros[j+1];
			            heros[j+1] = temp;
			        }
			    }
			}
			for(var i=0;i<heros.length;i++){
				console.log(heros[i]);
			}
		</script>
	</body>
</html>

Topics: Javascript ECMAScript