Practice
1. What does the
where method do when querying collection entries in Astro Framework?easy
Solution
Step 1: Understand the purpose of
Thewherewheremethod is used to select only those entries that meet a specific condition.Step 2: Compare with other options
Deleting, adding, or sorting entries are different operations not done bywhere.Final Answer:
It filters entries based on a given condition. -> Option AQuick Check:
wherefilters entries [OK]
Hint: Remember:
where means filter entries by condition [OK]Common Mistakes:
- Confusing
wherewith adding or deleting entries - Thinking
wheresorts entries - Assuming
wheremodifies the collection permanently
2. Which of the following is the correct syntax to filter entries where the field
status equals "active"?easy
Solution
Step 1: Identify correct function usage
Thewheremethod expects a function that takes an entry and returns a boolean condition.Step 2: Check each option's syntax
collection.where(entry => entry.status == "active")correctly uses a function with an arrow and comparesentry.statusto "active". Others have syntax errors or wrong usage.Final Answer:
collection.where(entry => entry.status == "active") -> Option AQuick Check:
Correct arrow function syntax =collection.where(entry => entry.status == "active")[OK]
Hint: Use arrow function with entry parameter in
where [OK]Common Mistakes:
- Using assignment (=) instead of comparison (==)
- Omitting the entry parameter in the function
- Passing condition directly without a function
3. Given the collection entries:
What will
[{ id: 1, type: "fruit" }, { id: 2, type: "vegetable" }, { id: 3, type: "fruit" }]What will
collection.where(entry => entry.type == "fruit") return?medium
Solution
Step 1: Identify entries matching the condition
Entries withtype == "fruit"are those with id 1 and 3.Step 2: Understand
Thewhereoutputwheremethod returns a list of matching entries, so it returns the two fruit entries.Final Answer:
[{ id: 1, type: "fruit" }, { id: 3, type: "fruit" }] -> Option BQuick Check:
Filter fruits = entries 1 and 3 [OK]
Hint: Filter entries by condition to get matching list [OK]
Common Mistakes:
- Returning non-matching entries
- Expecting a single entry instead of a list
- Confusing syntax error with correct code
4. What is wrong with this query?
collection.where(entry => entry.status = "active")
medium
Solution
Step 1: Identify the operator used
The query uses a single equals sign (=), which assigns a value instead of comparing.Step 2: Understand correct comparison syntax
To compare, double equals (==) must be used inside the function.Final Answer:
It uses assignment (=) instead of comparison (==). -> Option CQuick Check:
Use == for comparison, not = [OK]
Hint: Use == for comparison inside
where functions [OK]Common Mistakes:
- Using = instead of == causes wrong filtering
- Thinking parentheses are required around single parameter
- Believing
wherecan't take functions
5. You want to find all entries where
score is greater than 50 and passed is true. Which query correctly does this?hard
Solution
Step 1: Understand logical AND condition
To require both conditions, use the logical AND operator&&.Step 2: Check each option's logic
collection.where(entry => entry.score > 50 && entry.passed == true)uses&&correctly.collection.where(entry => entry.score > 50 || entry.passed == true)uses OR (||),collection.where(entry => entry.score > 50 & entry.passed == true)uses bitwise AND (&) which is incorrect here, andcollection.where(entry => entry.score > 50, entry.passed == true)has invalid syntax with two parameters.Final Answer:
collection.where(entry => entry.score > 50 && entry.passed == true) -> Option DQuick Check:
Use && for AND conditions inwhere[OK]
Hint: Use && to combine multiple conditions inside
where [OK]Common Mistakes:
- Using || instead of && for AND condition
- Using bitwise & instead of logical &&
- Passing multiple arguments instead of one function
