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.
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:
function Getarrmax () { return Math.max.apply (null, a); }
2.
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.
function Fib (n) { if(n===1| | n===2) {return 1;} Else return Fib (n-1) +fib (n-2); } /* Console.log (Fib); */
5.
function (x) { returnthis +x; } function (x) { returnthis-x; } /* Console.log ((5). Plus (3). Minus (6)); */
6.
function Add (num) { var self = arguments.callee; + = ~ Self; = num; return Self ; } function () {return add.num}; /* */
JavaScript Basics----Six interesting JS basic questions and Answers