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 DOM Scripting By Example Improving the Application Code Fix Function that Should be Creating Links

Dwayne Munro
seal-mask
.a{fill-rule:evenodd;}techdegree
Dwayne Munro
Front End Web Development Techdegree Student 13,108 Points

This code should create a sequence of div elements that each hold a headline above and a link below. Currently only the

can some one help me on this code?

app.js
const languages = ['Python', 'JavaScript', 'PHP', 'Ruby', 'Java', 'C'];
const section = document.getElementsByTagName('section')[0];

// Accepts a language name and returns a wikipedia link
function linkify(language) {
  const a = document.createElement('a');
  const url = 'https://wikipedia.org/wiki/' + language + '_(programming_language)';
  a.href = url;
  return a;
}

// Creates and returns a div
function createDiv(language) {
  const div = document.createElement('div');
  const h2 = document.createElement('h2');
  const p = document.createElement('p');
  const link = linkify(language);

  h2.textContent = language;
  p.textContent = 'Information about ' + language;
  link.appendChild(p);

  div.appendChild(h2);

  // Your code below


  // end

  return div;
}

for (let i = 0; i < languages.length; i += 1) {
  let div = createDiv(languages[i]);

  section.appendChild(div);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <title>Programming Languages</title>
  <style>
    body {
      font-family: sans-serif;
    }
  </style>
</head>
<body>
  <h1>Programming Languages</h1>
  <section></section>
  <script src="app.js"></script>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
231,269 Points

This is easier than it may seem at first.

Remember, the challenge says the code already displays the header (h2) but not the link. So if you look at the code and find where the header gets added to the div, you can do something similar to add the link.

Aurelian Stanica
Aurelian Stanica
7,823 Points

Can you be more specific please, I do not get this one...

Steven Parker
Steven Parker
231,269 Points

The line I'm talking about as an example is the one just above the comment "// Your code below".

div.appendChild(link);

Thanks for sharing this information. I was using

div.appendChild(p)

with the thought of p being the parent of link because it was appended in line 21. In my editor I used the div.appendChild(p) and div.appendChild(link); and saw that after removing the div.appendChild(p) the link worked! Thanks for the hint Steven Parker!