Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

why does this code not work?

    function printD(html) {
            return document.write(html);
    };


    var alphabetsZ = ["A", "B", "C"];


    function olArr(liArr) {
        var listz = "<ol>";
        for (var i = 0; i < liArr.length; i += 1) {
            listz += "<li>" + liArr[i] + "</li>";
        };
        listz += "</ol>";
    };

    printD( olArr( alphabetsZ ) );
// output -  undefined

i know the code is fine in general and if i put printD(listz); within the function and call the function as normal it will work fine. but trying a different way that i did, to me my way should work too. i know the problem is mainly :

    printD( olArr( alphabetsZ ) );

it comes off as undefined even though the code looks fine to me too! my guess is you cannot use function within a function?

1 Answer

You need to add a return statement. A function will return undefined by default. In order to return a value you need to add the return statement.

    function printD(html) {
            return document.write(html);
    };


    var alphabetsZ = ["A", "B", "C"];


    function olArr(liArr) {
        var listz = "<ol>";
        for (var i = 0; i < liArr.length; i += 1) {
            listz += "<li>" + liArr[i] + "</li>";
        };
      return  listz += "</ol>";
    };

    printD( olArr( alphabetsZ ) );

omg i feel so stupid that i forgot to add a return. thanks for the help Elijah