Challenge - 5 Problems
Loop Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a for loop in a smart contract
What is the output of this Solidity code snippet when the function
sumUpToFive() is called?Blockchain / Solidity
pragma solidity ^0.8.0; contract LoopTest { function sumUpToFive() public pure returns (uint) { uint sum = 0; for (uint i = 1; i <= 5; i++) { sum += i; } return sum; } }
Attempts:
2 left
💡 Hint
Add numbers from 1 to 5 using a loop.
✗ Incorrect
The for loop adds numbers 1 through 5 to sum. 1+2+3+4+5 equals 15.
❓ Predict Output
intermediate2:00remaining
While loop behavior in a blockchain contract
What will be the value of
counter after calling countDown() in this Solidity contract?Blockchain / Solidity
pragma solidity ^0.8.0; contract LoopCountdown { function countDown() public pure returns (uint) { uint counter = 5; while (counter > 0) { counter--; } return counter; } }
Attempts:
2 left
💡 Hint
The loop decreases counter until it is no longer greater than zero.
✗ Incorrect
The while loop decreases counter from 5 down to 0, then stops and returns 0.
🔧 Debug
advanced2:00remaining
Identify the error in this for loop
This Solidity function is intended to sum numbers from 1 to 3, but it causes a compilation error. What is the cause?
Blockchain / Solidity
pragma solidity ^0.8.0; contract LoopError { function sumThree() public pure returns (uint) { uint sum = 0; for (uint i = 1; i < 4; i++) sum += i; return sum; } }
Attempts:
2 left
💡 Hint
Check punctuation after statements inside the loop.
✗ Incorrect
The line 'sum += i' is missing a semicolon, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of nested loops in a smart contract
What is the output of the
nestedSum() function below?Blockchain / Solidity
pragma solidity ^0.8.0; contract NestedLoops { function nestedSum() public pure returns (uint) { uint total = 0; for (uint i = 1; i <= 2; i++) { for (uint j = 1; j <= 3; j++) { total += i * j; } } return total; } }
Attempts:
2 left
💡 Hint
Multiply i and j for each pair and add all results.
✗ Incorrect
The sums are: (1*1 + 1*2 + 1*3) + (2*1 + 2*2 + 2*3) = 6 + 12 = 18.
🧠 Conceptual
expert2:00remaining
Gas cost implications of loops in blockchain
Which statement best describes the gas cost behavior of loops in Ethereum smart contracts?
Attempts:
2 left
💡 Hint
Think about how Ethereum charges for computation steps.
✗ Incorrect
Each loop iteration executes instructions, increasing gas cost linearly with iterations.