# Finding errors in code with DOM in JavaScript

In the following tasks, some programmer has written code and may have made mistakes in it. You should check if the code does what is described. If the code does not work correctly, you need fix the errors.

The code should add text to the end of each paragraph:

`<p>1</p> <p>2</p> <p>3</p>` `let elems = document.querySelector('p'); elems.textContent += '!';`

When you click on a paragraph, its value should increase by one:

`<p>1</p> <p>2</p> <p>3</p>` `let elems = document.querySelectorAll('p'); for (let elem of elems) { elem.addEventListener('click', function() { this.textContent += Number(1); }); }`

By clicking on the button, the text of the paragraph should become bold:

`<p>text</p> <button>click</button>` `let button = document.querySelector('button'); let elem = document.querySelector('p'); button.addEventListener('click', function() { elem.innerHTML = '<b>elem.innerHTML</b>'; });`

By clicking on the button, the sum of the numbers from the paragraphs should be displayed:

`<p>1</p> <p>2</p> <p>3</p> <button>click</button>` `let button = document.querySelector('button'); let elems = document.querySelector('p'); button.addEventListener('click', function() { let sum = 0; for (let elem of elems) { sum += Number(elems.textContent); } console.log(sum); });`

By clicking on a paragraph, the specified text should be added to the end of it:

`<p>1</p> <p>2</p> <p>3</p>` `let elems = document.querySelectorAll('p'); for (let elem of elems) { elem.addEventListener('click', () => { this.textContent += '!'; }); }`

By clicking on the button, the text of each paragraph should become bold:

`<p>text1</p> <p>text2</p> <p>text3</p> <button>click</button>` `let button = document.querySelector('button'); let elems = document.querySelectorAll('p'); button.addEventListener('click', function() { for (let elem of elems) { elem.innerHTML = '<b>+elem.innerHTML+</b>'; } });`

By clicking on the button, the sum of the numbers from the paragraphs should be displayed in the console:

`<p>1</p> <p>2</p> <p>3</p> <button>click</button>` `let button = document.querySelector('button'); let elems = document.querySelectorAll('p'); let sum = 0; for (let elem of elems) { sum = elem.textContent + 1; button.addEventListener('click', function() { console.log(sum); }); }`

Numbers are entered into the inputs. By clicking on the button, the sum of the numbers from the inputs should be displayed in the console:

`<input> <input> <input> <button>click</button>` `let button = document.querySelector('button'); let elems = document.querySelectorAll('input'); let sum = 0; for (let elem of elems) { sum += elem.value; } button.addEventListener('click', function() { console.log(sum); });`

Numbers are entered into the first two inputs. By clicking on the button, the sum of these numbers should be displayed in the third input:

`<input id="inp1"> <input id="inp2"> <input id="inp3"> <button id="btn">click</button>` `let btn = document.querySelector('#btn'); let inp1 = document.querySelector('#inp1'); let inp2 = document.querySelector('#inp2'); let inp3 = document.querySelector('#inp3'); btn.addEventListener('click', function() { inp3.textContent = inp1.textContent + inp2.textContent; });`

Numbers are entered into the first two inputs. By clicking on the button, the sum of these numbers should be displayed in the paragraph:

`<input id="inp1"> <input id="inp2"> <p id="res"></p> <button id="btn">click</button>` `let btn = document.querySelector('btn'); let res = document.querySelector('res'); let inp1 = document.querySelector('inp1'); let inp2 = document.querySelector('inp2'); button.addEventListener('click', function() { res.value = inp1.value + inp2.value; });`

On button click, you need to check that the text in each input matches the text of its data attribute:

`<input data-text="text1"> <input data-text="text2"> <input data-text="text3"> <button id="btn">click</button>` `let inputs = document.querySelectorAll('input') let button = document.querySelector('#button') button.addEventListener('click',function() { for (let input of inputs) { if (input.value === input.dataset) { input.classList.add('right') } else { input.classList.add('wrong') } } });`

By clicking on the button, you need to check that the text in each input matches the text of the corresponding array element:

`<input> <input> <input> <button id="btn">click</button>` `let inputs = document.querySelectorAll('input') let button = document.querySelector('#button') let texts = [ 'text1', 'text2', 'text3', ]; button.addEventListener('click',function() { for (let input of inputs) { for (let text of texts) { if (input.value === text) { input.classList.add('right') } else { input.classList.add('wrong') } } } });`

Numbers are entered into the inputs. By clicking on the button, the sum of the entered numbers should be displayed in the console:

`<input> <input> <input> <button id="btn">click</button>` `let inputs = document.querySelectorAll('inputs'); let btn = document.querySelector('#btm'); let sum = 0; btn.addEventListener('click', function() { for (let input of inputs) { sum += Number(input); } console.log(sum); });`

Numbers are entered into the first two inputs. By clicking on the button, the sum of these numbers should be displayed in the third input:

`<input id="inp1"> <input id="inp2"> <input id="inp3"> <button id="btn">click</button>` `let btn = document.querySelector('#btn'); let inp1 = document.querySelector('#inp1'); let inp2 = document.querySelector('#inp2'); let inp3 = document.querySelector('#inp3'); let sum = inp1.value + inp2.value; btn.addEventListener('click', function() { inp3.value = sum; });`

A number is entered into the input. Upon loss of focus, the sum of the digits of this number should be displayed in the console:

`<input id="inp">` `let inp = document.querySelector('#inp').value; inp.addEventListener('blur', function() { let digits = +inp.split(''); let sum = 0; for (let digit of digits) { sum += digit; } console.log(sum); });`
← →
↑
en ru