JS- basis

 A data type

String (string), numbers (number), the array (array), the object (object), an empty (null), Boolean (boolean), undefined (undefined), the function (function)

II. Operators

(1) Arithmetic: plus (+), subtract (-), multiply (*), divide (/), modulo (%), jerk (+), and reduction (-)

(2) relational operators : greater than (>), less than (<), greater than or equal to (> =), less than or equal to (<=), equality (==), congruent (===), not equal (! =) , and incomplete (! ==)

Congruent (===): data types and values ​​are the same

NOTE: NaN3 represents a non-numeric, it is not equal to any value, nor do calculation

document.write((1==false)+"<br/>")//false
document.write((1===true)+"<br/>")//false
document.write((0!=false)+"<br/>")//false
document.write((0!==false)+"<br/>")//true
alert(NaN == NaN)  //flase

(3) logic operations : logical AND (&&), logical OR (||), logical negation (!)  

a

b

a&&b

a||b

!a

!b

true true true true false false
true false false true false true
false false false false true true
false true false true true false
document.write(!(6>2)+'<br/>')//false
document.write((16<789&&12>11)+"<br/>")//true
document.write((16<789||12>11)+"<br/>")//true

(4) bit operation : | (!) (&) , (),, (^), (>>), (<<), (>>>)

document.write((25 & 3)+"<br/>") //1
document.write((25 | 3)+"<br/>")  //27
document.write((2 << 3)+"<br/>")  //16
document.write((4^3)+"<br/>")  //7

(5) assignment operator : (=), (= +), (- =), (= *), (/ =), (=%)

(6) trinocular operations :?

III. Functions

Number (): Any type can be converted into a digital attempt

​
alert(Number(true)); //1,Boolean 类型的 true 和 false 分别转换成 1 和 0
alert(Number(25)); //25,数值型直接返回
alert(Number(null)); //0,空对象返回 0
alert(Number(undefined)); //NaN,undefined 返回 NaN
console.log(Number(24))//24
console.log(Number("24"))//24
console.log(Number("a"))//NaN
console.log(Number("a24"))//NaN
console.log(Number("24a"))//NaN

parseFloat (): Switch to float

parseInt (): to Integer

console.log(parseInt("a24"))//NaN
console.log(parseInt("24a"))//24
console.log(parseInt("a"))//NaN
console.log(parseInt(24))//24
console.log(parseInt("24"))//24

isNaN (num): num variable is determined whether the value is NaN

Math.round (): rounding function

Math.random (): returns a random number between 0-1

Math.max (num1, num2): Returns the larger number

Math.min (num1, num2): returns the smaller number

Math.abs (): absolute value

Math.ceil (): rounding up

Math.floor (): rounded down

Math.pow (x, y): y x ​​to the power

Math.sqrt (num): square root

IV. String

  • Acquisition method string

    1. charAt (3) // get subscript characters 3
    2. charCodeAt (3) // get the next character encoding standard 3
    3. fromCharCode (94) // converted to the character coding
var str="hello,world!"
console.log(str.charAt(3)) //l
console.log(str.charCodeAt(3)) //108
console.log(String.fromCharCode(94)) //^ 
  • Find string methods

    1. indexOf: Gets the string value of the first occurrence of a string
    2. lastIndexOf: Gets the string value in the last occurrence of a string
    3. Alternatively replace string stringObj.replace ( "tmd", "*");
var str1="good good study day day up"
console.log(str1.indexOf("g")) //0
console.log(str1.lastIndexOf("g")) //5
console.log(str1.replace('o','z')) //gzod good study day day up
console.log(str1.replace(/o/ig,'x')) //gxxd gxxd study day day up
console.log(str1.indexOf("day")) //16
  • String comparison method

    1. == and ===
console.log(null==undefined) // 输出:true

console.log(null===undefined) // 输出:false

console.log(null===null) // 输出:true

console.log(undefined===undefined) // 输出:true

console.log(NaN==undefined) // 输出:false

console.log(NaN==null)  // 输出:false

console.log(NaN==NaN)  // 输出:false

console.log(NaN===NaN)  // 输出:false
  • String interception method

    1. substring (strat, end): at the end labeled element does not include the character string taken. end is optional, there is no default all strings from start to finish.
    2. slice (strat, end): at the end labeled element does not include the character string taken. end is optional, there is no default all strings from start to finish.
    3. substr (strat, length): The second parameter is the number of characters in the substring must be numeric. Can not
var str="helloworld"
console.log(str.slice(3,6)) //low
console.log(str.substring(3,6)) //low
console.log(str.substr(3,6)) //loworl
  • String split

    1. split (): The delimiter, split into an array
    2. toLowerCase (): turn lowercase, toUpperCase (): turn uppercase
var str2="How are you doing today"
console.log(str2.split(" ")) //["How", "are", "you", "doing", "today"]

var str="helloworld"
console.log(str.toUpperCase())//HELLOWORLD

var str1="HELLOWORLD"
console.log(str1.toLowerCase())//helloworld

V. Array

  • Creating an array

1. The conventional way:

          var myCars=new Array(); 

          myCars[0]="Saab";       

          myCars[1]="Volvo";

          myCars[2]="BMW";

2. Simple mode: var myCars = new Array ( "Saab", "Volvo", "BMW");

3.字面:var myCars=["Saab","Volvo","BMW"];

  • Array Methods

1.arr.toString (): the array to a string

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f1=fruits.toString()
console.log(f1)  //Banana,Orange,Apple,Mango

2.arr.join (): all the array elements into a string binding.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f2=fruits.join()
console.log(f2) //Banana,Orange,Apple,Mango

3.arr.pop (): Removes the last element in the array and returns that element

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f3=fruits.pop()
console.log(f3) //Mango

4.arr.shift (): Delete the first element in the array and returns that element

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f4=fruits.shift()
console.log(f4) //Banana

5.arr.push (): adding an end of the array element, and returns the length of the array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f5=fruits.push("Lemon")
console.log(f5) //5
console.log(fruits)  //["Banana", "Orange", "Apple", "Mango", "Lemon"]

6. arr.unshift (): Add a beginning of an array element, and returns the array length

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f6=fruits.unshift("Lemon")
console.log(f6) //5
console.log(fruits)  //["Lemon", "Banana", "Orange", "Apple", "Mango"]

7. arr.reverse (): Array Flip

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var f7=fruits.reverse()
console.log(f7) // ["Mango", "Apple", "Orange", "Banana"]

8. delete arr [index]: delete elements, the array will be left undefined empty, replace it with pop () or shift () with delete in

var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0]
console.log(fruits) // [empty, "Orange", "Apple", "Mango"]

9. arr.splice (): spliced ​​array to add new items to the array, you can also delete elements.

//splice() 方法可用于向数组添加新项,第一个参数定义了应添加新元素的下标(拼接)。第二个参数定义应删除多少元素。其余参数(“Lemon”,“Kiwi”)定义要添加的新元素。
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi"); 
console.log(fruits) //["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]

10. arr.concat (): to create a new array by combining (connecting) existing array

var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias", "Linus"];
var arr3 = ["Robin", "Morgan"];
var myChildren = arr1.concat(arr2, arr3); 
console.log(myChildren)  //["Cecilie", "Lone", "Emil", "Tobias", "Linus", "Robin", "Morgan"]

11. arr.slice (index): The cut out a fragment of the array new array.

When Slice (index1, index2) method of imparting two parameters, it is an element taken from index1 index, the subscript does not include the element index2

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,4)
console.log(citrus)  //["Orange", "Lemon", "Apple"]
  • Sorting an array

1. Bubble Sort

//冒泡排序
var arr1=[23,45,2,456,65,24,223,787]
for(var i=1;i<=arr1.length;i++){
	for(var j=0;j<arr1.length-i;j++){
		if(arr1[j]>arr1[j+1]){
			var temp=null;
			temp=arr1[j];
			arr1[j]=arr1[j+1];
			arr1[j+1]=temp;	
		}
	}
}
console.log(arr1)

2.sort Sort: to sort strings case

var arr=["aB","Ba","Ct","As","bs","cd"]
arr.sort()
console.log(arr)//["As", "Ba", "Ct", "aB", "bs", "cd"]

example:

//输出斐波那契额数列数列的前20项(递归)
function f(n){//n为第几项,第n项等于f(n)=f(n-1)+f(n-2)
	if(n==0){
		return 0;
	}else if(n==1){
		return 1;
	}
	return f(n-2)+f(n-1)
}
var arr=[];
for(var i=0;i<20;i++){//i为数组的下标
	arr.push(f(i))
}
console.log(arr)

//山上有一口缸可以装50升水,现在有15升水。老和尚叫小和尚下山挑水,每次可以挑5升。问:小和尚要挑几次水才可以把水缸挑满?
var i=15,n=0;//n为挑水的次数
do{
	i+=5;
	n++;//挑水的次数加1
}while(i<50)
document.write(n)

/*有一个从小到大排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置*/
var a=[1,24,52,66,78,98,109]
var left=[];
var right=[];
function btn(){
	//获取输入框中的值
	var num=document.getElementById("num").value;	
	for(var i=0;i<a.length;i++){
		//如果num大于a[i],就把a[i]放入left数组的最后一位
		if(num>=a[i]){
			left.push(a[i])
		}else{
			right.push(a[i])
		}
    }
	left.push(num)//把输入框中输入的数放入left数组的最后一位
	//把right数组依次放入left数组的最后一位
	for(var k=0;k<right.length;k++){
		left.push(right[k])
	}
	console.log(left)
}

//开发一个标题为“FlipFlop”的游戏应用程序。它从1计数到100,遇到3的倍数就替换为单词“Flip”,5的倍数就替换为单词“Flop”,既为3的倍数又为5的倍数则替换为单词“FlipFlop”。
var str=""
for(var i=1;i<=100;i++){
	if(i%3==0 && i%5!=0){
		document.write("Flip"+'\n')
	}else if(i%5==0 && i%3!=0){
		document.write("Flop"+'\n')
	}else if(i%3==0 && i%5==0){
		document.write("FlipFlop"+'\n')
	}else{
		document.write(i+'\n')
	}
}
document.write(str+"<br/>")

/*已知字符串 “a,a,b,c,c,d”去掉重复的字符,使结果显示 abcd;
统计每个字符出现的次数,结果显示a2、b1、c2、d1
找出出现的次数最多的那个字母 和他相应的次数;
找出出现的次数最少的那个字母 和他相应的次数*/
var str="a,a,b,c,c,d";
var arr=str.split(",")
var obj={}//定义了一个空对象
for(var i in arr){//for in循环,i为arr的下标,arr[i]:数组里的内容
	var n=arr[i]   //n=arr[i]:数组里的内容
	if(!obj[n]){
		obj[n]=1  //obj[n]的值为1
	}else{
		obj[n]++ //obj={a:2,b:1,c:2,d:1}
	}
}
var arr1=[]
for(var j in obj){
	arr1.push(j)  //arr1=["a","b","c","d"]
	var str1=arr1.join("")
}
console.log(str1)//abcd

var maxA=null;//出现的次数最多的那个字母
var maxCount=0;//出现的次数最多的那个字母的次数
var minA=null;//出现的次数最少的那个字母
var minCount=str.length;//出现的次数最少的那个字母的次数
for(var k in obj){ //for in循环,k为obj的属性名,
	console.log(k+obj[k]) //a2 b1 c2 d1
	if(maxCount<obj[k]){
		maxCount=obj[k]
		maxA=k
	}
	if(minCount>obj[k]){
		minCount=obj[k]
		minA=k
	}
}
console.log("出现的次数最多的那个字母为:"+maxA+",出现的次数最多的那个字母的次数为:"+maxCount)
console.log("出现的次数最少的那个字母为:"+minA+",出现的次数最少的那个字母的次数为:"+minCount)

VI. Object

  • Create Object

//方法一
var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};

//方法二
var person = new Object();
person.firstName = "Bill";
person.lastName = "Gates";
person.age = 50;
person.eyeColor = "blue"; 
  • Object Methods

Objects can also have methods

var man=new Object();
man.age=20;
man.name="张三";
man.height="180cm"
man.talk=function(){
	document.write("我的名字是:"+man.name+"我的年龄是:"+man.age+"我的身高是:"+man.height)
}
man.talk()

//第二种方法
var man1={name:"张三",age:20,height:"180cm",talk:function(){
	document.write("我的名字是:"+man1.name+"我的年龄是:"+man1.age+"我的身高是:"+man1.height)
}}
man1.talk()
  • Use this keyword indicates that the current object

    var obj={
        f:function(){
            return this.y
        },
        y:1.2
    }
    var y=obj.f() 
    console.log(y) //1.2

     

 

Released eight original articles · won praise 6 · views 321

Guess you like

Origin blog.csdn.net/yadibaibai/article/details/103494321