Tell us what’s happening:
Mt main-section id’s match the first child text but requests at point 8. is still not satisfied. Please help me check?
Section ID: What_is_JavaScript
Header Text: What is JavaScript?
Section ID: A_Hello_World_example
Header Text: A “Hello World!” example
Section ID: Language_basic_crash_course
Header Text: Language basic crash course
Section ID: Supercharging_our_example_website
Header Text: Supercharging our example website
Section ID: Conclusion
Header Text: Conclusion
Your code so far
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="/styles.css" href="styles.css">
</head>
<body>
<nav id="navbar">
<header>
In this article
</header>
<ul>
<li><a href="#What_is_JavaScript" class="nav-link">What is JavaScript?
</a></li>
<li><a href="#A_Hello_World_example" class="nav-link">A "Hello World!"
example</a></li>
<li><a href="#Language_basic_crash_course" class="nav-link">Language
basic crash course
</a></li>
<li><a href="#Supercharging_our_example_website" class="nav-link">
Supercharging our example website
</a></li>
<li><a href="#Conclusion" class="nav-link">Conclusion</a></li>
</ul>
</nav>
<main id="main-doc">
<section class="main-section" id="What_is_JavaScript">
<header>"What is JavaScript?"</header>
<p>JavaScript is a powerful programming language that can add
interactivity to a website. It was invented by Brendan Eich.
</p>
<p>JavaScript is versatile and beginner-friendly. With more experience,
you'll be able to create games, animated 2D and 3D graphics,
comprehensive database-driven apps, and much more!
</p>
<p>
JavaScript itself is relatively compact, yet very flexible. Developers
have written a variety of tools on top of the core JavaScript language,
unlocking a vast amount of functionality with minimum effort. These
include:
</p>
<ul>
<li>
Browser Application Programming Interfaces (APIs) built into web
browsers, providing functionality such as dynamically creating HTML
and setting CSS styles, collecting and manipulating a video stream
from a user's webcam, or generating 3D graphics and audio samples.
</li>
<li>
Third-party APIs that allow developers to incorporate functionality
in sites from other content providers, such as YouTube or Facebook.
</li>
<li>
Third-party frameworks and libraries that you can apply to HTML to
accelerate the work of building sites and applications.
</li>
</ul>
<p>It's outside the scope of this article—as a light introduction to
JavaScript—to present the details of how the core JavaScript language
is different from the tools listed above. You can learn more in our
Core modules, as well as in other parts of MDN.
</p>
<p>The section below introduces some aspects of the core language and
offers an opportunity to play with a few browser API features too. Have
fun!
</p>
</section>
<section class="main-section" id="A_Hello_World_example">
<header>"A "Hello World!" example"</header>
<p>JavaScript is one of the most popular modern web technologies! As your
JavaScript skills grow, your websites will enter a new dimension of
power and creativity.
</p>
<p>
However, getting comfortable with JavaScript is more challenging than
getting comfortable with HTML and CSS. You should start small, and
progress gradually. To begin, let's examine how to add JavaScript to
your page for creating a Hello world! example.
</p>
<ul>
<li>Inside your first-website folder, create a new folder named
scripts.
</li>
<li>
Within the scripts folder, create a new text document called main.js,
and save it.
</li>
<li>
Go to your index.html file and enter this code on a new line, just
before the closing </body> tag:
<div>
<code><script src="scripts/main.js"></script></code>
</div>
This is doing the same job as the <link> element for CSS. It
applies the JavaScript to the page, so it can have an effect on the
HTML (along with the CSS, and anything else on the page).
</li>
<li>Add this code to your scripts/main.js file:
<div>
<code>
const myHeading = document.querySelector("h1");
myHeading.textContent = "Hello world!";
</code>
</div>
</li>
<li>Make sure the HTML and JavaScript files are saved, then load index.
html in your browser.
</li>
</ul>
<h3>What happened?</h3>
<p>
We have used JavaScript to change the heading text to Hello world!. We
did this by using a function called querySelector() to grab a reference
to your heading, and then store it in a variable called myHeading. This
is similar to what we did using CSS selectors. When you want to do
something to an element, you need to select it first.
</p>
<p>
Following that, the code set the value of the myHeading variable's
textContent property (which represents the content of the heading) to
Hello world!.
</p>
</section>
<section class="main-section" id="Language_basic_crash_course">
<header>"Language basic crash course"</header>
<p>
To give you a better understanding of how JavaScript works, let's
explain some of the core features of the language. It's worth noting
that these features are common to all programming languages. If you
master these fundamentals, you have a head start on coding in other
languages too!
</p>
<p>Variables are containers that store values. You start by declaring a
variable with the let keyword, followed by the name you give to the
variable:
</p>
<div>
<code>let myVariable;</code>
</div>
<p>
A semicolon at the end of a line indicates where a statement ends. It
is only required when you need to separate statements on a single line.
However, some people believe it's good practice to have semicolons at
the end of each statement. There are other rules for when you should
and shouldn't use semicolons. For more details, see Your Guide to
Semicolons in JavaScript.
</p>
<p>
You can name a variable nearly anything, but there are some
restrictions. (See this section about naming rules.) If you are unsure,
you can check your variable name to see if it's valid.
</p>
<p>
JavaScript is case sensitive. This means myVariable is not the same as
myvariable. If you have problems in your code, check the case!
</p>
<p>
After declaring a variable, you can give it a value:
</p>
<div>
<code>myVariable = "Bob";</code>
</div>
<p>
Also, you can do both these operations on the same line:
</p>
<div>
<code>let myVariable = "Bob";</code>
</div>
<p>
You retrieve the value by calling the variable name:
</p>
<div>
<code>myVariable;</code>
</div>
<p>
After assigning a value to a variable, you can change it later in the
code:
</p>
<div>
<code>
let myVariable = "Bob";<br>
myVariable = "Steve";
</code>
</div>
<h3>Conditionals</h3>
<p>
Conditionals are code structures used to test if an expression returns
true or not. A very common form of conditionals is the if...else
statement. For example:
</p>
<div>
<code>
let iceCream = "chocolate";
if (iceCream === "chocolate") {
alert("Yay, I love chocolate ice cream!");
} else {
alert("Awwww, but chocolate is my favorite…");
}
</code>
</div>
<p>
The expression inside the if () is the test. This uses the strict
equality operator (as described above) to compare the variable
iceCream with the string chocolate to see if the two are equal. If
this comparison returns true, the first block of code runs. If the
comparison is not true, the second block of code—after the else
keyword—runs instead.
</p>
<h3>Functions</h3>
<p>
Functions are a way of packaging functionality that you wish to
reuse. It's possible to define a body of code as a function that
executes when you call the function name in your code. This is a good
alternative to repeatedly writing the same code. You have already
seen some uses of functions. For example:
</p>
<div>
<code>let myVariable = document.querySelector("h1");</code>
<code>alert("hello!");</code>
</div>
<p>The document.querySelector() and alert() functions are built intothe
browser.
</p>
<p>
If you see something which looks like a variable name, but it's
followed by parentheses — () — it is likely to be a function.
Functions often take arguments: bits of data they need to do their
job. Arguments go inside the parentheses, separated by commas if
there is more than one argument.
</p>
<p>
For example, the alert() function makes a pop-up box appear inside
the browser window, but we need to give it a string as an argument to
tell the function what message to display.
</p>
<p>
You can also define your own functions. In the next example, we
create a simple function which takes two numbers as arguments and
multiplies them:
</p>
<div>
<code>
function multiply(num1, num2) {
let result = num1 * num2;
return result;
}
</code>
</div>
<p>Try running this in the console; then test with several arguments.
For example:
</p>
<div>
<code>
multiply(4, 7);<br>
multiply(20, 20);<br>
multiply(0.5, 3);
</code>
</div>
<h3>Events</h3>
<p>
Real interactivity on a website requires event handlers. These are
code structures that listen for activity in the browser, and run code
in response. The most obvious example is handling the click event,
which is fired by the browser when you click on something with your
mouse. To demonstrate this, enter the following into your console,
then click on the current webpage:
</p>
<div>
<code>
document.querySelector("html").addEventListener("click", function () {
alert("Ouch! Stop poking me!");
});
</code>
</div>
<p>
There are a number of ways to attach an event handler to an element. Here we select
the <html> element. We then call its addEventListener() function, passing in the
name of the event to listen for ('click') and a function to run when the event
happens.
</p>
<p>
The function we just passed to addEventListener() here is called an anonymous
function, because it doesn't have a name. There's an alternative way of writing
anonymous functions, which we call an arrow function. An arrow function uses ()
=> instead of function ():
</p>
<div>
<code>
document.querySelector("html").addEventListener("click", () => {
alert("Ouch! Stop poking me!");
});
</code>
</div>
</section>
<section class="main-section" id="Supercharging_our_example_website">
<header>"Supercharging our example website"</header>
<h3>Adding an image changer</h3>
<p>
In this section, you will learn how to use JavaScript and DOM API features to
alternate the display between two images. This change will happen as a user clicks
the displayed image.
</p>
<ul>
<li>Choose another image to feature on your example site. Ideally, the image will be
the same size as the image you added previously, or as close as possible.</li>
<li>Save this image in your images folder.</li>
<li>Add the following JavaScript code to your main.js file, making sure to replace
firefox2.png and both instances of firefox-icon.png with your second and first
image names, respectively.
<div>
<code>
const myImage = document.querySelector("img");
myImage.addEventListener("click", () => {
const mySrc = myImage.getAttribute("src");
if (mySrc === "images/firefox-icon.png") {
myImage.setAttribute("src", "images/firefox2.png");
} else {
myImage.setAttribute("src", "images/firefox-icon.png");
}
});
</code>
</div>
</li>
<li>Save all files and load index.html in the browser. Now when you click the image,
it should change to the other one.</li>
</ul>
<p>
In the above code, you stored a reference to your <img> element in myImage. Next, you
gave it a click event handler function with no name (an "anonymous" function). Every
time this element is clicked, the function:
</p>
<ul>
<li>Retrieves the value of the image's src attribute.</li>
<li>Uses a conditional to check if the src value is equal to the path of the original
image.</li>
</ul>
<h3>Adding a personalized welcome message</h3>
<p>Next, let's change the page heading to a personalized welcome message when the user
first visits the site. This welcome message will persist. Should the user leave the
site and return later, we will save the message using the Web Storage API. We will
also include an option to change the username, and therefore, the welcome message.
</p>
<ul>
<li>In index.html, add the following line just before the <script> element:</li>
<div>
<code><button>Change user</button></code>
</div>
<li>
In main.js, place the following code at the bottom of the file, exactly as it is
written. This creates references to the new button and the heading, storing each
inside variables:
</li>
<div>
<code>
let myButton = document.querySelector("button");
let myHeading = document.querySelector("h1");
</code>
</div>
<li>Add the following function to set the personalized greeting. This won't do
anything yet; we will call the function later on.
<div>
<code>function setUserName() {
const myName = prompt("Please enter your name.");
localStorage.setItem("name", myName);
myHeading.textContent = `Mozilla is cool, ${myName}`;
}
</code>
</div>
The setUserName() function contains a prompt() function, which displays a dialog
box, similar to alert(). This prompt() function does more than alert(), asking the
user to enter data, and storing it in a variable after the user clicks OK. In this
case, we are asking the user to enter a name. Next, the code calls on the
localStorage API, which allows us to store data in the browser and retrieve it
later. We use localStorage's setItem() function to create and store a data item
called "name", setting its value to the myName variable which contains the user's
entry for the name. Finally, we set the textContent of the heading to a string,
plus the user's newly stored name.
</li>
<li>
Add the following condition block after the function declaration. We could call this
initialization code, as it structures the app when it first loads.
<div>
<code>if (!localStorage.getItem("name")) { setUserName();
} else { const storedName = localStorage.getItem("name");
myHeading.textContent = `Mozilla is cool, ${storedName}`;
}
</code>
</div>
This first line of this block uses the negation operator (logical NOT, represented by
the !) to check whether the name data item is already stored in in localStorage. If
not, the setUserName() function runs to create it. If it exists (that is, the user
set a user name during a previous visit), we retrieve the stored name using getItem()
and set the textContent of the heading to a string, plus the user's name, as we did
inside setUserName().
</li>
<li>
Add a click event handler function to the button, as shown below. When clicked,
setUserName() runs. This allows the user to enter a different name by pressing the
button.
</li>
<div>
<code>myButton.addEventListener("click", () => {
setUserName();
});
</code>
</div>
</ul>
</section>
<section class="main-section" id="Conclusion">
<header>"Conclusion"</header>
<p>
Now that you've finished creating your website, the next step is to get it online so
that others can check it out. We'll show you how to do so in our next article —
Publishing your website.
</p>
<p>
We have just scratched the surface of JavaScript in this article. You'll find a lot
more JavaScript later on in our learning pathway, starting with our Dynamic scripting
with JavaScript module.
</p>
</section>
</main>
</body>
</html>
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Challenge Information:
Technical Documentation Page - Build a Technical Documentation Page
https://www.freecodecamp.org/learn/2022/responsive-web-design/build-a-technical-documentation-page-project/build-a-technical-documentation-pagePreformatted text