From Manfredhu Blog: http://www.manfredhu.com/2016/04/02/15-veryGoodForUsing/
1. Use reduce to count the number of distinct words in an array
var arr = [ Span class= "hljs-string" > "Apple" , "Orange" , , , " pear ", " Orange "]; function getWordCnt () { return arr.reduce (function (Prev,next,index,arr { Prev[next] = (Prev[next] + 1 ) | | 1 ; //This sentence is the focus return prev; },{});} Console.log (getwordcnt ());
2. Given the string str, check whether it contains consecutive duplicate letters (A-ZA-Z), which contains true, otherwise returns false
function containsRepeatingLetter(str) { return/([a-zA-Z])\1/// \1指代第一个括号的匹配项}
In regular expressions, you use () to group, use slashes and numbers to denote references, \1 refers to the first grouping, and \2 refers to the second grouping. by making [a-za-z] a grouping, and then referencing, you can determine whether there is a continuous repetition of the letters.
3. The FN is known as a pre-defined function that implements the function Curryit
Code that needs to be fully complementary
varfunction(a, b, c) { return a + b + c};curryIt(fn)(1)(2)(3//6function curryIt(fn) { //这里补充}
Try to play, here is the function of the currying
varfn = function(A, B, c) { returnA + B + c};console.log (Curryit (FN) (1)(2)(3));//6 function curryit(fn) { varlen = fn.length;//Get the number of function parameters varSlice =Array. Prototype.slice;vararg = Slice.call (arguments,1);return function() {arg = Arg.concat (Slice.call (arguments));if(Arg.length < Len) {called when equal to the number of parameters required by the function return arguments. callee; }Else{returnFn.apply (NULL, ARG); } }}
JavaScript Programming Questions