Exercise 6: Storage with Expiry Time
Problem Statement
Function `setWithExpiry(key, value, ttl)` banao jo value ko expiry time ke saath store kare.
Example: setWithExpiry("token", "abc123", 3600000) → expires after 1 hour
Sample Output:
setWithExpiry("token", "abc123", 3600000)
// Stores with 1 hour expirySolution
const setWithExpiry = (key, value, ttl) => {
const item = {value, expiry: Date.now() + ttl};
localStorage.setItem(key, JSON.stringify(item));
};Explanation
Overall Goal:
- Value ko expiry time ke saath store karna.
Real world:
- Token storage: expiry management.
- Cache: time-based expiration.