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

JavaScript JavaScript Arrays Loop Through Arrays Loop Through an Array

createListItems is not defined

Hi everyone, this is my code and for some reason i keep getting createListItems is not defined. Where have I gone wrong?

const playlist = [ 'So What', 'Respect', 'What a Wonderful World', 'At Last', 'Three Little Birds', 'The Way You Look Tonight' ];

function createListItems(arr) { let items = ''; for ( let i = 0; i < arr.length; i++) { items += <li>${ arr[i] }</li>; } return items; }

thank you.

2 Answers

Steven Parker
Steven Parker
231,248 Points

When posting code, always use Markdown formatting to preserve the code's appearance,. And for when your question refers to something you are doing in a workspace, an even better way is to make a snapshot of your workspace and post a link to it. You may also want to take a look at this video about Posting a Question.

But there's no obvious problem in this code (other than the accent marks did not come through). Perhaps the issue is in other project code? A snapshot would allow us to see.

Damien Lavizzo
Damien Lavizzo
4,265 Points

You may want to check your HTML, there could be a culprit tucked away in there. If you're getting an error that createListItems is not defined, there might be a spelling error, syntax error, or you may not have called the right script. Example:

JavaScript:

const playlist = [
    'So What',
    'Respect',
    'What a Wonderful World',
    'At Last',
    'Three Little Birds',
    'The Way You Look Tonight'
];

// Define a function that accepts an array as an argument

function createList(arr) {
    let items = '';
    // Add a for loop that iterates through the array items passed to this function 
    for (let counter = 0; counter < arr.length; counter++) {
        items += `<li>${arr[counter]}</li>`;
    } 
    return items;
};

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Array Practice</title>
</head>
<body>
    <script scr="playlist.js"></script>
</body>
</html>

This code gives the same error you're recieving.