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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

What wrong with my code here?

MY INDEX CODE

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Musical Playlist</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My Music Playlist</h1>
<script src="js/arrayAfor.js"></script>
</body>
</html>

MY JAVASCRIPT CODE

var playList = [
  'I Did It My Way',
  'Respect',
  'Imagine',
  'Born to Run',
  'Louie Louie',
  'Maybellene'
];

function print(message) {
  document.write(message);
}

function printList(l){
    var HTML='<ol>';
    for(var i=0;i<l.length;i=i+1){
        HTML+='<li>'+l[i]+'</li>';
    }
    HTML+='</ol>';
    print(HTML);
}
printList(playList);
Samuel Webb
Samuel Webb
25,370 Points

I edited your code to make it more readable. If you're curious how I did it, go to edit it. You can use Markdown in the forum so if you're curious how to do it, click the Markdown Cheatsheet and you'll see a small popup that'll show you a few code snippets.

2 Answers

Samuel Webb
Samuel Webb
25,370 Points

The code works exactly like its supposed to. I'm assuming your problem is that everything on the web page is being replaced right? That's what document.write() does. If you want to add it to the page without replacing everything, you'll have to use some mildly more advanced code to place the HTML produced by your JS into a specific spot on the page.

Samuel Webb
Samuel Webb
25,370 Points

If your JS doesn't work at all, you may want to make sure your script source has the correct path.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

So did you see any problem now?Everything match with teacher,it should work,but it doesn't

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Oh now it work already thank you.and How to make the markdown sheet?

Samuel Webb
Samuel Webb
25,370 Points

Under the 'Add an Answer' box there's a little line of text that says, "Reference this Markdown Cheatsheet for syntax examples for formatting your post." If you click the part that says Markdown Cheatsheet, it'll show you how to do some of the markdown stuff.