Uncle Tom's six questions about javascript programming

Source: Internet
Author: User

Uncle Tom's six questions about javascript programming

Check out Uncle Tom's blog post, which contains the last six programming questions (here). So I will try again. You can try it first.



1. Find the largest element in the number array (using the Math. max function)

 
 
  1. var a = [1, 2, 3, 6, 5, 4]; 
  2. var ans = Math.max.apply(null, a); 
  3. console.log(ans);  // 6 

Apply is cleverly used for this question. If it is not an array, many numbers are used to calculate the maximum value. We know that we can do this:

 
 
  1. var ans = Math.max(1, 2, 3, 4, 5, 6); 
  2. console.log(ans); // 6 

The second parameter of apply is exactly an array and does not need to be converted.

 
 
  1. var a = [1, 2, 3, 6, 5, 4]; 
  2. var ans = eval( 'Math.max(' + a.toString() + ')'); 
  3. console.log(ans); // 6 

There is also an implementation using eval + toString:

2. convert a number array to a function Array (corresponding numbers are displayed for each function)

 
 
  1. var a = [1, 2, 3, 4, 5, 6]; 
  2. var len = a.length; 
  3. for(var i = 0; i < len; i++) { 
  4.   var num = a[i]; 
  5.   (function(num) { 
  6.     var f = function() { 
  7.       console.log(num); 
  8.     }; 
  9.     a[i] = f; 
  10.   })(num); 
  11.   
  12. for(var i = 0; i < len; i++) 
  13.   a[i](); 
  14. // 1 
  15. // 2 
  16. // 3 
  17. // 4 
  18. // 5 
  19. // 6 

I think this is a type of question corresponding to the corresponding tag displayed for n a tags. Just use the closure to save the variable to the memory.

3. Sort the object array (the sorting condition is the number of attributes of each element object)

 
 
  1. var a = { 
  2.   name: 'hanzichi', 
  3.   age: 10, 
  4.   location: 'china' 
  5. }; 
  6.   
  7. var b = { 
  8.   name: 'curry' 
  9. }; 
  10.   
  11. var c = { 
  12.   name: 'kobe', 
  13.   sex: 'male' 
  14. }; 
  15.   
  16. Object.prototype.getLength =  function() { 
  17.   var num = 0; 
  18.   for(var key in this) { 
  19.     if(this.hasOwnProperty(key)) 
  20.       num++; 
  21.   } 
  22.   return num; 
  23. }; 
  24.   
  25. var arr = [a, b, c]; 
  26. arr.sort(function(a, b) { 
  27.   return a.getLength() > b.getLength(); 
  28. }); 
  29. console.log(arr); 

This is not difficult. array sorting, of course, is sort. The sorting condition is the number of attributes of an object. You can write a function to calculate the number. Note that you may need to use hasOwnProperty to determine the number.

4. print the number of Fibonacci using JavaScript (no global variable is used)

 
 
  1. (function(a, b) { 
  2.   var c = a + b; 
  3.   console.log(c); 
  4.   if(c > 100) return; 
  5.   arguments.callee(b, c); 
  6. })(-1, 1); 

I don't understand this question. Is it the first n items in the Fibonacci series? Or n...

 
 
  1. Function fn (n ){
  2. Var a = [];
  3. A [0] = 0, a [1] = 1;
  4. For (var I = 2; I <n; I ++)
  5. A [I] = a [I-1] + a [I-2];
  6. For (var I = 0; I <n; I ++)
  7. Console. log (a [I]);
  8. }
  9. Fn (5); // 10 indicates the number of expected Fibonacci Series
  10. // 0
  11. // 1
  12. // 1
  13. // 2
  14. // 3

If we don't use global variables, it should be local variables if I write them in the function. Is that all right? What do you think?

5. Implement the following syntax functions: var a = (5). plus (3). minus (6); // 2

 
 
  1. Number.prototype.plus = function(a) { 
  2.   return this + a; 
  3. }; 
  4.   
  5. Number.prototype.minus = function(a) { 
  6.   return this - a; 
  7. }; 
  8.   
  9. var a = (5).plus(3).minus(6); 
  10. console.log(a); // 2 

Simply add the Extension Method to the Number object. The legend suggests this is not good, but I cannot think of a better way...

6. Implement the following syntax functions: var a = add (2) (3) (4); // 9

 
 
  1. function add(a) { 
  2.   var temp = function(b) { 
  3.     return add(a + b); 
  4.   } 
  5.   temp.valueOf = temp.toString = function() { 
  6.     return a; 
  7.   }; 
  8.   return temp; 
  9. var ans = add(2)(3)(4); 
  10. console.log(ans); // 9 

For more information about valueOf and toString, see valueOf and toString.

In addition, we can see a very elegant way (from Gaubee ):

 
 
  1. function add(num){ 
  2.   num += ~~add; 
  3.   add.num = num; 
  4.   return add; 
  5. add.valueOf = add.toString = function(){return add.num}; 
  6. var ans = add(3)(4)(5)(6);  // 18 
  7. alert(ans); 

Do you have any comments or suggestions ~

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.