Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Using for directive in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
For Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple for directive loop
What is the output of this blockchain smart contract snippet using a for directive to iterate over transactions?
Blockchain / Solidity
let total = 0;
for (let i = 0; i < 3; i++) {
  total += i * 10;
}
console.log(total);
A30
B60
C0
DSyntaxError
Attempts:
2 left
💡 Hint
Remember the loop runs from 0 to 2, adding i*10 each time.
🧠 Conceptual
intermediate
1:30remaining
Understanding for directive in blockchain event processing
In a blockchain smart contract, which best describes the purpose of a for directive when processing a list of events?
ATo repeat actions for each event in the list sequentially
BTo execute only the first event and skip the rest
CTo randomly select events to process
DTo permanently stop the contract execution
Attempts:
2 left
💡 Hint
Think about how loops help handle multiple items.
🔧 Debug
advanced
2:00remaining
Identify the error in this for directive code
What error does this blockchain smart contract code produce?
Blockchain / Solidity
for (let i = 0; i < 5; i++) {
  console.log(i);
}
AReferenceError because console is undefined
BTypeError because i is not defined
CSyntaxError due to missing parentheses in for loop
DNo error, prints numbers 0 to 4
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header.
❓ Predict Output
advanced
2:30remaining
Output of nested for directives in blockchain code
What is the output of this nested for directive code snippet?
Blockchain / Solidity
let result = '';
for (let i = 1; i <= 2; i++) {
  for (let j = 1; j <= 2; j++) {
    result += `${i}${j} `;
  }
}
console.log(result.trim());
A"1 2 1 2"
B"12 11 22 21"
C"SyntaxError"
D"11 12 21 22"
Attempts:
2 left
💡 Hint
The outer loop controls the first digit, inner loop the second.
🚀 Application
expert
3:00remaining
Calculating total token transfers using for directive
Given this blockchain code snippet that sums token transfers, what is the final value of totalTokens?
Blockchain / Solidity
const transfers = [100, 200, 50, 150];
let totalTokens = 0;
for (const amount of transfers) {
  if (amount > 100) {
    totalTokens += amount;
  }
}
console.log(totalTokens);
A500
B350
C100
DSyntaxError
Attempts:
2 left
💡 Hint
Add only amounts greater than 100.

Practice

(1/5)
1. What does the for directive do in blockchain programming?
easy
A. Creates a new blockchain transaction
B. Encrypts blockchain data
C. Deletes data from the blockchain
D. Repeats a block of code for each item in a list

Solution

  1. Step 1: Understand the purpose of the for directive

    The for directive is used to repeat code multiple times, once for each item in a list or collection.
  2. Step 2: Relate to blockchain data processing

    In blockchain, it helps process multiple data items like transactions or blocks easily by looping through them.
  3. Final Answer:

    Repeats a block of code for each item in a list -> Option D
  4. Quick Check:

    for directive = repeat code per item [OK]
Hint: For loops repeat code for each item in a list [OK]
Common Mistakes:
  • Confusing for directive with data encryption
  • Thinking for creates or deletes blockchain data
  • Mixing for directive with transaction creation
2. Which of the following is the correct syntax to loop over an array blocks using the for directive?
easy
A. for (let block of blocks) { /* code */ }
B. for (let block in blocks) { /* code */ }
C. for blocks (let block) { /* code */ }
D. loop (block in blocks) { /* code */ }

Solution

  1. Step 1: Identify correct for loop syntax for arrays

    In modern blockchain programming (JavaScript style), for...of loops over array elements directly.
  2. Step 2: Compare options

    for (let block of blocks) { /* code */ } uses for (let block of blocks), which is correct. for (let block in blocks) { /* code */ } uses in which loops keys, not values. Options C and D are invalid syntax.
  3. Final Answer:

    for (let block of blocks) { /* code */ } -> Option A
  4. Quick Check:

    Use for...of to loop array values [OK]
Hint: Use for...of to loop over array items directly [OK]
Common Mistakes:
  • Using for...in to loop arrays (loops keys, not values)
  • Writing invalid loop syntax
  • Confusing loop keywords like loop or for blocks
3. What will be the output of this code snippet?
const blocks = [101, 102, 103];
let result = '';
for (const block of blocks) {
  result += block + '-';
}
console.log(result);
medium
A. 101-102-103
B. 101-102-103-
C. 101102103
D. SyntaxError

Solution

  1. Step 1: Understand the loop operation

    The loop goes through each number in blocks: 101, 102, 103. Each number is added to result with a '-' after it.
  2. Step 2: Trace the string concatenation

    After loop: result = '101-' + '102-' + '103-' = '101-102-103-'
  3. Final Answer:

    101-102-103- -> Option B
  4. Quick Check:

    Concatenate each block with '-' = 101-102-103- [OK]
Hint: Add '-' after each item in loop to get trailing dash [OK]
Common Mistakes:
  • Expecting no trailing dash at the end
  • Confusing for...of with for...in output
  • Thinking output is concatenated without dashes
4. Identify the error in this for loop that processes blockchain transactions:
const transactions = [10, 20, 30];
for (transaction of transactions) {
  console.log(transaction);
}
medium
A. for loop syntax is invalid
B. Incorrect array name
C. Missing declaration keyword for loop variable
D. console.log cannot print transaction

Solution

  1. Step 1: Check loop variable declaration

    The loop variable transaction is used without let or const, which causes a ReferenceError in strict mode.
  2. Step 2: Validate other parts

    Array name and syntax are correct. console.log can print numbers. So only missing declaration is the error.
  3. Final Answer:

    Missing declaration keyword for loop variable -> Option C
  4. Quick Check:

    Declare loop variable with let or const [OK]
Hint: Always declare loop variable with let or const [OK]
Common Mistakes:
  • Forgetting let or const before loop variable
  • Assuming console.log can't print numbers
  • Thinking array name is wrong
5. You have a list of blockchain block heights: [5, 10, 15, 20]. Using a for directive, how do you create a new array with each height doubled?
hard
A. for (let i = 0; i < heights.length; i++) { doubled[i] = heights[i] * 2; }
B. for (heights of doubled) { doubled.push(heights * 2); }
C. for (let h in heights) { doubled.push(h * 2); }
D. for (h of heights) { doubled.push(h * 2); }

Solution

  1. Step 1: Understand the goal and data

    We want to double each number in heights and store in doubled array. heights = [5,10,15,20]
  2. Step 2: Analyze each option

    for (h of heights) { doubled.push(h * 2); } uses for...of but missing let or const declaration for loop variable h, so ReferenceError. for (let i = 0; i < heights.length; i++) { doubled[i] = heights[i] * 2; } uses classic for loop with index to assign doubled[i] = heights[i]*2, which works correctly. for (let h in heights) { doubled.push(h * 2); } uses for...in which loops keys (indexes as strings), so multiplying keys is wrong. for (heights of doubled) { doubled.push(heights * 2); } has invalid syntax (for (heights of doubled)) and reversed arrays.
  3. Final Answer:

    for (let i = 0; i < heights.length; i++) { doubled[i] = heights[i] * 2; } -> Option A
  4. Quick Check:

    Use index loop to assign doubled values [OK]
Hint: Use index loop to fill new array with doubled values [OK]
Common Mistakes:
  • Using for...in to loop array values
  • Not initializing doubled array before pushing
  • Writing invalid loop syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard