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 trialColin Bondi
8,005 PointsStumped on the If condition of this code challenge.
For the if statement I am testing if the value entered stored in the index variable is equal to the current li item stored in the law variable. This is not correct so I can't see what I should be testing here?
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 ( index === law ) {
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
Peter Vann
36,427 PointsHi Colin!
You are on the right track, for sure...
In your if statement, you want to be comparing apples to apples, but unfortunately, you are currently sort of attempting to compare apples to oranges.
index is an int value (apple)
i is an int value (apple)
laws[i] assigned to law is a list item element (orange)
So instead of this:
if ( index === law ) {
You want this:
if ( index === i ) {
Which passes.
Another way to think of it is, for each iteration of the loop, if that interation's index (i) matches the input variable (index), then embolden that list item.
I hope that helps.
Stay safe and happy coding!
Colin Bondi
8,005 PointsHey Peter Thanks for your help with this and that certainly makes it clearer. I think I need a lot more practice with this because its still a bit confusing...
Peter Vann
36,427 PointsHey Colin!
Just stick with it. It gets easier over time.
And tap into all the 3rd party resources you can, such as:
https://developer.mozilla.org/en-US/
https://www.youtube.com/results?search_query=derek+banas
And, of course, there's always Google and Stack Overflow!?!
I hope that helps.
Stay safe and happy coding!
Colin Bondi
8,005 PointsHelpful suggestions Thanks Peter!