JavaScript DOM Scripting By Example Improving the Application Code Fix Function that Should be Creating Links

Lisset Torres
Lisset Torres
770 Points

appending Child <link> without appending Child <p>

I could figure out the result appending the link to the created div, but I don't understand why I can't do that without appending the p element to the link. What if I just want to append the raw link without the "Information about..."

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

  div.appendChild(link);

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

1 Answer

Steven Parker
Steven Parker
177,888 Points

You could omit these 3 lines from the code if you did not want the paragraph:

  const p = document.createElement('p');

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

But be aware that this is just to answer your question, it would not be part of completing the challenge. Also, a link with no content would exist in the DOM, but not be visible (or usable) on the page.