JavaScript Basics----Six interesting JS basic questions and Answers

Source: Internet
Author: User

Topic:


1. Find the largest element in a numeric array (using the Math.max function)
2. Convert a numeric array into a function array (each function pops up the corresponding number)
3. Sort an object array (sorting criteria is the number of attributes per element object)
4. Use JavaScript to print out the number of Fibonacci (without using global variables)
5. Implement the following syntax functions: var a = (5). Plus (3). Minus (6); 2
6. Implement the following syntax functions: var a = Add (2) (3) (4);//9


Answer:
1.

    1. var a = [10,1,2,3,4,8,9]; function Max (a) {                        var len = a.length-1;                         var maxnum=A[len];                          while (len--) {                 Maxnum=Math.max (Maxnum,a[len])                        }                        return  maxnum;                }                alert (Max (a));

Another solution:

    1. function Getarrmax () {                return Math.max.apply (null, a);                }

2.

  1.  var  getnumfun = [];  function   Tofun (a) { var  len = A.length;  while  (Len-- 
          
           function 
            (i) {getnumfun[i]  = fu                  Nction   () {alert (a[i]);                                 }}) (Len);        }}  /*   Tofun (a);          GETNUMFUN[5] ();  */ 

3.

varObjarr =[{A:2,b:1}, {d:5}, {a:1,b:2,c:3} ];functionrule (o1,o2) {varSizeobj =function(obj) {varSize=0;  for(varIinchOBJ) size++; returnsize; }       returnSizeobj (O1)-Sizeobj (O2); }/*objarr.sort (rule);        Console.log (Objarr[0]); */

4.

    1. function Fib (n) {       if(n===1| | n===2) {return 1;}        Else return Fib (n-1) +fib (n-2);     } /*     Console.log (Fib);    */

5.

    1. function (x) {     returnthis +x;    }     function (x) {     returnthis-x;    } /*     Console.log ((5). Plus (3). Minus (6));   */


6.

    1. function Add (num) {  var self = arguments.callee;   + = ~ Self;   = num;   return Self ;  }   function () {return  add.num}; /* */

JavaScript Basics----Six interesting JS basic questions and Answers

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.