js capability assessment classic problem of the (two) function

1, fix a given js code, the function definition problems

//看返回值 getValue(),原本的函数定义便不符合需求,以变量的形式存储,方便调用
function functions(flag) {
    if (flag) {
      var getValue=function(){return 'a';}//function getValue() { return 'a'; }
    } else {
      var getValue=function(){return 'b';}//function getValue() { return 'b'; }
    }

    return getValue();
}

2, is called modified parseInt js code to make it through all the test cases.

The input: '12px', output: 12

function parse2Int(num) {
   var i=0;
   while(num[i]>='0' && num[i]<='9'){
       i++;    
   }
    return parseInt(num.slice(0,i));
}

3, to achieve a point timer, claim
(1), from start to end (start and comprising End), a digital console.log every 100 milliseconds, every increase of figures 1
(2), the returned object needs to contain a cancel method, the timing for stopping the operation
(3), a first number of the required output immediately

方法一:setInterval和clearInterval,setInterval方法主要是调用函数执行
function count(start, end) {
    console.log(start++);
    var st=setInterval(function(){
        if(start<=end)
            console.log(start++);
    },100);
    return {
        cancel: function(){clearInterval(st);}
    }
}
方法二:setTimeout和clearTimeout,setTimeout方法主要是采用递归的方式执行
function count(start, end) {
    if(start<=end){
        console.log(start++);
        st=setTimeout(function(){//此处有一点,st的定义,不能用var st=……
            count(start,end);
        },100);
    }
    return {
        cancel: function(){clearTimeout(st);}
    }
}

4, a function to achieve fizzBuzz, num relationship parameter and the return value is as follows:
(1) if num simultaneously divisible by 3 and 5, FizzBuzz returns the string
(2) If num is divisible by 3, Fizz returns the string
(3) If num be divisible by 5, returns the string Buzz
(. 4) is empty or not, if the parameter type Number, returns to false
(5) to rest, the return parameters num

function fizzBuzz(num) {
    if(num==null ||typeof(num)!='number')
         return false;
    if(num%15==0){
        return 'fizzbuzz';
    }else if(num%5==0) return 'buzz';
     else if(num%3==0) return 'fizz';
    return num;
}

5, the elements of the array arr as calling the function fn.

输入:function (greeting, name, punctuation) {return greeting + ', ' + name + (punctuation || '!');}, ['Hello', 'Ellie', '!']

Output: Hello, Ellie!

//函数名.apply(上下文,数组名)
//函数名.call(上下文,各参数)
function argsAsArray(fn, arr) {
    return fn.apply(this,arr);
}

6, the execution of the function fn context object obj to

输入:function () {return this.greeting + ', ' + this.name + '!!!';}, {greeting: 'Hello', name: 'Rebecca'}

Output: Hello, Rebecca !!!

//方法一:利用bind绑定函数的上下文,再调用函数
function speak(fn, obj) {
    return fn.bind(obj)();
}
//方法二:call第一个参数为函数的上下文,对第一个参数进行赋值即可
function speak(fn, obj) {
    return fn.call(obj);
}
//方法三:apply与call同,第一个参数为函数的上下文,对第一个参数进行赋值即可
function speak(fn, obj) {   
    return fn.apply(obj);
}

7, implementing a function functionFunction, after the call to meet the following conditions:
(1) a return value of the function F
(2) F function call returns, the return value is a comma and a space according to the parameters of the splicing calling sequence, splicing character, i.e., ''
(3) the number of arguments of all functions of 1, and are of type String

Input: functionFunction ( 'Hello') ( 'world')

Output: Hello, world

//此函数仅限案例中输入输出的格式,函数名(参数一)(参数二)
function functionFunction(str) {
    return function(s){
        return str+', ' +s;
    }
}

8, to achieve a function makeClosures, after the call to meet the following conditions:
(1) returns an array of functions result, the same length as ARR
(2) operation result in the i-th function, i.e., result [i] (), the result of fn (arr [ i]) the same

Input: [1, 2, 3], function (x) {return x * x;}

Output: 4

//此题主要考察的是数组内如何存放函数,将函数压入到数组内,直接将数组返回即可。
function makeClosures(arr, fn) {
    var newArray=[];
    arr.forEach(function(e){
        newArray.push(function(){
            return fn(e);
        })
    })
    return newArray;
}

9, performing the known function fn requires three parameters. Implement functions partial, after the call to meet the following conditions:
(1) function returns a result, the function accepts a parameter
(2) execution result (str3), and returns the result fn (str1, str2, str3) consistent

输入:var sayIt = function(greeting, name, punctuation) {     return greeting + ', ' + name + (punctuation || '!'); };  partial(sayIt, 'Hello', 'Ellie')('!!!');

Output: Hello, Ellie !!!

//此题主要是partial对fn的一个二次封装函数
function partial(fn, str1, str2) {
    var result=function(str3){
        return fn(str1, str2,str3);
    }
    return result;
}

10, may receive a useArguments function and the above parameters. Please implement a function useArguments, returns the result of the sum of all call parameters. This question is all the test parameters of type Number, without regard to parameter conversion.

Input: 1, 2, 3, 4

Output: 10

//此题的重点是利用函数的arguments,获取此函数传递的参数
function useArguments() {
    var sum=0;
    for(i=0;i<arguments.length;i++){
        sum+=arguments[i];
    }
    return sum;
}

11, to realize the function CALLIT, after the call to meet the following conditions
(1) after the result returned as the result of the call to fn
of all parameters after (2) fn call parameters for the first parameter of CALLIT

//对于函数的arguments是不能进行处理的,arguments.slice会无效,甚至产生错误,因此采用下面这种方式产生新的数组
function callIt(fn) {
   return fn.apply(this,[].slice.call(arguments,1)) ;
}

12, to realize the function partialUsingArguments, after the call to meet the following conditions:
(1) function returns a result
after (2) call result, consistent results with the results returned by calling the function fn
(3) is fn call parameters of the first parameter partialUsingArguments all call parameters and the result parameters after

//利用arguments获取调用函数时传递的参数,此处需要注意,arguments在哪个函数内部,便获取该函数调用时参数
function partialUsingArguments(fn) {
    var arg=[].slice.call(arguments,1);
    function result(){
        return fn.apply(this,arg.concat([].slice.call(arguments)));
    }
    return result;
}

13, is known as a predefined function fn, implement a function curryIt, after the call to meet the following conditions:
(1) a function that returns a, a length of an attribute value (i.e., a receiving explicitly declare a parameter)
(2) call after a, the function returns a b, b 1 is the length property
after (3) call b, returns a function c, c 1 is the length property
after (4) call c, results returned call the return value fn consistent with
(5) fn parameters were a function call parameters a, b, c of

Input: var fn = function (a, b, c) {return a + b + c}; curryIt (fn) (1) (2) (3);

Output: 6

//方法一:利用局部递归的方式执行内部函数,直至函数内部return
function curryIt(fn) {
    var n=fn.length;
    var arr=[];
    var result=function(x){
        arr.push(x);
        if(arr.length==n) return fn.apply(this,arr);
        return result;
    }
    return result;
}
//方法二:利用函数签到函数的方式进行,此方法仅限三个括号即函数名(fn)(参数一)(参数二)(参数三)
function curryIt(fn) {
    return function(x){
        return function(y){
            return function(z){
                return fn.call(this,x,y,z);
            }
        }
    }
}

Conclusion:

Examine the question currently function on here, I will only my understanding and solution posted, and we want to own some reference and reference.

Published 22 original articles · won praise 28 · views 50000 +

Guess you like

Origin blog.csdn.net/zfan520/article/details/81227320