1 finding the largest element in a numeric array (using the Math.max function)
Math.max.apply (this, arr)
View Code
2 Convert a numeric array into a function array (each function pops up the corresponding number)
for (var i=0;i<arr.length;i++) { var tmp=Arr[i]; Arr[i]=function(TMP) { returnfunction() { alert (TMP); } } (TMP)} or Arr.map (function(e) { returnfunction() {alert (e)};});
View Code
3 Sort an object array (sorting criteria is the number of attributes per element object)
Objarray.sort (A, b){ return Object.keys (a). length-Object.keys. length;});
View Code
4 Print out Fibonacci using JavaScript (without global variables)
function Fibonacci (n) { if(n==0| | N==1) { return n; } return Fibonacci (n-1) +fibonacci (N-2);} Fibonacci (5);
View Code
5 implements the following syntax functions: var a = (5). Plus (3). Minus (6); 2
number.prototype.plus=function(n) { returnthis +N;} Number.prototype.minus=function(n) { returnthis-N;}
View Code
6 implements the following syntax functions: var a = Add (2) (3) (4); 9
// determines whether a function or a value is returned function Add (n) { var f=function(m) { return Add (n+m); }; f.tostring=f.valueof=function() { return n; }; return F;}
View Code
7 Variable Promotion
if inch window)) { var a = 1;} alert (a);
// [alert:undefined]
View Answer 8 Variable objects
var a = 1, function A (x) { && A (--x); }; alert (a);
// [Alert:1] // precedence: Function parameters > Function declarations > variable declarations // equivalent to function expression b = function (x) {// x && B (--x); // };
View Answer
function A (x) { return x * 2;} var A;alert (a);
// alert:function A (x) {// return x * 2; // }
View Answer
9 Arguments objects
function b (x, Y, a) { arguments[2] = ten; alert (a);} B (1, 2, 3); function b (x, Y, a) { arguments[2] = ten; alert (a);} B (1, 2);
// Alert:10 and alert:undefined // The value of the properties-indexes is shared with the arguments actually passed in. // This share is not really a shared memory address, but 2 different memory addresses, using the JavaScript engine to ensure that 2 values are always the same, of course, if the index value is less than the number of parameters passed in.
View Answer
Ten this
function A () { alert (this);} A.call (null);
// alert: [Object Window] // The call method takes the global object as the value of this if the first parameter passed in to the object caller is null or undefined.
View Answer
44-Channel JS Puzzle
JS Pen questions (not updated regularly)