English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Common Logical Questions in JavaScript and Solutions

One, count the number of times the value in the array arr is equal to item

function count(arr, item) {
   var count = 0;
   arr.forEach(function(e){
     //e for each element in arr, if e is equal to item then count+1
     e == item ? count++ : 0;
   });
   return count;
 }
function count(arr, item) {
  return arr.filter(function(a){
    return (a==item);
  }).length
}
function count(arr, item) {
var res;
return (res = arr.toString().match(new RegExp("//b"+item+"//b","g")))?res.length:0;
}
function count(arr, item) {
      var count = 0;
      arr.map(function(a) {
        if(a === item) {
          count++;
        }
      });
      return count;
    }

Two, square the second power of each element in the array arr. Do not directly modify the array arr, and return a new array as the result

function square(arr) {
  return arr.map(function(item,index,array){
    return item*item;
  })
}
function square(arr) {
  //Declare a new array to store the results
   var a = [];
   arr.forEach(function(e){
     //Square each element in arr and add it to the a array
     a.push(e*e);
   });
   return a;
 }
function square(arr) {
//Duplicate the arr array
  var newarr = arr.slice(0);
  for (var i=0;i<newarr.length;i++){
    newarr[i]= newarr[i]* newarr[i];
  }
  return newarr;
}
function square(arr) {
  return arr.map(function(item){
    return Math.pow(item, 2);
  })
}

Three, pass the elements of the array arr as parameters to the function fn

function argsAsArray(fn, arr) {
 return fn.apply(this, arr);
 }
function argsAsArray(fn, arr) {
  return function(para1,para2){ 
    return para1.apply(this,para2);
  }(fn,arr);
}

Four, complete the function createModule, after calling it, it should meet the following requirements:

     1、Return an object

     2、The greeting attribute value of the object equals str1,The name attribute value equals str2

     3、The object has a sayIt method that returns a string of greeting attribute value + ',' +name attribute value

function createModule(str1, str2) {
   var obj = {
     greeting : str1,
     name : str2,
     sayIt : function() {
       //Both properties need to be prefixed with this
       return this.greeting+", "+this.name;
     }
   };
   return obj;
 }
//Using constructor method
function createModule(str1, str2) {
  function Obj() {
    this.greeting = str1;
    this.name = str2;
    this.sayIt = function() {
      return this.greeting + ', ' + this.name;
    };
  }
  return new Obj();
}
//Combining constructor and prototype
function createModule(str1, str2) {
  function CreateMod() {
    this.greeting = str1;
    this.name = str2;
  }
  CreateMod.prototype.sayIt = function() {
    return this.greeting + ', ' + this.name;
  }
  return new CreateMod();
}

Five, given that fn is a predefined function, implement the function curryIt, and after the call, it meets the following conditions:

      1Return a function a, and the length property value of a is1(i.e., explicitly declare a to accept one parameter)

      2After calling a, return a function b, and the length property value of b is 1

      3After calling b, return a function c, and the length property value of c is 1

      4After calling c, the result returned is consistent with the return value of calling fn

      5The parameters of fn are the call arguments of functions a, b, c in turn

Input example:

var fn = function (a, b, c) {return a} + b + c}; curryIt(fn) (1) (2) (3);
function curryIt(fn) {
   //Get the number of fn parameters
   var n = fn.length;
   //Declare an array args
   var args = [];
   //Return an anonymous function
   return function(arg) {
     //Place the parameters in the parentheses after curryIt into an array
     args.push(arg);
     //If the number of arguments in args is less than the number of arguments in the fn function,
     //Then execute arguments.callee (which refers to the currently executing function, here the returned anonymous function).
     //Otherwise, return the result of the fn call
     if (args.length < n) {
      return arguments.callee;
     } else return fn.apply("", args);
   }
 }
function curryIt(fn) {
  return function a(xa){
    return function b(xb){
      return function c(xc){
        return fn.call(this,xa,xb,xc);
      };
    };
  };
}

6. Output the element position in the array

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

7. Sum of array

function sum(arr) {
  return eval(arr.join("+"));
};

8. Delete the given element

  function remove(arr, item) {
    for(var i=0, m=arr.length, res=[]; i<m; i++){
      if(item === arr[i]) continue;
      else res.push(arr[i]);
    }
    return res;
  }
function remove(arr, item) {
  var newA=arr.slice(0);
  for(var i=newA.indexOf(item);i>-1;i=newA.indexOf(item)){
    newA.splice(i,1);
  }
  return newA;
}

Summary

That's all for the common logic questions in JavaScript, do you all learn it? The content of this article is still helpful to everyone's learning or work, if you have any questions, you can leave a message to communicate, thank you all for your support of the呐喊 tutorial.

You May Also Like