Bird
Raised Fist0
Firebasecloud~10 mins

Upload progress monitoring in Firebase - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start an upload task using Firebase Storage.

Firebase
const uploadTask = storageRef.child('images/file.jpg').[1](file);
Drag options to blanks, or click blank then click option'
Aput
Bget
Cdelete
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'put' to upload.
Trying to delete before uploading.
2fill in blank
medium

Complete the code to listen for upload progress updates.

Firebase
uploadTask.on('state_changed', snapshot => {
  const progress = (snapshot.bytesTransferred / snapshot.[1]) * 100;
  console.log('Upload is ' + progress + '% done');
});
Drag options to blanks, or click blank then click option'
Asize
BmaxBytes
CtotalBytes
DfullBytes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'maxBytes' which does not exist.
Using 'size' which is not a snapshot property.
3fill in blank
hard

Fix the error in the code to correctly handle upload completion.

Firebase
uploadTask.on('state_changed', null, error => {
  console.error(error);
}, () => {
  console.log('Upload [1] successfully');
});
Drag options to blanks, or click blank then click option'
Astarted
Bcompleted
Cfailed
Dpaused
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failed' which is for errors.
Using 'started' which is before upload.
4fill in blank
hard

Fill both blanks to calculate and log upload progress percentage.

Firebase
uploadTask.on('state_changed', snapshot => {
  const progress = (snapshot.[1] / snapshot.[2]) * 100;
  console.log(`Progress: ${progress.toFixed(2)}%`);
});
Drag options to blanks, or click blank then click option'
AbytesTransferred
BtotalBytes
CmaxBytes
DbytesUploaded
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'maxBytes' which is invalid.
Using 'bytesUploaded' which is not a snapshot property.
5fill in blank
hard

Fill all three blanks to handle upload states and log messages accordingly.

Firebase
uploadTask.on('state_changed', snapshot => {
  switch(snapshot.state) {
    case '[1]':
      console.log('Upload is running');
      break;
    case '[2]':
      console.log('Upload is paused');
      break;
    case '[3]':
      console.log('Upload is canceled');
      break;
  }
});
Drag options to blanks, or click blank then click option'
Arunning
Bpaused
Ccanceled
Dcompleted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'completed' as a state in switch, which is handled differently.
Mixing up paused and canceled states.

Practice

(1/5)
1. What does the state_changed event in Firebase Storage upload tasks help you monitor?
easy
A. The number of files in storage
B. The download speed of the file
C. The file size before upload
D. The progress of the file upload

Solution

  1. Step 1: Understand the purpose of state_changed

    The state_changed event triggers updates during the upload process.
  2. Step 2: Identify what is monitored

    This event provides progress updates, such as bytes transferred and total bytes.
  3. Final Answer:

    The progress of the file upload -> Option D
  4. Quick Check:

    Upload progress = state_changed event [OK]
Hint: Remember: state_changed tracks upload progress [OK]
Common Mistakes:
  • Confusing upload progress with download speed
  • Thinking it tracks file size before upload
  • Assuming it counts files in storage
2. Which of the following is the correct way to attach a listener for upload progress in Firebase Storage using JavaScript?
easy
A. uploadTask.on('state_changed', progressCallback)
B. uploadTask.listen('progress', progressCallback)
C. uploadTask.addEventListener('upload', progressCallback)
D. uploadTask.track('state_change', progressCallback)

Solution

  1. Step 1: Recall Firebase Storage upload syntax

    The method to listen for upload events is on with event name state_changed.
  2. Step 2: Match correct method and event

    Only uploadTask.on('state_changed', progressCallback) is valid syntax.
  3. Final Answer:

    uploadTask.on('state_changed', progressCallback) -> Option A
  4. Quick Check:

    Use .on('state_changed') for progress [OK]
Hint: Use .on('state_changed') to monitor upload progress [OK]
Common Mistakes:
  • Using non-existent methods like listen or track
  • Wrong event names like 'progress' or 'upload'
  • Confusing event listener syntax with DOM events
3. Given the following code snippet, what will be logged if 500 bytes out of 1000 bytes are uploaded?
uploadTask.on('state_changed', (snapshot) => {
  const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  console.log(`Upload is ${progress}% done`);
});
medium
A. Upload is 0.5% done
B. Upload is 50% done
C. Upload is 500% done
D. Upload is 100% done

Solution

  1. Step 1: Calculate progress percentage

    Progress = (500 / 1000) * 100 = 50%
  2. Step 2: Understand console output

    The console logs: "Upload is 50% done".
  3. Final Answer:

    Upload is 50% done -> Option B
  4. Quick Check:

    Progress = bytesTransferred / totalBytes * 100 [OK]
Hint: Divide bytesTransferred by totalBytes, multiply by 100 [OK]
Common Mistakes:
  • Confusing bytesTransferred with percentage
  • Multiplying by 100 twice
  • Using integer division without decimal
4. You wrote this code to monitor upload progress but it never logs anything:
uploadTask.on('state_change', (snapshot) => {
  const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  console.log(`Upload is ${progress}% done`);
});
What is the error?
medium
A. The event name should be 'state_changed', not 'state_change'
B. The callback function is missing a return statement
C. You must use uploadTask.addListener instead of on
D. The progress calculation formula is incorrect

Solution

  1. Step 1: Check event name spelling

    The correct event name is 'state_changed', not 'state_change'.
  2. Step 2: Understand impact of wrong event name

    Using wrong event name means callback never triggers, so no logs appear.
  3. Final Answer:

    The event name should be 'state_changed', not 'state_change' -> Option A
  4. Quick Check:

    Event name must be exact 'state_changed' [OK]
Hint: Check event spelling: 'state_changed' not 'state_change' [OK]
Common Mistakes:
  • Misspelling event names
  • Expecting logs without callback firing
  • Confusing method names like addListener vs on
5. You want to display upload progress as a percentage and also show a message when upload completes. Which code snippet correctly implements this using Firebase Storage's state_changed event?
hard
A. uploadTask.on('progress', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); });
B. uploadTask.on('state_changed', snapshot => { console.log('Upload complete!'); });
C. uploadTask.on('state_changed', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); }, error => { console.error(error); }, () => { console.log('Upload complete!'); });
D. uploadTask.on('state_changed', () => { console.log('Upload is done'); });

Solution

  1. Step 1: Use correct event and callbacks

    The state_changed event accepts three callbacks: progress, error, and completion.
  2. Step 2: Verify progress calculation and completion message

    uploadTask.on('state_changed', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); }, error => { console.error(error); }, () => { console.log('Upload complete!'); }); calculates progress and logs completion correctly; others miss callbacks or use wrong event.
  3. Final Answer:

    uploadTask.on('state_changed', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); }, error => { console.error(error); }, () => { console.log('Upload complete!'); }); -> Option C
  4. Quick Check:

    Use three callbacks with 'state_changed' event [OK]
Hint: Use three callbacks with 'state_changed' for progress and completion [OK]
Common Mistakes:
  • Using wrong event names like 'progress'
  • Missing completion callback
  • Logging completion inside progress callback
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