JavaScript Proficiency Test 01 (Easy Level), how many points can you score? Our aim is to crush the interviewer!

table of Contents

I saw an ability test on the Internet, and it felt very fun, so I took the question to do it, haha

Look at the question (the answer is at the back), all the questions are function encapsulation.

Answer reference


I saw an ability test on the Internet, and it felt very fun, so I took the question to do it, haha

Look at the question (the answer is at the back), all the questions are function encapsulation.

1. Encapsulate a function indexOf(arr,item), find out the position of element item in the given array arr, return -1 if it does not exist

For example: arr [1,3,5,7,9] item 7 returns 3

2. Encapsulate a function sum(arr) to calculate the sum of all elements in a given array arr 

For example: [1,3,5] returns 9

3. Encapsulate a function remove(arr, item) to delete the element equal to item in the array arr, the original array remains unchanged, and the new array is returned

For example, arr: [1,3,5,7] item: 5 returns: [1,3,7] (this is a new array)

4. Encapsulate a function removeWithoutCopy(arr, item), remove the element equal to item in the array arr, operate on the original array, and return this array

For example, arr: [1,3,5,7] item: 5 returns: [1,3,7] (this is the original array)

5. Encapsulate a function prepend(arr, item), add element item at the beginning of the array arr, the original array remains unchanged, and the new array is returned

For example, arr: [1,3,5,7] item: 9 returns: [9,1,3,5,7] 

6. Encapsulate a function count(arr, item) to count the number of occurrences of the element whose value is equal to item in the array arr

For example, arr: [1,3,5,7,5,8,5] item: 5 returns: 3

7. Encapsulate a function duplicatetes(arr) to find out the repeated elements in the array arr

For example, [1, 3, 3, 5, 3, 1, 5, 3,7,7] returns [1,3,57]

8. Encapsulate a function count(start, end) to realize the timer function

(1) From start to end (including start and end), console.log records a number every 100 milliseconds, and the number increases by 1 each time

(2) The first number needs to be output immediately

(3) The returned object needs to include a cancel method to stop timing operations
9. Encapsulate a function closures(arr, fn), closure

(1) Return a function array result with the same length as arr

(2) Run the i-th function in result, which is result[i](), and the result is the same as fn(arr[i])

10. Encapsulate a function secondary(fn), secondary encapsulation

(1) This function returns a result function, after calling the result function, the returned result is consistent with the result of calling function fn

(2) The call parameters of fn are all the parameters after the first parameter of secondary and the call parameters of result

 

Answer reference (do it yourself, not a standard answer)

Answer 1

function indexOf(arr, item) {
    for(var i=0;i<arr.length;i++){
        if(arr[i]==item){
            console.log(i)
            return i;
        }
        
    }
    return -1;
}

Answer 2

function sum(arr) {
    var sum=0;
    arr.forEach(function(item){
        sum+=item;
    })
    return sum
}

Answer 3

function remove(arr, item) {
    var newArr=[];
    arr.forEach(function(a){
        if(a!=item){
            newArr.push(a);
        }
    })
    return newArr
}

Answer 4

function removeWithoutCopy(arr, item) {
   for(let i=0;i<arr.length;i++){
        if(arr[i]==item){
            arr.splice(i,1);
            i--;
        }
    }
    return arr;
}

Answer 5

function prepend(arr, item) {
    var newArr=[item];
    newArr = newArr.concat.apply(newArr,arr)
    return newArr;
}

Answer 6

function count(arr, item) {
    let n=0
    arr.forEach((a)=>{
        if(a===item){
            n++;
        }
    })
    return n;
}

Answer 7

function duplicates(arr) {
    var newArr=[],tempArr=[];
    arr.forEach((a)=>{
        if(tempArr.indexOf(a)!=-1){
        	if(newArr.indexOf(a)==-1){
        		newArr.push(a);
        	}
        }else{
            tempArr.push(a);
        }
    })
    return newArr;
}

Answer 8

function count(start, end) {
        console.log(start);
        var timter = setInterval(function(){
             console.log(++start);
            if(start>=end){
                clearInterval(timter);
            }
        },100)
    return {
        'cancel':function(){
            clearInterval(timter);
        }
    } 
}

Answer 9

function closures(arr, fn) {
    var result=[]
    arr.forEach((a)=>{
       result.push(fn.bind(a,a))
    })
    
    return result;
}

Answer 10

function secondary(fn) {
     var args = Array.prototype.slice.call(arguments,1);
     return function(){
         var args2=Array.prototype.slice.call(arguments,0);
         args = args.concat(args2);
        return  fn.apply(null,args);
     }
}

 

Guess you like

Origin blog.csdn.net/dkm123456/article/details/111562771