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

Gary Sorrell
Gary Sorrell
6,543 Points

Less than 20 lines of code and 2 pots of coffee. Fun though I must say.

Below is my first JavaScript. It took all day, but I finally figured it out, its an image scroller, although it doesnt scroll yet, just replaces images every 3secs. What I finally did have to 'go to google' for was the order of things. I had stuff all over the place inside my function. But other than that, satisfying days work.

 var i = 0;
 var pics = ["img/a.jpg","img/b.jpg","img/c.jpg"];

function imageScroll()
{
   document.getElementById("image").src = pics[i];
   if(i < pics.length - 1) 
    {
        i++;
    } else {
        i = 0;
    }
   setTimeout("imageScroll()",3000);
}
window.onload=imageScroll;

Sometimes you'll be surprised how small little bits of code make you feel after you've finally figured them out.

Just a quick tip if you use three backtick characters before and after your code block it'll preserve the formatting here on the forums. You can also specify the language by putting the language name after the opening set of backticks (ex: ```javascript).

1 Answer