This is the 9th day of my participation in the August More Text Challenge
☆☆☆ Today’s content (using functions to achieve specific functions) is important!! Is also the most basic ability requirements! Do do do
Can serve as a beginner’s learning route, can refer to, learning progress is not particularly driven! Keep updating,
With their own understanding, simple and comprehensive summary of the basic knowledge points, most of the content for the vernacular content, the foundation of the foundation of the foundation to deepen understanding!
Consolidate the basic knowledge of learning JavaScript, deepen the understanding of memory, to build a solid foundation to high-rise! Come on
For more details on learning – recommended view MDN-JavaScript documentation, (little Red Book/white paper/blue book /..) Learn to advance!
Read more: Review of previous updates
【 Relearn JS】 Study every day to consolidate the foundation: 【 Day1 】
【 DAY2 】, 【day3】, 【day4】, 【day5】, 【day6】, 【day7】, 【day8】,
☆☆☆ Today’s content is more important, but also the most basic ability requirements in the work! Do do do
Let’s get started! Take each day seriously!!
1, comb the knowledge learned yesterday
-
Array deduplicating:
-
Plan 2: Order the array 1 1 1 33 33 55 66 100
If the previous number! = the last number is stored in the array 1, 33, 55, 66, 100
for (var i = 0; i < arr.length; i++) {
if(arr[i] ! = arr[i +1]) {
brr.push(arr[i])
}
}
Copy the code
- Sorting:
Bubble sort
Selective sorting
sort(function (a, b) {
return a - b
})
Copy the code
-
You can add an array using forEach indexOf Map filter reduce
-
String object:
CharAt ()
charCodeAt
String.fromCharCode
indexOf
lastIndexOf
substr
substring
split
replace
trim
toLowerCase
2,Math
Object mathematical object
Note: The Math object does not need to be defined to invoke methods directly in math. mode
2.1 Three methods of rounding:
-
Math.floor(m) round down math.floor (4.9) 4 math.floor (-4.9) -5 to get the largest integer less than m
-
Math.ceil(m) rounding math.ceil (4.3) 5 Math.ceil(-4.3) -4 yields the smallest integer greater than m
-
Math.round(m) Round math.round (4.5) 5
-
Math.abs(m) takes the absolute value
-
Math.pow(m,n) gets m to the NTH power
-
Math. SQRT (m) to take root
-
Math.max(multiple numbers) takes the maximum value
-
Math. Min () the minimum value
-
Math.min.apply(null, []) gets the minimum value of the array
-
Math.max.apply(null, []) gets the maximum value of the array
-
Math.random() gets a random decimal between [0,1)
To obtain any interval value formula:
Math.round(Math.random() * (max - min) + min)
Copy the code
Package function functions:
Define a function to get arbitrary interval value data:
function rand(min,max){
return Math.round( Math.random()_(max-min) + min )
}
Copy the code
3,Date
object
Definition:
var d = new Date(); Gets the current system time
toLocaleString
() Convert the date to local time formatgetFullYear
() for yearsgetMonth
() month gets 0–11 using d.goetmonth () + 1getDate
(),getHours
() hoursgetMinutes
()getSeconds
() secondsgetDay()
Week 0- 60 0 indicates Sunday
Write a function functiondateToString()
4.1 Function Display a customized date and time format
function dateToString(now) {
var str1 = now.getFullYear() + The '-' + (now.getMonth() + 1) + The '-' + now.getDate()
var h = toTwo(now.getHours())
var m = toTwo(now.getMinutes())
var s = toTwo(now.getSeconds())
var str2 = h + ':' + m + ':' + s
return str1 + ' ' + str2
}
Copy the code
4.2 Functions Functions0
Fill a
The function checks whether the passed parameter is less than 10. If less than 10, concatenate 0 in front of the number
function toTwo(num) {
// Returns a two-digit number
return num < 10 ? '0' + num : num
}
Copy the code
5, custom time format
There are no hours and seconds. The default is 8 a.m
var d = new Date('1998-09-09')
Copy the code
6, write a function:stringToDate()
The date () function converts a string to a date
function stringToDate(str) {
return new Date(str)
}
Copy the code
7. Time difference:getTime()
The now () function returns the milliseconds between midnight, 1970.1.1 and now
Time difference function
function diff(start, end) {
// Returns the time difference in seconds
return Math.abs(start.getTime() - end.getTime()) / 1000
}
Copy the code
8. Set the timesetDate()
Set the time a few days later
SetDate (current date + number of days) The setDate() method changes
The result of the original day object
Such as:
var now = new Date(a)// Set the time after 9 days
now.setDate(now.getDate() + 9)
alert(now)
Copy the code
SetTime () usage:
Date object.setTime (current date ms value + number of days * 1000 * 3600 * 24)
9. TimersetInterval
- Definition:
var timer = setInterval(Tasks to be performed, interval time)// Time default: milliseconds
Copy the code
The typical task to perform is a function function
- Clear timer:
ClearInterval (Timer name);
Come on, dream chaser
Learning is a continuous process, stick to it, there will be harvest!
Accumulate over a long period, consolidate the foundation, early into Dachang!
It’s not easy to keep it up, and it’s not easy to keep it up. You’re great!
Calm Down & Carry On!