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 trialSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsHelp with code challenge.
Hi guys I'm stuck on this fix DOM manipulation code
. can't figure it out.
Steven Parker
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 (laws.indexOf(index)) {
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>
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsThe answer is quite simple if you know what values you're checking.
In the code you're creating two variables you want to check your condition against.
You have i
which is created in your for
loop and you have index
which you use to store the value that you capture in parseInt.
What you're checking for is that these values are the same, so use the equality operator in your test condition.
Good luck! :-)
Steven Parker
231,248 PointsI was alerted by your tag, but it looks like Jonathan already has you covered. Happy coding!
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsYep he did. Thanks Steven Parker
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsGot the answer. I swear at some point I did that, I'm sure there was a typo and it didn't parse cause I was really annoyed so I was typing fast couldn't figure out why it wasn't working. hahahaha. 3 days, man 3 days. hahahaha.
Thank you
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsNever mind, I'm glad you got there in the end. All a learning experience! :)