Traditional Culture Encyclopedia - Traditional customs - What language does the front-end brush algorithm use?
What language does the front-end brush algorithm use?
Leecode can use js to brush questions. Is my big js more and more recognized? However, some features of js will drop when you brush the questions. Let me summarize the pit I fell.
The array object in pit 1: JS is a reference object.
In js, besides object, array objects are also reference objects, which is often overlooked. Therefore, when recursively passing an array, you should copy a new array with arr.slice(0), otherwise the array you passed in will be recursively changed to the same level, and the result is wrong.
So as long as you copy the array, you'd better write it like this, unless you really want to refer to it. Please note that there is a big difference between slicing and splicing. If you use splice(0), you can also copy the array, but the original array will be deleted because splice is an operation.
Elements added and deleted, and the return value is a collection of deleted elements. Splice(0) means deleting from idx to the end of the array, so the return value is also this array. Slice can be understood as slice. Omitting end means going to the end of the array. If end is negative,
Count from the end of the array.
The sorting in Pit 2: JS is sorted in dictionary order by default.
Generally, when sorting, it is considered that if it is ascending, there is no need to write the compare function. However, arr.sort () in js is sorted in dictionary order, which leads you to get the desired effect when the array is non-negative, but it is wrong if there is a negative number.
Like arr=?
So remember to write comparison functions, don't be lazy, and it's better to write anonymous functions.
Pit 3: The function has no variable promotion.
In fact, this is generally not easy to commit, mainly because I was combined with Erkeng. When I customized the comparison function, I wrote VARCompare = function (a, b) {returnb; }, yes, but I wrote it under sort, so sort can't find the compare function, so it is arranged in the default character order.
I think there is something wrong with the definition of compare. Defining variables in a function has a "variable promotion", that is, defining vara anywhere in the function is equivalent to defining vara at the beginning of the function, so after getting used to it, you forget that the function is not a variable and there is no variable promotion. You can't find the following definition. So when writing a comparison function,
Write it as an anonymous function.
Pit 4: floating-point number operation is inaccurate.
Try console.log(0.3-0.2) and you will find that it is 0.00998.998995. I find that js can't calculate accurately as long as it has odd floating-point numbers. This should be related to the mechanism of floating-point number storage in js. There is no good solution to this problem. Generally speaking, the answer is to keep 2 or 3 decimal places and use fixed (n) to keep them directly.
N bits is enough, and this method is originally rounded. But there are always cases where you want to take the upper and lower bounds instead of rounding, so you can only hack them by some methods. For example, you want to take the top two: (a *100-b *100)/100.
Pit 5: Judge that the array is empty.
If (! Arr), because arrays are not implicitly converted like variables, you can use arr. length >;; 0, but in some cases, maybe that variable is not an array, so it can't be lengthened, so I saw a more comprehensive method on the Internet, which is to judge the object first.
Whether the bottom is an array type or not depends on the length.
- Previous article:What are the main physical training for children?
- Next article:Definition of Philosophy
- Related articles
- How to draw the year of the tiger
- Make kites and write compositions.
- How to drink Tupperware rose vinegar? How much is Tupperware's rose vinegar?
- Why are snakes afraid of bamboo?
- What is the Dai nationality?
- On the Traditional Country in the Type of Population Growth
- What does industrial fund mean?
- Folk folk prescription: the making method of Sun Simiao, the king of traditional Chinese medicine health care medicine.
- Classification of woody plants in the garden
- What do you mean by five poisons?