Assess your knowledge of Sass control directives with a quiz focusing on the syntax, logic, and practical uses of @if, @for, and @each statements in stylesheet preprocessing. Strengthen your grasp of conditional logic and looping constructs in Sass to enhance your CSS coding efficiency.
In Sass, which of the following statements correctly uses the @if directive to apply a green background only when a variable $theme is 'success'?
Explanation: The correct answer uses the proper Sass syntax for @if: '==', no parentheses, and correct placement of the condition and block. Option A is incorrect because it uses a single '=', which is an assignment operator, not a comparison. Option C incorrectly omits quotes and uses parentheses in an invalid way. Option D adds a colon after @if, which is not valid Sass syntax.
What will be the output of the Sass code '@for $i from 1 through 3' in terms of loop iterations?
Explanation: @for $i from 1 through 3 runs the loop for values 1, 2, and 3, as 'through' includes the end value. Option B is incorrect because it falsely includes a value (4) outside the specified range. Option C is incorrect since the loop starts at 1, not 2. Option D is wrong because the loop does execute three times according to these bounds.
Which of the following @each loops contains a syntax error for iterating through a color list?
Explanation: The correct answer is '@each $color of (red, blue, green) { color: $color; }' because the 'of' keyword is not valid; 'in' should be used in Sass. Option A, B, and D use different ways of listing items but valid syntax. Option D uses a space-separated list, which is also acceptable in Sass.
When using a nested @if inside an @each loop in Sass to apply a $primary color class, which comparison operator is correct to check for equality?
Explanation: In Sass, the correct comparison operator for equality in an @if directive is '=='. Option A, '===', is not supported in Sass and is a strict equality operator in some programming languages. Option C uses '=', which is for assignment, not comparison. Option D, '!=', checks for inequality rather than equality.
Given the Sass code '@each $fruit, $color in (apple red, banana yellow, grape purple)', what will each loop iteration assign to $fruit and $color?
Explanation: With destructuring in @each, each variable receives the respective values from the nested lists in order, correctly assigning $fruit to the fruit name and $color to its color. Option A is incorrect since the pairing is not of all fruits with all colors. Option C is false; both variables are properly assigned. Option D is incorrect as Sass allows destructuring in @each loops.