Using for loops — Version 2

Poor design algorithm

Argument list = []


Argument list = ["red"]


Argument list = ["red", 123]


Argument list = ["red", 123, true]


Good design algorithm

Argument list = []


Argument list = ["red"]


Argument list = ["red", 123]


Argument list = ["red", 123, true]
Picture of an argument list
// We have 1 or more arguments
// Create a string to start the list. Begin with start of list
var Result = "["

// Process each of the arguments in turn.
for (arg_num = 0; // Start at the first argument
       arg_num < arguments.length; // Continue while at
                                                       // an argument

     arg_num += 1 // Advance to next argument
     )

{

   if ( arg_num === 0) { // First argument is a special case,
                                    // no preceeding comma

         Result += typeof arguments[arg_num];
   }
   else {
// All other arguments have a preceeding comma
         Result += ", " + typeof arguments[arg_num];
   }
}


Result += "]"; // Terminate the type list
// We have 1 or more arguments
// Create a string to hold the first argument
var Result = "[" + typeof arguments[0];

// Process each of the arguments in turn.
for (arg_num = 1; // Start at the first argument
       arg_num < arguments.length; // Continue while at
                                                       // an argument

     arg_num += 1 // Advance to next argument
     )



{

    // Add type to the result.
     Result += typeof arguments[arg_num] + ", ";
}





Result = Result + "]"; // Terminate the type list