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 trialArnold Nunez
Front End Web Development Techdegree Student 9,494 PointsHow do I approach this if loop? I have included the index and law variables as conditions and it comes up wrong.
I have included the index and law variables as conditions and it comes up wrong. I have tried variations of "if (law == index)" and 'if (e.target == index)" and more.
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 (law === 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
Samuel Zhen
33,571 PointsThen you can replace it with
if (i === index) {
law.style.fontWeight = 'bold';
} else {
law.style.fontWeight = 'normal';
}
Samuel Zhen
33,571 PointsI assume you want to give your chosen law a bold font weight after you click the button? Try this
const laws = document.getElementsByTagName('li');
const indexText = document.getElementById('boldIndex');
const button = document.getElementById('embolden');
button.addEventListener('click', () => {
const index = parseInt(indexText.value);
// Reset all text back to normal weight
for(let i = 0; i < laws.length; i++) {
laws[i].style.fontWeight = 'normal';
}
// Bold chosen text
laws[index - 1].style.fontWeight = 'bold';
});
When you do law === index, you're comparing a li element with input text value, that's why your code didn't work.
Arnold Nunez
Front End Web Development Techdegree Student 9,494 PointsThis seems to make sense but the challenge of this task is to replace just the false condition to make the if loop work. The challenge is that the list item index that matches the input field, should have its font weight changed. Thank you though.