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 trialPolo Moreno
11,782 PointsI am confuse in this challengue
The challengue say the next
There are three list items in the index.html file. We want to be able to enter a 0, 1 or 2 in the text field to embolden the list item with the corresponding index. Can you figure out what condition to put into the if statement to make this code work?
who is the correct form for write the correct conditional ?
const laws = document.getElementsByTagName('li');
const indexText = document.getElementById('boldIndex');
const button = document.getElementById('embolden');
button.addEventListener('click', (e) => {
const index = parseInt(indexText.value, 10);
for (let i = 0; i < laws.length; i += 1) {
let law = laws[i];
// replace 'false' with a correct test condition on the line below
if (false) {
law.style.fontWeight = 'bold';
} else {
law.style.fontWeight = 'normal';
}
}
});
<!DOCTYPE html>
<html>
<head>
<title>Newton's Laws</title>
</head>
<body>
<h1>Newton's Laws of Motion</h1>
<ul>
<li>An object in motion tends to stay in motion, unless acted on by an outside force.</li>
<li>Acceleration is dependent on the forces acting upon an object and the mass of the object.</li>
<li>For every action, there is an equal and opposite reaction.</li>
</ul>
<input type="text" id="boldIndex">
<button id="embolden">Embolden</button>
<script src="app.js"></script>
</body>
</html>
Steven Parker
231,248 PointsDid you get it now?
5 Answers
Steven Parker
231,248 PointsDon't over-think this one, it's quite simple.
All you need to do here is compare the current loop index with the value that the user has placed in the input box. And to make it easy, the challenge has already created a variable named index and assigned it with the numeric value produced by converting the input field.
You just need to test if the loop index (i) is equal to the user's "index".
Polo Moreno
11,782 PointsTanks for your help, i found the solution.
Raman Pauliuts
11,104 Pointshi all! I still have not found a solution..help
Raman Pauliuts
11,104 Pointsstill doesn't understand the decision answer
Steven Parker
231,248 PointsSo when I said: You just need to test if the loop index (i) is equal to the user's "index".
The code for that would be:
if (i == index) {
Raman Pauliuts
11,104 Pointsstrange...but where did (i) ? it hurts my head
Steven Parker
231,248 PointsThe variable "i" is the loop index. It is right there, being compared to "index" in the statement shown.
Raman Pauliuts
11,104 PointsI just realized (i) is a loop law ...but I didn't know that you can only put i on ...okay thanks anyway
Justin Hicks
14,290 PointsI think I did exactly what you said not to do in the above. I over thought it. After looking at it again you are absolutely correct. const laws = document.getElementsByTagName('li'); sets li to laws. Then later let law = laws[i]; while laws is still the li the [i] tracks the number. Thanks for the clarification Steven!!
Justin Hicks
14,290 PointsI put if(index == law) but it doesn't work? Index is the value of the input and law = laws[i] so should that not be the be the same as just i?
Steven Parker
231,248 PointsNo, "law" is a list item (an HTML element), but "i" is just a number (int) counting the loops.
Jasmine Martin
14,307 PointsThe question just may need to be reworded :/ Thankful for the forum
Fredy-Edwin Esse
Front End Web Development Techdegree Student 14,550 PointsFredy-Edwin Esse
Front End Web Development Techdegree Student 14,550 PointsI had the same problem. I tried all sorts of different solutions so if anyone has an idea what the right condition is, please help :)