Reputation: 15652
I want the function to make the arguments into a single string and return it. What have I done incorrectly?
function cooncc(divider, lastdiv){
var returner;
for (var i = 0; i < (arguments.length - 2); i++)
{
returner += arguments[i+2] + divider;
}
returner -= divider;
returner += lastdiv + arguments[arguments.length - 1];
return divider;
}
var output = cooncc(", ", ", and ", "Andy", "Becky", "Caitlin", "Dave", "Erica", "Fergus", "Gaby");
document.body.innerHTML = "<h1>" + output + ".</h1>";
Upvotes: 0
Views: 95
Reputation: 66399
Your function needed major rewrite, here is working version:
function cooncc(){
var arrWords = new Array();
var divider = arguments[0];
var lastdiv = arguments[1];
for (var i = 2; i < arguments.length; i++)
{
var curWord = arguments[i];
if (i == arguments.length - 1 && i > 2) {
arrWords[arrWords.length - 1] += lastdiv + curWord;
}
else {
arrWords.push(curWord);
}
}
return arrWords.join(arguments[0]);
}
(no change in calling it)
Updated jsFiddle: http://jsfiddle.net/yahavbr/LU3pE/1/
Upvotes: 0
Reputation: 234354
You're doing lots of things wrong. Here's a breakdown of what you need to fix:
You're returning divider
! Change that to returner
:
return returner;
This line does not do what you expect:
returner -= divider
You cannot subtract strings from strings unless they're numbers, and that's why you get NaN (not-a-number) in the output.
Try:
returner = returner.substring(0,returner.lastIndexOf(divider));
You did not initialize returner
. That will get you a "undefined" into your string. Initialize to the empty string:
var returner = "";
You're adding the last string twice: once in the loop and then one last time with the last divider. Just stop the loop one earlier:
for (var i = 0; i < (arguments.length - 3); i++)
Finally, think about what happens if you call it like this with only one string to concatenate: cooncc(", ", ", and ", "Andy")
. You can solve this with a guard clause:
if(arguments.length == 3) return arguments[2];
Upvotes: 4
Reputation: 521995
Sidestepping the question, but there are more elegant ways to do what you want to do:
var names = ["Andy", "Becky", "Caitlin", "Dave", "Erica", "Fergus", "Gaby"];
var divider = ', ';
var lastDivider = ' and ';
var concatNames = names.slice(0, -1).join(divider) +
lastDivider +
names[names.length - 1];
alert(concatNames);
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/join
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/slice
Upvotes: 0
Reputation:
you need to initialize returner and you can't use -= with divider. If you put alert(returner) after the returner -= divider you get NaN back. try this fix.
function cooncc(divider, lastdiv){
var returner = "";
for (var i = 0; i < (arguments.length - 3); i++)
{
returner += arguments[i+2] + divider;
}
returner = returner.substring(0,returner.lastIndexOf(divider));
returner += lastdiv + arguments[arguments.length - 1];
return returner;
}
var output = cooncc(", ", ", and ", "Andy", "Becky", "Caitlin", "Dave", "Erica", "Fergus", "Gaby");
document.body.innerHTML = "<h1>" + output + ".</h1>";
Upvotes: 0
Reputation: 5559
Initialize returner?
var returner = "";
And you can't do with strings:
returner -= divider
Upvotes: 0
Reputation: 47183
Make the arguments into a single string? Do you mean you want this output?
, , and AndyBeckyCaitlinDaveEricaFergusGaby
If, yes, you can use this modification of your code:
function cooncc(divider, lastdiv){
var returner = "";
console.log(arguments.length);
for (var i = 0; i < arguments.length; i++) {
returner += arguments[i];
}
return returner;
}
var output = cooncc(", ", ", and ", "Andy", "Becky", "Caitlin", "Dave", "Erica", "Fergus", "Gaby");
document.body.innerHTML = "<h1>" + output + ".</h1>";
Oh, and if you change the last bit in your code from return divider
to return returner
, you get
NaN, and Gaby
although, I'm not sure if this is what you want.
Upvotes: 0