views:

75

answers:

3

I have defined my JavaScript function as follows:

function printCompanyName(company1, company2, company3, company4, company5)
{
document.write("<p>" + company1 + "</p>");
document.write("<p>" + company2 + "</p>");
document.write("<p>" + company3 + "</p>");
document.write("<p>" + company4 + "</p>");
document.write("<p>" + company5 + "</p>");
}

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  A: 

Try this:

printCompanyName("Dell", "Microsoft", "Apple", "Gizmodo", "Amazon");
Daniel A. White
+3  A: 

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");
SLaks
+1 for the extra explanation.
ChaosPandion
+2  A: 

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".

brendan
Thank you so much! You saved my life because i was about to take it!
Justice Conder