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 One Solution

Nicolás Melgarejo
Nicolás Melgarejo
14,154 Points

My solution

// 1: Select the element with the ID 'about'. 
//    Store the element in the variable `about`.
const about = document.querySelector('#about');
about.style.border = "2px solid firebrick";

// 2: Select all the <h2> elements in the document.
//    Set the color of the <h2> elements to a different color.

const h2s = document.querySelectorAll('h2');

for(h2 of h2s) {
  h2.style.color = "orchid";
}


// 3: Select all elements with the class '.card'. 
//    Set their background color to the color of your choice.

const cards = document.querySelectorAll('.card');

for(card of cards){
  card.style.backgroundColor = "cyan";
}

// 4: Select only the first <ul> in the document.
//    Assign it to a variable named `ul`.

const ul = document.querySelector('ul');
ul.style.border = "2px solid indigo";

// 5: Select only the second element with the class '.container'.
//    Assign it to a variable named `container`.

const container = document.getElementsByClassName('container')[1];

container.style.backgroundColor = "royalblue";

// 6: Select all <a> elements that have a 'title' attribute. 
//    Set their color value to the color of your choice.

const anchorsWithTitle = document.querySelectorAll('a[title]');

for(a of anchorsWithTitle){
  a.style.color = 'orchid';
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

Best practice would be to explicitly declare loop variables (like: for (let h2 of h2s) {)..
It's also syntactically necessary when strict mode is in use.

Just for fun, a way to do this without an explicit loop is with the forEach method:

h2s.forEach(h2 => h2.style.setProperty("color", "orchid"));