Solve the JavaScript problem of Uncle Tom

Source: Internet
Author: User

Original reference, http://www.cnblogs.com/TomXu/archive/2012/02/10/2342098.html

Uncle Tom's Blog in-depth understanding of the JavaScript series is really our guiding beacon to learn JavaScript, http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html

Friends who are interested to learn it by themselves. The answer to his question is 6.

First question: Find the largest element in a numeric array (using the Match.max function)

The original answer is this.

functionGetmax (Array) {
var max= array[0];
Array.ForEach (function (Element) {
Max<element?max=element:max;
}) return max;
}
var arr = [12, 5 8, 130 44,99,888];
console. Log (getmax (arr))

See the hint, check the Match.max document Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/max

Find out that it's just as it should be.

 var arr = [12, 5 8, 130 44,99,888]; 
console. Log (math. Max. apply (null

In fact, this question is the use of the test and the usual multi-API documentation is familiar. It should be very simple to be familiar.

The second question: converts a numeric array into a function array (each function pops up the corresponding number)

functionnumbertofunction (arr) {varFunarr = [];    Arr.foreach (convert); functionConvert (element,index,array) {functionlog (Element) {return function() {console.log (element);};    } funarr.push (log (element)); }    returnFunarr;}vararr = [12, 5, 8, 130, 44,99,888];varFuna =numbertofunction (arr);//validation result Funa.foreach (function(elment) {Elment.call ();});

I understand this problem is the concept of closures, that is, closures. There are two points to note

1. The loop cannot use closures so a function convert is created.

2. If you want to accept the parameter element of convert in push, you cannot use closures. All add function log again. Otherwise, you can create a function array, but the result is not printed in the array.

question three: sort an object array (sorting criteria is the number of attributes per element object)

varPersons =[{name:' Jason ', Age:20, Gender:' Male '}, {name:' Tom ', Age:18}, {name:' May ', Age:18, Gender:' Female ', High:' 165cm '}];p Ersons.sort (function(A, b) {if(Object.getownpropertynames (a). length >Object.getownpropertynames (b). Length) {        return1; }    if(Object.getownpropertynames (a) .length<Object.getownpropertynames (b). Length) {        return-1; }    return0;}); Console.log (persons);

Object.keys (). Length can also. Test API Common Sense Array.Sort,object.getownpropertynames() or Object.keys ()

question fourth: print out Fibonacci with JavaScript (no global variables)

 function   Pfibonacci () { var  arrfib = [0,1 var  count = arguments[0]?arguments[0]:10     return   {print:  function   0 1 for  (var  i = 2; i< count;i++< Span style= "color: #000000;"                ) {Arrfib[i]  = arrfib[i-1] + arrfib[i-2). print (); 

Question Fifth: The function of implementing the following syntax: var a = (5). Plus (3). Minus (6);

function (x) {    return this +function(x) {    Console.log (  This );     return  this- x;};
Console.log ((5). Plus (3). Minus (6)); 2

Test the use of prototype.

Span style= "font-size:15px;" > sixth: The ability to implement the following syntax: var a = Add (2) (3) (4);//9

var function () {    var x = arguments[0];     return function () {        + = arguments[0]        ; return function () {            return x + = Arguments[0];}};    };
Console.log (Add (2) (3) (4));//9

This problem is very tangled, want to write an Add () () () (...), the infinite method fails. can only be fixed 3 heavy connected.

Solve the JavaScript problem of Uncle Tom

Related Article

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.