# Finding errors in code with loops in JavaScript

In the following problems, 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 output numbers from `0` to `10`:

`for (let i = 0; i > 10; i++) { console.log(i); }`

The code should output numbers from `10` to `0`:

`for (let i = 10; i > 0; i++) { console.log(i); }`

The code should output numbers from `10` to `0`:

`for (let i = 10; i == 0; i--) { console.log(i); }`

The code should output numbers from `0` to `10`:

`let i = 0; while (i >= 10) { console.log(i); i++; }`

The code should find the sum of integers from `1` to `10`:

`let res; for (let i = 1; i <= 10; i++) { res += i; } console.log(res);`

The code should find the product of integers from `1` to `10`:

`let res = 0; for (let i = 1; i <= 10; i++) { res *= i; } console.log(res);`

The code should find the sum of the array elements:

`let arr = ['1', '2', '3', '4', '5']; let sum = 0; for (let elem of arr) { sum += elem; } console.log(sum); // should output 15`

The code should find the sum of the array elements:

`let arr = ['1', '2', '3', '4', '5']; let sum = ''; for (let elem of arr) { sum += +elem; } console.log(sum); // should output 15`

The code should find the sum of the array elements:

`let arr = ['1', '2', '3', '4', '5']; let sum = 0; for (let elem of arr) { sum = +elem; } console.log(sum); // should output 15`

The code should find the sum of the array elements, however, it always outputs `NaN`:

`let arr = ['1', '2', '3', '4', '5']; let sum = 0; for (let i = 0; i <= arr.length; i++) { sum += +arr[i]; } console.log(sum); // for some reason outputs NaN`

The code should find the sum of the array elements:

`let arr = ['1', '2', '3', '4', '5']; let sum = 0; for (let i = 0; i < arr.length - 1; i++) { sum += +arr[i]; } console.log(sum); // for some reason it doesn't output 15`

The code should find the sum of the array elements:

`let arr = ['1', '2', '3', '4', '5']; let sum = 0; for (let i = 0; i < arr.length; i++) { sum += +i; } console.log(sum); // for some reason it doesn't output 15`

The code should square each element of the array:

`let arr = [1, 2, 3, 4, 5]; for (let elem of arr) { elem = elem ** 2; } console.log(arr);`

The code should fill the array with numbers from `1` to `5`:

`let arr; for (let i = 1; i <= 5; i++) { arr.push(i); } console.log(arr);`

The code should find the sum of the elements of the object:

`let obj = {a: 1, b: 2, c: 3}; let sum = 0; for (let elem in obj) { sum += elem; } console.log(sum);`

The code should find the sum of the elements of the object:

`let obj = {a: 1, b: 2, c: 3}; let sum = 0; for (let key in obj) { sum = +obj.key; } console.log(sum);`

The code should check if the array contains the number `3` or not:

`let arr = [1, 2, 3, 4, 5]; let res = ''; for (let elem of arr) { if (elem === 3) { res = '+++'; } else { res = '---'; } } console.log(res);`

The code should fill the array with numbers from `1` to `5`:

`for (let i = 1; i <= 5; i++) { arr.push(i); } console.log(arr);`

The code should check if the array contains the number `3` or not:

`let arr = [1, 2, 3, 4, 5]; let res = false; for (let elem of arr) { if (elem === 3) { let res = true; break; } } console.log(res);`

The code should output only even elements from the array:

`let arr = [1, 2, 3, 4, 5]; for (let elem of arr) { if (elem % 2 = 0) { console.log(elem); } }`

The code should write to the new array only the odd elements of the old array:

`let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; for (let elem of arr) { if (elem % 2 != 0) { res.push(elem); } } console.log(res);`
←→
↑
en ru