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 trialSeth Pabst
10,285 Pointscant get an answer
ive reposted this twice, please tell me how to do it, ive tried a multitude of possibilities but cant get it right. Please answer. Ive been stuck on this one for days
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 === laws[i]) {
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
Steven Parker
231,269 PointsI just posted additional info to your previous question. You don't need to re-ask the same question, people can and will still post to it until you select a "best answer" (and some will post answers even after then).
Don't over-think this, it's just a simple comparison expression. The most important suggestion I've given you is that you only need to compare two number values in this expression. Both are variables provided already in the code.
Seth Pabst
10,285 Pointsso how do you access the numeric value. The only way I knew of was what i used (laws[i]) which now i know returns an element not a number. the only thing else i can think of is the .value property but that wont return a number. can you just tell me so i can get past this. Thanks
Seth Pabst
10,285 Pointsnevermind dude i got it!!! thank you!!!
Steven Parker
231,269 PointsFacepalm moment? Congrats, and happy coding.
Seth Pabst
10,285 PointsSeth Pabst
10,285 Pointssorry this is frustrating me, anyway to respond to the last thing you said, i thought the law variable held the current list item and laws[i] held the index value or number. either way I've compared both variables to index. not sure what else there is to do?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThey both have the same value, one was assigned from the other:
let law = laws[i];
Either one represents the list item element, but you only want the item's number.