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 Simplify Repetitive Tasks with Loops For Loops

Samantha North
Samantha North
9,450 Points

Divs not showing up

Hi there everyone. My divs don't show up on the page, even though I think my code matches the video. Can anyone help? Here's my code:

var html = ' ';

for (var i = 1; i <= 10; i += 1) { html += '<div>' + i + '</div>';

} document.write(html);

11 Answers

Samantha, I've just been sitting here beating myself up, having the exact issue you had. Finally, I downloaded the Workspace file and ran the code outside of Workspace, it worked just as it should. I don't know whether it's a glitch/setting in our local machines, or one with Treehouse/Workspaces, but what I have taken away from the last 45 minutes of time wasted... if after going over your "non-working" code once and all seems ok, pull and run the code elsewhere outside of here to know for certain it's truly broken code, or just whatever this glitch is that sometimes keeps good code from running in Workspaces. For the record... I did save my work and restart Workspaces (and ALL browser windows, as well as a complete machine reboot) several times, didn't make a difference, code still wouldn't run properly until I got it away from Workspaces.

Mike LaPan
Mike LaPan
5,190 Points

Yup, same thing here.

Valentin Fezza
Valentin Fezza
18,166 Points

Samantha,

I tried running your first and second pieces of code and both fired A-OK on my browser. I was having a similar problem until I tried writing the <script> tag again. Not sure why, after writing down the exact same thing, the code ran without a hitch.

I know you're 100% sure script.js is inside the js folder. However, I'd really like for you to check one last time. All new files are created in the root folder, so you'd have to drag script.js into the js folder for it to actually be there. This is often overlooked, especially because the workspaces UI can make you think new files are inside one of the top folders when they're really not.

If it looks like both index.html and script.js are at the same level of indentation in the file explorer, give this a go. Then relaunch workspaces. All the best!

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

Nope, I can't find any bug. I copy and pasted your code into mine and it worked so the only thing I can think of is that your script.js file might be in the wrong place.

Samantha North
Samantha North
9,450 Points

I've checked again and the file is definitely inside the js folder. I'll try reloading the workspace; as perhaps it's a glitch with that. Thanks for helping me out.

I'm having the same trouble. My code looks identical to yours and Dave's, yet it still won't run. I'm stuck!

Darrell Conklin
Darrell Conklin
21,786 Points

I was having the same issue until I took the code outside of workspaces and pasted into a code editor (currently using Brackets) and found a lot of errors. Revised the code to:

var html = '';
var i;

for (i = 1; i <= 10; i += 1) {
  html += '<div>' + i + '</div>';
}
document.write(html);

pasted it back into WorkSpaces and everything worked fine.

Mike LaPan
Mike LaPan
5,190 Points

I took the code outside of Workspaces, and it works fine. Inside of Workspaces it does not. This shouldn't be the case, but it is. This should be flagged for Treehouse to look into.

Very odd behavior... and super frustrating if you are trying to learn. :-)

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

Remember to put your script tag inside of your body tag in the index.html file.

<script src="js/script.js"></script>

This is my code for the "script.js" file:

var html = '';

for ( var i = 1; i <= 10; i += 1) {
    html += '<div>' + i + '</div>';
}

document.write(html);

I hope that helps a little bit.

Samantha North
Samantha North
9,450 Points

Hey Carlos, thanks for your reply. I've checked and re-checked the script tags and everything looks exactly the same as yours and Dave's. The js file is in the correct place inside the js folder. But there's still nothing showing up in the browser... Am I missing anything else that you can think of?

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

Could you post (copy and paste) your code so I can see if I find some error?

https://teamtreehouse.com/community/posting-code-to-the-forum

In that link it explains you how to do it properly.

Samantha North
Samantha North
9,450 Points

OK, here's my html first:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Circles</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body id="color">
<script src="js/script.js"></script>
</body>
</html>

and my JavaScript:

var html = ' ';

for (var i = 1; i <= 10; i += 1) {
  html += '<div>' + i + '</div>';

}
document.write(html);
Darrell Conklin
Darrell Conklin
21,786 Points

Using Mozilla Firefox Developer Edition eliminated the issues I was having with code not working in workspaces

Derek Wagner
PLUS
Derek Wagner
Courses Plus Student 1,413 Points

After you add the script tag to the index.html make sure that you save that file. After saving index.html and script.js it worked for me.