String search functions and analytical operations topics

1. Find the string

function isContain(a, b){
  var aLen = a.length
  var bLen = b.length
  for(var i = 0; i < bLen; i++){
    if(b[i]===a[0]){
      for(var j = 1; j < aLen; j++){
        if(i + j >= bLen || a[j] != b[i + j]) {
          break;
        }

        if(j == aLen - 1) {
          return i
        }
      }
    }
  }
  return -1
}
console.log(isContain('34 is', '1234567' )); 
the console.log (isContain ( '35', '1234567' )); 
the console.log (isContain ( '355', '12,354,355' )); 

Results:
2
-1
. 5
@ string matching // cycle full-text, to find the first character string and need to match to match i, if not find a direct return -1 // find in the future, need to traverse the string matching to see whether the character has been follow-up matching, if it is, return the above i

2. function operation, unlimited ride

function multiply(num) {
  var res = num;
  return function inner(nextNum) {
    if(nextNum == undefined) {
      return res;
    } else {
      res = res * nextNum; 
      return inner;
    }
  };
}
console.log(multiply(1)());
console.log(multiply(1)(2)(3)());
console.log(multiply(1)(2)(3)(4)());

结果:
1
6
24
//闭包应用 //Multiply call starts, initialization start value, in order to achieve the chain, we need to return a function // call the function returns directly back judgment result according to the parameters and returns a function or continue to do multiplication

 

Guess you like

Origin www.cnblogs.com/bravo2012/p/11356416.html