JavaScript DOM Scripting By Example Editing and Filtering Names Fix DOM Manipulation Code

DOM scripting question

I understand the concept that the input of index needs to match the index of law in the conditional statement.

I just can’t figure out what the correct code is, can someone please help me

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) {

  = 'bold';
       } else {
  = 'normal';
<!DOCTYPE html>
  <title>Newton's Laws</title>
  <h1>Newton's Laws of Motion</h1>
    <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>
  <input type="text" id="boldIndex">
  <button id="embolden">Embolden</button>
  <script src="app.js"></script>

1 Answer

Steven Parker
Steven Parker
200,540 Points

You're close, but "law" is the HTML element you want to set the style on, not the index number.

So what you want to compare "index" to is the index of "law", which is the loop variable ("i").