JS: function in function

Title description To
implement the function functionFunction, the following conditions shall be met after the call:
1. The return value is a function f
2. The function f returned by the call, the return value is the parameter splicing in the order of the call, the splicing character is an English comma plus a space, that is,', '
3, all functions of the number of parameters is 1, and both of type String
input examples:

functionFunction(‘Hello’)(‘world’)

Output example:

Hello, world

Ideas I
didn't understand the topic at first, and didn't know how to write it. Then step by step


function functionFunction(str){
    
    
  //既然要返回函数,那么就要写一个函数进去咯;用函数表达式,因为要让他在执行时才被解析
  var f=function(s){
    
    
  //这里要返回值为按照调用顺序的参数拼接,拼接字符为英文逗号加一个空格
  return str+", "+s;//记得逗号后面加空格啊!!!
}
 //返回函数f
  return f;
}

Other methods
1.

/*看题目要求!!!!
  1.按照调用顺序的参数拼接,拼接字符为英文逗号加一个空格
  2.所有函数的参数数量为 【 1 】,且均为【 String 类型】
*/
    function functionFunction(str){
    
    
     if(typeof str=="string"&&argument.length==1){
     var f=function(s){
    
    
     if(typeof s=="string"&&argument.lenght==1){
      return str+", "+s;
  }
     return f;
 }
 }
}

2. Use the join method

join(): Convert all elements in the array into a string:
syntax [array.join(separator)]
parameter value [separator] optional. Specify the separator to be used. If this parameter is omitted, a comma is used as the separator.

function functionFunction(str){
    
    
  return function(s){
    
    
        return [str,s].join(',');
     }

}

Guess you like

Origin blog.csdn.net/sinat_35803474/article/details/68951062