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.

Задание 1

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

html<p>1</p>
<p>2</p>
<p>3</p>
javascriptlet elems = document.querySelector('p');
elems.textContent += '!';
Задание 2

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

html<p>1</p>
<p>2</p>
<p>3</p>
javascriptlet elems = document.querySelectorAll('p');

for (let elem of elems) {
  elem.addEventListener('click', function() {
    this.textContent += Number(1);
  });
}
Задание 3

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

html<p>text</p>
<button>click</button>
javascriptlet button = document.querySelector('button');
let elem   = document.querySelector('p');

button.addEventListener('click', function() {
  elem.innerHTML = '<b>elem.innerHTML</b>';
});
Задание 4

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

html<p>1</p>
<p>2</p>
<p>3</p>

<button>click</button>
javascriptlet 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);
});
Задание 5

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

html<p>1</p>
<p>2</p>
<p>3</p>
javascriptlet elems = document.querySelectorAll('p');

for (let elem of elems) {
  elem.addEventListener('click', () => {
    this.textContent += '!';
  });
}
Задание 6

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

html<p>text1</p>
<p>text2</p>
<p>text3</p>
<button>click</button>
javascriptlet button = document.querySelector('button');
let elems  = document.querySelectorAll('p');

button.addEventListener('click', function() {
  for (let elem of elems) {
    elem.innerHTML = '<b>+elem.innerHTML+</b>';
  }
});
Задание 7

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

html<p>1</p>
<p>2</p>
<p>3</p>

<button>click</button>
javascriptlet 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);
  });
}
Задание 8

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:

html<input>
<input>
<input>

<button>click</button>
javascriptlet 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);
});
Задание 9

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:

html<input id="inp1">
<input id="inp2">
<input id="inp3">

<button id="btn">click</button>
javascriptlet 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;
});
Задание 10

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

html<input id="inp1">
<input id="inp2">
<p id="res"></p>

<button id="btn">click</button>
javascriptlet 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;
});
Задание 11

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

html<input data-text="text1">
<input data-text="text2">
<input data-text="text3">

<button id="btn">click</button>
javascriptlet 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')
    }
  }
});
Задание 12

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

html<input>
<input>
<input>

<button id="btn">click</button>
javascriptlet 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')
      }
    }
  }
});
Задание 13

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

html<input>
<input>
<input>

<button id="btn">click</button>
javascriptlet 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);
});
Задание 14

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:

html<input id="inp1">
<input id="inp2">
<input id="inp3">

<button id="btn">click</button>
javascriptlet 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;
});
Задание 15

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:

html<input id="inp">
javascriptlet 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);
});