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)
- var a = [1, 2, 3, 6, 5, 4];
- var ans = Math.max.apply(null, a);
- 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:
- var ans = Math.max(1, 2, 3, 4, 5, 6);
- console.log(ans); // 6
The second parameter of apply is exactly an array and does not need to be converted.
- var a = [1, 2, 3, 6, 5, 4];
- var ans = eval( 'Math.max(' + a.toString() + ')');
- 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)
- var a = [1, 2, 3, 4, 5, 6];
- var len = a.length;
- for(var i = 0; i < len; i++) {
- var num = a[i];
- (function(num) {
- var f = function() {
- console.log(num);
- };
- a[i] = f;
- })(num);
- }
-
- for(var i = 0; i < len; i++)
- a[i]();
- // 1
- // 2
- // 3
- // 4
- // 5
- // 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)
- var a = {
- name: 'hanzichi',
- age: 10,
- location: 'china'
- };
-
- var b = {
- name: 'curry'
- };
-
- var c = {
- name: 'kobe',
- sex: 'male'
- };
-
- Object.prototype.getLength = function() {
- var num = 0;
- for(var key in this) {
- if(this.hasOwnProperty(key))
- num++;
- }
- return num;
- };
-
- var arr = [a, b, c];
- arr.sort(function(a, b) {
- return a.getLength() > b.getLength();
- });
- 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)
- (function(a, b) {
- var c = a + b;
- console.log(c);
- if(c > 100) return;
- arguments.callee(b, c);
- })(-1, 1);
I don't understand this question. Is it the first n items in the Fibonacci series? Or n...
- Function fn (n ){
- Var a = [];
- A [0] = 0, a [1] = 1;
- For (var I = 2; I <n; I ++)
- A [I] = a [I-1] + a [I-2];
- For (var I = 0; I <n; I ++)
- Console. log (a [I]);
- }
- Fn (5); // 10 indicates the number of expected Fibonacci Series
- // 0
- // 1
- // 1
- // 2
- // 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
- Number.prototype.plus = function(a) {
- return this + a;
- };
-
- Number.prototype.minus = function(a) {
- return this - a;
- };
-
- var a = (5).plus(3).minus(6);
- 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
- function add(a) {
- var temp = function(b) {
- return add(a + b);
- }
- temp.valueOf = temp.toString = function() {
- return a;
- };
- return temp;
- }
- var ans = add(2)(3)(4);
- 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 ):
- function add(num){
- num += ~~add;
- add.num = num;
- return add;
- }
- add.valueOf = add.toString = function(){return add.num};
- var ans = add(3)(4)(5)(6); // 18
- alert(ans);
Do you have any comments or suggestions ~