Using for loops — Version 1

Incorrect algorithm

Argument list = []


Argument list = ["red"]


Argument list = ["red", 123]


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


Correct poor 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
// A string to hold the arguments. 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
)


{

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






Result += "]"; // Terminate the type 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