JavaScript Programming Questions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.