Common logic questions and solutions in Javascript, javascript Logic

Source: Internet
Author: User

Common logic questions and solutions in Javascript, javascript Logic

1. count the number of times an element in the array arr is equal to an item

Function count (arr, item) {var count = 0; arr. forEach (function (e) {// e is each element in arr. If it is equal to item, 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 ;}

2. Calculate the second side for each element in the array arr. Do not directly modify the array arr and return a new array.

Function square (arr) {return arr. map (function (item, index, array) {return item * item;})} function square (arr) {// declare a new array to store the result var a = []; arr. forEach (function (e) {// calculate the square of each element in arr and add it to array. push (e * e) ;}); return a;} function square (arr) {// copy an 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 );})}

3. Use the elements in the array arr as parameters for calling 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);}

4. Complete the createModule function and meet the following requirements after calling:

1. Return an object

2. Objectgreeting The property value is equal to str1,name The property value is equal to str2.

3. An object existssayIt Method. The string returned by this method isgreetingAttribute Value + ',' + name Attribute Value

Function createModule (str1, str2) {var obj = {greeting: str1, name: str2, sayIt: function () {// both attributes must be prefixed with this return this. greeting + "," + this. name ;}; return obj ;}// use the constructor method to function createModule (str1, str2) {function Obj () {this. greeting = str1; this. name = str2; this. sayIt = function () {return this. greeting + ',' + this. name ;}}return new Obj () ;}// constructor and prototype combination function createModule (str1, str2) {function CreateMod () {this. greeting = str1; this. name = str2;} CreateMod. prototype. sayIt = function () {return this. greeting + ',' + this. name;} return new CreateMod ();}

5. We know that fn is a predefined function to implement the curryIt function. The following conditions are met after the call:

1. Return the value of the length attribute of function a to 1 (explicitly declaring that function a receives a parameter)

2. After calling a, return a function B. The length attribute value of B is 1.

3. After B is called, the value of the length attribute of a function c and c is 1.

4. After Calling c, the returned results are the same as those returned by calling fn.

5. fn parameters are called by functions a, B, and c in sequence.

Input example:

Var fn = function (a, B, c) {return a + B + c}; curryIt (fn) (1) (2) (3); function curryIt (fn) {// obtain the number of fn parameters var n = fn. length; // declare an array args var args = []; // return an anonymous function return function (arg) {// put the parameters in the brackets following curryIt into the array args. push (arg); // if the number of parameters in args is smaller than the number of parameters in the fn function, // execute arguments. callee (which is used to reference the currently executed function. Here is the current returned anonymous function ). // Otherwise, 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 );};};};}

Vi. Position of the output element 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);}

VII. Sum of Arrays

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

8. delete a 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

The common logic questions in Javascript are summarized here. Do you know what you have learned? The content in this article is helpful for your study and work. If you have any questions, you can leave a message. Thank you for your support for the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.