ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SCSS Dynamic Class Generation with Prefix

SCSS

Goal -- WPM

Ready
Exercise Algorithm Area
1@mixin generate-class($prefix, $suffix) {
2$class-name: "#{$prefix}-#{$suffix}";
3.#{class-name} {
4// Placeholder for styles
5content: "Class: #{$class-name}";
6}
7}
8
9@include generate-class('icon', 'large');
Algorithm description viewbox

SCSS Dynamic Class Generation with Prefix

Algorithm description:

This SCSS mixin simplifies the creation of dynamic class names. It takes a prefix and a suffix, then generates a CSS class by combining them with a hyphen. This is useful for creating utility classes or component-specific modifiers where the name needs to be programmatically determined, such as `.icon-large`.

Algorithm explanation:

The `generate-class` mixin accepts a `$prefix` and a `$suffix`. It constructs a `$class-name` by interpolating these two variables with a hyphen in between. The `.` before `#{class-name}` in the rule ensures that the generated string is treated as a class selector. The `@content` directive is not used here; instead, placeholder styles are provided. The time complexity is O(1) as it performs a fixed number of operations. Space complexity is also O(1) for the generated class.

Pseudocode:

mixin generate_class(prefix, suffix):
  class_name = prefix + "-" + suffix
  define class .#{class_name}:
    // Placeholder styles