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 trialJohn Cannon
Full Stack JavaScript Techdegree Student 3,161 Pointsfix-dom-manipulation-code
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. i cant figure it out
const laws = document.getElementsByTagName('li');
const indexText = document.getElementById('boldIndex');
const button = document.getElementById('embolden');
const input = document.querySelector("input");
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 () {
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>
4 Answers
Steven Parker
231,248 PointsThe challenge code already has conveniently converted the user's input into a number, and put it into the variable "index". So now all you need to do is compare that to the loop variable to see if they match.
John Cannon
Full Stack JavaScript Techdegree Student 3,161 PointsOmg it's (i === index) 🤦 it took me days.
Thanks
John Cannon
Full Stack JavaScript Techdegree Student 3,161 PointsI tried everything if (law.value === index) if (parseInt(law.value) === index) if (parseInt(law) === index)
Still no results 😭😢😫
Also you said law isn't the loop variable but it's inside the loop I don't get it
Steven Parker
231,248 PointsThe "loop variable" is the one that is created inside the "for" initialization clause. It stores the iteration number and is incremented each time the loop repeats. It's not a property of "law".
Robert Young
11,579 PointsJohn Cannon This threw me off so damn hard - I was thinking 'it can't be i === index
as it seemed too obvious... arrgg thanks Steven Parker
John Cannon
Full Stack JavaScript Techdegree Student 3,161 PointsJohn Cannon
Full Stack JavaScript Techdegree Student 3,161 PointsYes i tried it but it didnt work
Steven Parker
231,248 PointsSteven Parker
231,248 PointsBut "law" isn't the loop variable, and it's also not a number. It's an element reference that will be used to making the style change.
What you want to do is to compare "index" (a number) with the loop variable, which is also a number.