0
0
Typescriptprogramming~30 mins

Why patterns matter for safety in Typescript - See It in Action

Choose your learning style9 modes available
Why patterns matter for safety
📖 Scenario: Imagine you are building a small program to check if certain actions are safe to perform based on a list of safety patterns. These patterns help prevent mistakes and keep things secure, like wearing a helmet when riding a bike or checking if a door is locked before leaving.
🎯 Goal: You will create a TypeScript program that stores safety patterns, sets a threshold for safety importance, filters the patterns based on that threshold, and then shows which patterns are important for safety.
📋 What You'll Learn
Create a dictionary called safetyPatterns with exact keys and numeric values representing importance.
Create a variable called importanceThreshold with the exact value 5.
Use a dictionary comprehension (object comprehension) to create a new dictionary called importantPatterns that only includes patterns with importance greater than or equal to importanceThreshold.
Print the importantPatterns dictionary.
💡 Why This Matters
🌍 Real World
Safety patterns help prevent accidents and mistakes in many areas like driving, working with machines, or software security.
💼 Career
Understanding how to filter and manage data based on conditions is a key skill in programming jobs, especially when working with safety-critical systems.
Progress0 / 4 steps
1
Create the safety patterns dictionary
Create a dictionary called safetyPatterns with these exact entries: 'WearHelmet': 7, 'CheckBrakes': 6, 'LockDoor': 4, 'UseReflectors': 5, 'AvoidDistractions': 8.
Typescript
Need a hint?

Use an object literal with keys and values exactly as shown.

2
Set the importance threshold
Create a variable called importanceThreshold and set it to the number 5.
Typescript
Need a hint?

Use const importanceThreshold = 5; to create the variable.

3
Filter important safety patterns
Create a new dictionary called importantPatterns that includes only the entries from safetyPatterns where the value is greater than or equal to importanceThreshold. Use Object.entries, filter, and reduce to build importantPatterns.
Typescript
Need a hint?

Use Object.entries to get pairs, then filter by importance, then reduce to build the new object.

4
Display the important safety patterns
Write a console.log statement to print the importantPatterns dictionary.
Typescript
Need a hint?

Use console.log(importantPatterns); to show the filtered dictionary.