Practice and reinforce the concepts from Lesson 12
In this coding challenge, you will:
Time estimate: 5 minutes
academic-telebort/Web-3-Svelte-Coding-Challenge-Challenge3
src/lib
folder to create your components⚠️ Warning
⚠️ IMPORTANT: Before You Start
DO NOT DELETE the existing files in the template:
- Package files
- Any other files you didn't create
ONLY EDIT the necessary files.
In this coding challenge, you will combine multiple Svelte concepts including data binding, logic blocks, reactivity, and Firebase integration. These challenges will test your ability to create complete, interactive applications using advanced Svelte features.
Time estimate: 20-25 minutes
Svelte REPL (Remove all the codes before doing exercise)
Objective: Create an output that displays a student's name, ranking and score immediately when you type their name in the text input.
Sample Output: Video Demo
Copy and paste the provided code in App.svelte with the students array and card structure.
Create an input field with data binding to search for student names:
bind:value={studentName}
for two-way data bindingtext-center
classImplement Svelte logic to display student results when name matches:
{#each students as student}
to iterate through the array{#if student.name === studentName}
to match the input💡 Tip Helpful hint: Make sure the name matching is case-sensitive. You might want to consider adding
.toLowerCase()
for case-insensitive search!
<script>
let studentName;
let students = [
\{ranking:1, name:"Sharvin", score: 89.6\},
\{ranking:2, name:"Yong Sheng", score: 80.5\},
\{ranking:3, name:"Mai", score: 79.9\},
\{ranking:4, name:"Ravan", score: 79.5\},
\{ranking:5, name:"Jia Sheng", score: 75.0\},
\{ranking:6, name:"Ammar", score: 65.7\}
]
</script>
<section class="text-center">
<input type="text" bind:value=\{studentName\} placeholder="Enter student name">
</section>
\{#each students as student\}
\{#if student.name === studentName\}
<section class="text-center mt-5">
<div class="card">
<div class="card-header">
Telebort Student Result
</div>
<div class="card-body">
<h4 class="card-title">\{student.name\}</h4>
<p class="card-text">Ranking : \{student.ranking\}</p>
<p class="card-text">Score : \{student.score\}</p>
</div>
</div>
</section>
\{/if\}
\{/each\}
<style>
@import url("https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css");
</style>
Input.svelte
component for the search inputResult.svelte
component for the result cardℹ️ Time for bonus: Add 10-15 minutes if attempting the bonus challenge
Time estimate: 15-20 minutes
Svelte REPL (Remove all the codes before doing exercise)
Objective: Build a calculator that calculates a cylinder's volume and surface area with toggle functionality.
Sample Output: Video Demo
Surface Area:
Volume:
changeFormula()
function to toggle between formulas2πrh + 2πr²
πr²h
<script>
let radius, height, result, formula;
let clicked = false;
function changeFormula() \{
clicked = !clicked;
\}
$: if (clicked) \{
formula = "Surface Area";
result = (2*Math.PI*radius*height)+(2*Math.PI*radius*radius);
\} else \{
formula = "Volume";
result = Math.PI * radius * radius * height;
\}
</script>
<section class="text-center">
<h1>Calculate Volume and Surface Area of Cylinder</h1>
<p>Radius</p>
<input type="number" bind:value=\{radius\}>
<p>Height</p>
<input type="number" bind:value=\{height\}>
</section>
<section class="text-center mt-2">
<button on:click=\{changeFormula\}>
\{formula\}
</button>
<h4>
\{formula\}: \{result\}
</h4>
</section>
<style>
@import url("https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css");
</style>
💡 Tip Common issue: If you see
NaN
(Not a Number), make sure both radius and height have values before calculating!
.toFixed(2)
Time estimate: 45-60 minutes
Use StackBlitz SvelteKit Template: academic-telebort/telebort-web-series-W3-SvelteCodingChallenge-C3
Objective: Create a Book Inventory Table using Firebase with add and delete functionality.
Sample Output: Video Demo
Firebase.js
file⚠️ Warning Important: Make sure your Firebase project has Authentication and Firestore enabled before starting!
auth
, getFirestore
, collection
, addDoc
saveBookDetails()
function to add books to Firestore<script>
import \{auth\} from '../firebase';
import \{ onMount\} from 'svelte';
import \{onAuthStateChanged \} from "firebase/auth";
import \{getFirestore,collection,addDoc\} from 'firebase/firestore';
let title, genre, author, publishedYear, quantity, uid;
let db = getFirestore();
onMount(() => \{
onAuthStateChanged(auth, async (user) => \{
uid = user.uid;
\});
\});
async function saveBookDetails() \{
try\{
await addDoc(collection(db, "books"),\{
title: title,
genre: genre,
author: author,
publishedYear: publishedYear,
quantity: quantity,
uid: uid
\});
\}catch(error)\{
console.log(error);
\}
location.reload();
\}
</script>
<!-- Modal HTML with form inputs bound to variables -->
💡 Tip Firebase tip: Remember to handle errors gracefully using try-catch blocks when working with Firestore operations!
fetchBookList()
function to retrieve books from FirestoredeleteBook()
function to remove books from the collection{#each}
block to display books in table format<script>
import \{onAuthStateChanged\} from 'firebase/auth';
import \{getFirestore,collection,onSnapshot, doc,deleteDoc\} from 'firebase/firestore';
import \{auth\} from '../firebase';
import \{onMount\} from 'svelte';
let bookList = [];
let db = getFirestore();
let uid;
onMount(() => \{
onAuthStateChanged(auth, async (user) => \{
uid = user.uid;
fetchBookList();
\});
\});
async function fetchBookList()\{
try\{
let unsubscribe = onSnapshot(collection(db, "books"),(querySnapshot)=>\{
let bookLists = [];
querySnapshot.forEach((doc)=>\{
let item = \{...doc.data(),id: doc.id\}
bookLists = [item,...bookLists];
\});
bookList = bookLists;
\});
\}catch(error)\{
console.log(error);
\}
\}
async function deleteBook(id) \{
try \{
await deleteDoc(doc(db, "books", id));
\}catch(error)\{
console.log(error);
\}
\}
</script>
<!-- Table HTML with book data display -->
index.svelte
Make sure your Coding Challenge projects include:
bind:value
{#each}
{#if}
$:
{#if}
condition is inside the {#each}
blockchangeFormula()
function is properly called with on:click
onSnapshot
listener is properly set updeleteDoc()
ℹ️ Info
Ready to submit? Follow these steps:
- Test all functionality thoroughly
- Ensure all three challenges are complete
- Commit and push your code if using GitHub
- Submit your projects using the form below
⚠️ Warning Before submitting: Make sure all features work correctly and your code follows Svelte best practices!