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!
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

Derrick Mull
18,518 PointsFeedback: Javascript hurts my brain
So in an attempt to make myself better at javascript I started coding a small concept page that relies a lot more on javascript/jquery. Just wanting some review on the javascript to see if there is a way to make it DRYer.
2 Answers

James Barnett
39,199 PointsUse an array instead of a series of if statements for element changing and use innerHTML instead of prepend to not have to replace html.
Also your CSS could use some DRY-ing out as well, weighing in at 300 lines for one small page.

James Barnett
39,199 PointsEarlier I was thinking of using the indexOf property with 2 arrays. Then I remembered there are associative arrays in JavaScript.
var element = { 'au':'Gold','ag':'Silver','pt':'Platium','pd':'Palladium' };
document.getElementById("metal-head").innerHTML = element[id];
Note that I've changed the HTML markup for metal-head
from a class to an id. Also note that I'm using the innerHTML
property instead of empty
and html
properties.
Derrick Mull
18,518 PointsDerrick Mull
18,518 PointsThanks.
Some of the CSS is for parts of the page not posted.
James Barnett
39,199 PointsJames Barnett
39,199 PointsIn that case, I'd read up on reduced test cases
Derrick Mull
18,518 PointsDerrick Mull
18,518 PointsTrying to wrap my brain around using an array as you described, but coming up short after lookin' around the webs... Know of any examples I could look at that are close to this?