Codistwa

Practice Loops - Quiz

beginner

level 1

You've read the guide, now test your knowledge of Loops! If you have any doubt, you can go back to the guide.

Question 1

Which code is used to perform a decrement?

1
javascript
1const basket = [1, 2, 3, 4, 5, 6, 7, 8];
2
3for (let i = basket.length; i >= 2; i--) {
4  console.log(i);
5}
2
javascript
1const basket = [1, 2, 3, 4, 5, 6, 7, 8];
2
3for (let i = 0; i < basket.length; i++) {
4  console.log(fruits[i] * 2);
5}
Select...

Question 2

How to triple them? (each column will have 3 fruits)

1
python
1fruits = [1, 1]
2
3for i in fruits:
4    print(i * 3)
5
2
python
1fruits = [1, 1]
2
3for i in range(len(fruits)):
4    print(i * 3)
Select...

Question 3

What does this code return?

javascript
1const fruits = [1, 2, 3];
2
3for (let i = 0; i < fruits.length; i++) {
4  console.log(fruits[i] * 2);
5}
Select...

Question 4

How to interpret this specification?

"Stop the loop when the 2nd index is reached".

1
javascript
1const fruits = [1, 9, 7, 4, 5];
2
3let i = 0;
4do {
5  console.log(fruits[i] * 2);
6  i++;
7} while (i < fruits.length);
2
javascript
1const fruits = [1, 9, 7, 4, 5];
2
3let i = 0;
4while (i < fruits.length) {
5  if (i === 2) {
6    break;
7  }
8  i++;
9  console.log(i);
10}
Select...