Flexible Arguments


Using a variable number of arguments to our advantage.
 function merge(root){ 
   for ( var i = 0; i < arguments.length; i++ ) 
     for ( var key in arguments[i] ) 
       root[key] = arguments[i][key]; 
   return root; 
 } 
  
 var merged = merge({name: "John"}, {city: "Boston"}); 
 assert( merged.name == "John", "The original name is intact." ); 
 assert( merged.city == "Boston", "And the city has been copied over." ); 

How can we find the Min/Max number in an array?
 function smallest(array){ 
   return Math.min.apply( Math, array ); 
 } 
 function largest(array){ 
   return Math.max.apply( Math, array ); 
 } 
 assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value."); 
 assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value."); 

Another possible solution:
 function smallest(){ 
   return Math.min.apply( Math, arguments ); 
 } 
 function largest(){ 
   return Math.max.apply( Math, arguments ); 
 } 
 assert(smallest(0, 1, 2, 3) == 0, "Locate the smallest value."); 
 assert(largest(0, 1, 2, 3) == 3, "Locate the largest value."); 

Uh oh, what's going wrong here?
 function highest(){ 
   return arguments.slice(1).sort(function(a,b){
     return b - a;
   });
 } 
 assert(highest(1, 1, 2, 3)[0] == 3, "Get the highest value."); 
 assert(highest(3, 1, 2, 3, 4, 5)[1] == 4, "Verify the results."); 

QUIZ: We must convert array-like objects into actual arrays. Can any built-in methods help?
 // Hint: Arrays have .slice and .splice methods which return new arrays.
function highest(){ 
   return makeArray(arguments).slice(1).sort(function(a,b){
     return b - a;
   });
 } 

 function makeArray(array){
   // Implement me!
 }

 // Expecting: [3,2,1,1]
 assert(highest(1, 1, 2, 3)[0] == 3, "Get the highest value."); 
 // Expecting: [5,4,3,3,2,1]
 assert(highest(3, 1, 2, 3, 4, 5)[1] == 4, "Verify the results."); 

We can use built-in methods to our advantage.
 function highest(){ 
   return makeArray(arguments).sort(function(a,b){
     return b - a;
   });
 } 

 function makeArray(array){
   return Array().slice.call( array );
 }

 assert(highest(1, 1, 2, 3)[0] == 3, "Get the highest value."); 
 assert(highest(3, 1, 2, 3, 4, 5)[1] == 4, "Verify the results."); 

QUIZ: Implement a multiplication function (first argument by largest number).
 function multiMax(multi){ 
   // Make an array of all but the first argument
   var allButFirst = ___;

   // Find the largest number in that array of arguments
   var largestAllButFirst = ___;

   // Return the multiplied result
   return multi * largestAllButFirst;
 } 
 assert( multiMax(3, 1, 2, 3) == 9, "3*3=9 (First arg, by largest.)" ); 

We can use call and apply to build a solution.
 function multiMax(multi){ 
   // Make an array of all but the first argument
   var allButFirst = Array().slice.call( arguments, 1 );

   // Find the largest number in that array of arguments
   var largestAllButFirst = Math.max.apply( Math, allButFirst );

   // Return the multiplied result
   return multi * largestAllButFirst;
 } 
 assert( multiMax(3, 1, 2, 3) == 9, "3*3=9 (First arg, by largest.)" ); 

[b]Clos

你可能感兴趣的:(java)