Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
jlampstack
23,932 PointsNeed help with function that I'm passing a radio button element too
I've created a function to dynamically add radio buttons to keep my code DRY. My code seems to work fine, except that it returns undefined <input> <input> <input>. I don't want the first item returned to be undefined. They should all be <input>. How can I solve this?
let radioBtn = document.createElement('INPUT');
let radioButtons = function(btn) {
let output;
for(let i=0; i<3; i++) {
output += btn.outerHTML;
}
return output;
}
1 Answer
Calin Bogdan
14,921 PointsHello!
The correct for syntax is
for (let i=0; i < 3; i++)
You didn't declare the i in the loop. That should fix it.
Jordany Rosas
Courses Plus Student 4,557 PointsJordany Rosas
Courses Plus Student 4,557 PointsWhen you're adding the first output, output is still undefined, so it's going to be undefined until it knows what kind of information it's dealing with. You can set output = btn.outerHTML when you define it, then run the loop while i < 2. Not sure if that's what you're looking for.