Realize the factorial
function JX(num){
var sum = 1;
for(i=num; i>0; i--){ sum = sum*i }return sum
}
console.log(JX(4));
Copy the code
Maximum minimum value
var max = arr[0];
for(i=0; i<arr.length; i++){if(arr[i]>max){ max = arr[i]; }}console.log(max);
/ / math methods
var max = arr[0];
for(i=0; i<arr.length; i++){ max =Math.max(arr[i],max)
}
console.log(max)
Copy the code
Encapsulation of Max
function Max(arr){
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i]; }}return max;
};
}
Copy the code
Fibonacci numbers
var Fb = function(n) {
if( n==1 || n == 2) {return 1
}else{
return Fb(n-2) + Fb ( n-1)}}function printFb(n){
for( let i =1; i<=n ; i++){
console.log(Fb(i))
}
}
Copy the code
The sorting
function num(a, b){{
return a - b
}}
var arr = [1.9.8.5.6.2.3.7.4.8.6.3.6.8.9]
console.log(arr.sort(num))
Copy the code
Array to heavy
function chong(arr){
for( i= 0, i<= arr.length,i++){
for(j = i+1, i<= arr.length,j++){
if(arr[i] == arr[j]){
arr.splice(j,1)}}}return arr
}
/ / set methods
Copy the code
Take an even number
var temp = [];
for(i=0; i<arr.length; i++){if(arr[i]%2= =0){ temp.push(arr[i]); }}console.log(temp);
filter
console.log(arr.filter(x= >(x%2= =0)))
Copy the code
Inversion array
console.log(arr.reverse());
Copy the code
Copy the array
var arr = [1.2.3.4.5];
var b = arr.concat();
console.log(b);
Copy the code
Json objects are converted to and from strings
- Json string —–> JSON object
- Use the json.parse () function
var jsonStr = '{"name":"zhangsan","age":23,"email":"[email protected]"}';
var json = JSON.parse(jsonStr);
console.log(json);// Output :Object {name: "zhangsan", age: 23, email: "[email protected]"}
Copy the code
- Json object ——> JSON Character string
- JSON.stringify()
var json = {name: "zhangsan".age: 23.email: "[email protected]"};
var jsonStr = JSON.stringify(json);
console.log(jsonStr);/ / output: "{" name" : "zhangsan", "age" : 23, "email" : "[email protected]"}"
Copy the code
String to array
- parseint
- number
Array station Huawei string
- string—->array
- split
- array—->string
- Join passes in the partition symbol
- tostring
Array methods
- increase
- Push is added from the very back of the array
- Unshift is added from the front of the array
- Concat does not change the array structure
- delete
- Pop is removed from the back
- Shift is removed from the front
- Splice (subscript, value)
- change
- arr[index] = value
- Slice (startIndex, endIndex) does not alter the array structure and returns the truncated contents
- check
- indexof
- arr[index]
- Charat (index)
String method
- Slice segmentation
- Indexof lookup
- Concat concates multiple arrays
Encapsulate native Ajax requests
var xhr = new XMLHttpReaqust()
xhr.open('get', uel)
xhr.send()
a.onreadystatechange = function () {
if (a.readyState === 4 && a.status == 200) {
Parse () converts a string to a JSON object
var reponse = JSON.parse(a.responseText);
console.log(reponse)
}
}
Copy the code
Jq encapsulation ajax
$.ajax({
url:"https://douban.uieee.com/v2/movie/top250".type:"get".dataType:"jsonp".success:function(res){
var subjects = res.subjects;
var data = subjects[0];
var img = data.images.small;
var title = data.title;
Copy the code
vue
new Vue({
el: '#app'.data: {
msg: "hello world"
},
// Before creation
beforeCreate() {
var url = 'https://douban.uieee.com/v2/movie/top250';
$.get(url,res= >{
console.log(res);
},"jsonp")}})Copy the code
react
componentDidMount(){
var url = "https://douban.uieee.com/v2/movie/top250";
$.ajax({
url,
type:"get".dataType:"jsonp".success: res= > {
var subjects = res.subjects[0];
var img = subjects.images.small;
var title = subjects.title;
this.setState(() = >{
return {
img,
title
}
})
}
})
}
}
Copy the code
The visible area changes with background scrolling
<style>
body{
height: 2000px;
background: darksalmon;
}
.bg{
width: 500px;
height: 500px;
margin: 100px auto;
background: url('./pexels-photo-698319.jpeg');
background-attachment:fixed;
}
</style>
</head>
<body>
<div class="bg"></div>
</body>
Copy the code