How do you handle multi-argument JavaScript functions?-Collection of common programming errors

I have defined my JavaScript function as follows:

function printCompanyName(company1, company2, company3, company4, company5)
{
document.write("
" + company1 + "

"); document.write("
" + company2 + "

"); document.write("
" + company3 + "

"); document.write("
" + company4 + "

"); document.write("
" + company5 + "

"); }

And called it as follows:

printCompanyName("Dell, Microsoft, Apple, Gizmodo, Amazon");

But I get the following output:

Dell, Microsoft, Apple, Gizmodo, Amazon

undefined

undefined

undefined

undefined

What gives!? I have been trying to figure this out for hrs. I want:

Dell
Microsoft
Apple
Gizmodo
Amazon
  1. You want to call:

    printCompanyName("Dell", "Microsoft", "Apple", "Gizmodo", "Amazon");
    

    The way you’re currently doing it you’re passing in one company “Dell, Microsoft, Apple, Gizmodo, Amazon”.

  2. You’re passing a single string that happens to contain 4 commas. Therefore, the first parameter contains that single string, and the other 4 are undefined. (Sisnce you only gave one value)

    Since Javascript parameters are optional, you don’t get an error by not passing values for the other parameters.

    You need to pass 5 different strings with commas between them, like this:

    printCompanyName("Dell", "Microsoft", "Apple", "Gizmodo", "Amazon");
    
  3. Try this:

    printCompanyName("Dell", "Microsoft", "Apple", "Gizmodo", "Amazon");
    

Originally posted 2013-11-09 19:26:27.