Bird
Raised Fist0
SASSmarkup~20 mins

Why custom functions are useful in SASS - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sass Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use custom functions in Sass?
Which of the following best explains why custom functions are useful in Sass?
AThey automatically convert Sass files into JavaScript files for better browser compatibility.
BThey allow you to reuse code by performing calculations and returning values, making stylesheets easier to maintain.
CThey replace CSS selectors with HTML tags to simplify styling.
DThey let you write CSS without any syntax rules or structure.
Attempts:
2 left
💡 Hint
Think about how functions help avoid repeating code and make styles easier to update.
📝 Syntax
intermediate
2:00remaining
Output of a Sass custom function
What is the output CSS of this Sass code using a custom function? @function double($n) { @return $n * 2; } .box { width: double(5rem); }
SASS
@function double($n) {
  @return $n * 2;
}

.box {
  width: double(5rem);
}
ASyntaxError: Invalid function call
B
.box {
  width: double(5rem);
}
C
.box {
  width: 5rem;
}
D
.box {
  width: 10rem;
}
Attempts:
2 left
💡 Hint
The function doubles the input value.
❓ selector
advanced
2:00remaining
Using custom functions with selectors
Consider this Sass code with a custom function that returns a color. What color will the .alert background be in the compiled CSS? @function alert-color($type) { @if $type == 'error' { @return #ff0000; } @else if $type == 'success' { @return #00ff00; } @else { @return #0000ff; } } .alert { background-color: alert-color('error'); }
SASS
@function alert-color($type) {
  @if $type == 'error' {
    @return #ff0000;
  } @else if $type == 'success' {
    @return #00ff00;
  } @else {
    @return #0000ff;
  }
}

.alert {
  background-color: alert-color('error');
}
A
.alert {
  background-color: #ff0000;
}
B
.alert {
  background-color: #00ff00;
}
C
.alert {
  background-color: #0000ff;
}
DSyntaxError: Invalid color value
Attempts:
2 left
💡 Hint
The function returns red for 'error' type.
❓ layout
advanced
2:00remaining
Custom function impact on layout values
Given this Sass code, what will be the margin value in the compiled CSS? @function spacing($factor) { @return $factor * 1.5rem; } .container { margin: spacing(2); }
SASS
@function spacing($factor) {
  @return $factor * 1.5rem;
}

.container {
  margin: spacing(2);
}
A
.container {
  margin: 3rem;
}
B
.container {
  margin: 2rem;
}
CTypeError: Cannot multiply number by unit
D
.container {
  margin: spacing(2);
}
Attempts:
2 left
💡 Hint
Multiply 2 by 1.5rem.
❓ accessibility
expert
2:30remaining
Using custom functions to improve accessibility
How can custom functions in Sass help improve accessibility in web design?
ABy converting all text to uppercase for better visibility.
BBy automatically adding ARIA attributes to HTML elements.
CBy calculating and returning color contrast ratios to ensure text is readable against backgrounds.
DBy disabling keyboard navigation to simplify user interaction.
Attempts:
2 left
💡 Hint
Think about how colors and contrast affect readability.

Practice

(1/5)
1. Why are custom functions useful in Sass?
easy
A. They make JavaScript run faster.
B. They let you reuse code and calculations easily.
C. They replace HTML tags.
D. They automatically fix CSS errors.

Solution

  1. Step 1: Understand the purpose of custom functions

    Custom functions in Sass allow you to write reusable code snippets that perform calculations or return values.
  2. Step 2: Identify the benefit of reusing code

    Reusing code helps keep styles consistent and easier to maintain, which is exactly what custom functions provide.
  3. Final Answer:

    They let you reuse code and calculations easily. -> Option B
  4. Quick Check:

    Custom functions = reuse code [OK]
Hint: Custom functions help reuse code and calculations [OK]
Common Mistakes:
  • Thinking custom functions fix CSS errors
  • Confusing Sass functions with HTML or JavaScript features
  • Believing custom functions change JavaScript behavior
2. Which of the following is the correct way to define a custom function in Sass?
easy
A. @function calculate-spacing($size) { @return $size * 2; }
B. function calculate-spacing($size) { return $size * 2; }
C. def calculate-spacing($size): return $size * 2
D. calc-spacing($size) => $size * 2;

Solution

  1. Step 1: Recall Sass function syntax

    Sass custom functions start with @function, followed by the function name and parameters in parentheses.
  2. Step 2: Check the return statement

    The function must use @return to send back a value.
  3. Final Answer:

    @function calculate-spacing($size) { @return $size * 2; } -> Option A
  4. Quick Check:

    @function + @return = correct syntax [OK]
Hint: Sass functions start with @function and use @return [OK]
Common Mistakes:
  • Omitting @function keyword
  • Using JavaScript or Python syntax instead
  • Forgetting @return inside the function
3. What will be the output CSS of this Sass code?
@function double($n) { @return $n * 2; }
.box { margin: double(10px); }
medium
A. .box { margin: double(10px); }
B. Syntax error
C. .box { margin: 10px; }
D. .box { margin: 20px; }

Solution

  1. Step 1: Understand the custom function call

    The function double multiplies the input by 2, so double(10px) returns 20px.
  2. Step 2: Check the compiled CSS output

    The Sass compiler replaces the function call with the returned value, so the margin becomes 20px.
  3. Final Answer:

    .box { margin: 20px; } -> Option D
  4. Quick Check:

    Function doubles 10px to 20px [OK]
Hint: Custom functions return values replacing calls in CSS [OK]
Common Mistakes:
  • Expecting function name to appear in CSS
  • Thinking Sass functions run in browser
  • Confusing input and output units
4. Identify the error in this Sass custom function:
@function add($a, $b) { return $a + $b; }
medium
A. Using 'return' instead of '@return' causes error
B. Function name cannot be 'add'
C. Missing @return keyword inside the function
D. Parameters must be declared without $

Solution

  1. Step 1: Check Sass function syntax

    Sass requires @return keyword to return values, not plain return.
  2. Step 2: Identify the error

    Using return without @ causes a syntax error in Sass.
  3. Final Answer:

    Using 'return' instead of '@return' causes error -> Option A
  4. Quick Check:

    @return required, not return [OK]
Hint: Use @return, not return, inside Sass functions [OK]
Common Mistakes:
  • Writing 'return' without @ in Sass
  • Thinking function names are restricted
  • Omitting $ in parameter names
5. You want to create a custom function in Sass that returns a color with adjusted opacity. Which approach best uses custom functions to keep your design consistent?
hard
A. Use JavaScript to change opacity after the page loads.
B. Write the rgba color manually every time you need opacity.
C. Define a function that takes a color and opacity, then returns the color with that opacity applied.
D. Create multiple CSS classes for each opacity level.

Solution

  1. Step 1: Understand the goal of consistency and reuse

    Using a custom function to adjust opacity means you write the logic once and reuse it everywhere.
  2. Step 2: Compare options for maintainability

    Manually writing rgba or using JS or many classes is less maintainable and less consistent than a function.
  3. Final Answer:

    Define a function that takes a color and opacity, then returns the color with that opacity applied. -> Option C
  4. Quick Check:

    Custom function for opacity = consistent design [OK]
Hint: Use functions to apply opacity consistently [OK]
Common Mistakes:
  • Hardcoding colors repeatedly
  • Relying on JavaScript for style changes
  • Creating many CSS classes instead of functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard