Can I have a Code Review of some Javascript?

Can I have a Code Review of some Javascript?
0

#1

Hi

I was working on some javascript yesterday and was wondering if I could get some help with it?

I have created a codepen with my code at http://codepen.io/funkysi/pen/KNayQo

The purpose of it is to cycle through a series of news articles after a specified time period and there is also a next button which allows you to manually click through them.

The code works fine but the last news article is skipped and the time period seems to vary once you start clicking next.

Thanks for any help anyone can share


#2

I’ve take a look at your code and I think you aren’t seeing the last item in your list because in the next() function you have an if conditional which is hiding it.

Some other oddities I see:

  • you are using the id of newsrow1 multiple times in your html markup, when typically you should only use it once.
  • rather than set the css using jquery, you should create class to add/remove with jquery instead - examples: $(’.selected’).addClass(‘hide’); hide is class that is set to display: none;
  • i would also recommend moving away from jQuery or if you want to use it still, learn how to do everything you need to do with vanilla javascript. Look up querySelector on mdn.com and you’ll realize that vanilla js is very capable and not that daunting.

#3

Great advice, agree with everything you’ve said. hopefully it’s still helpful to this dude haha.