Uncle Tom's 6-way JavaScript programming problem
Look at Uncle Tom's blog, which has an article (poke here) at the end of the 6 programming questions, so I also try, we can try first.
1. Find the largest element in a numeric array (using the Math.max function)
123 |
var a = [1, 2, 3, 6, 5, 4]; var ans = Math.max.apply( null , a); console.log(ans); // 6 |
This is a clever use of apply, if not an array, is a lot of numbers to find the maximum value, we know that can be:
12 |
var ans = Math.max(1, 2, 3, 4, 5, 6); console.log(ans); // 6 |
The second parameter of apply is an array, which is not converted.
There is also an implementation using eval+tostring:
123 |
var a = [1, 2, 3, 6, 5, 4]; var ans = eval( ‘Math.max(‘ + a.toString() + ‘)‘ ); console.log(ans); // 6 |
2. Convert a numeric array into a function array (each function pops up the corresponding number)
1234567891011121314151617181920 |
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 to give n a tag, pop the corresponding tag corresponding to the number is a type of problem, with a closure to save the variable to memory.
3. Sort an object array (sorting criteria is the number of attributes per element object)
1234567891011121314151617181920212223242526272829 |
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 problem is not difficult, array sorting, of course, sort, sorting criteria is the number of properties of the object, you can write a function calculation, note may be judged by hasOwnProperty.
4. Use JavaScript to print out the number of Fibonacci (without using global variables)
123456 |
( function (a, b) { var c = a + b; console.log(c); if (c > 100) return ; arguments.callee(b, c); })(-1, 1); |
This question is not clear, is the Fibonacci sequence of the first n? or the nth item ...
123456789101112131415 |
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表示需要的斐波那契数列个数
// 0
// 1
// 1
// 2
// 3
|
Instead of using global variables, I write them in a function. It's a local variable, right? What do you think?
5, to achieve the following syntax functions: var a = (5). Plus (3). Minus (6); 2
12345678910 |
number.prototype.plus = function (a) { &NBSP;&NBSP; Code class= "javascript keyword" >return this + A; number.prototype.minus = function (a) { &NBSP;&NBSP; return this - A; var a = (5). Plus (3). Minus (6); console.log (a); //2 |
Add the extension method directly on the number object, it is not good in legend, but I can not think of a better way ...
6, to achieve the following syntax function: var a = Add (2) (3) (4); 9
1234567891011 |
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 valueof and ToString, refer to "valueof and ToString" for details.
See also a very elegant way of writing (from Gaubee):
12345678 |
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); |
Uncle Tom's 6-Way JS Topic