Skip to content

Instantly share code, notes, and snippets.

@craftybones
Created December 10, 2024 08:59
Show Gist options
  • Save craftybones/a7edc0d43fffb219fe4e3037d1fbba2a to your computer and use it in GitHub Desktop.
Save craftybones/a7edc0d43fffb219fe4e3037d1fbba2a to your computer and use it in GitHub Desktop.

Extend generatePattern with a Secondary Pattern

Instructions

Your task is to modify the generatePattern(style, dimensions, secondStyle) function to support generating two patterns side by side. The patterns will share the same dimensions and will be separated by a single space.


Key Details

  1. Third Parameter (secondStyle):

    • The secondStyle parameter specifies the style of the second pattern.
    • If secondStyle is not provided, the function should generate only the first pattern, as before.
    • Both patterns will use the same dimensions.
  2. Row Alignment:

    • If the first pattern has shorter rows (fewer characters) than the second, trailing spaces should be added to align both patterns horizontally. This ensures proper visual alignment.
  3. Output Format:

    • Each row from the first pattern should be combined with the corresponding row from the second pattern, separated by a single space (' ').
    • Rows should be joined with a newline character ('\n') at the end of each row.
    • There should be no trailing newline at the end of the final row.
  4. Spacing Between Patterns:

    • A single space will separate the two patterns in each row. No extra spaces should appear after the second pattern.
  5. Dimension Validity:

    • The dimensions parameter is always valid for both patterns. You do not need to handle mismatched or invalid dimensions in this assignment.

Examples

Example 1: filled-rectangle and hollow-rectangle

generatePattern('filled-rectangle', [3, 3], 'hollow-rectangle');
// Output:
*** ***
*** * *
*** ***

Example 2: triangle and right-aligned-triangle

generatePattern('triangle', [3], 'right-aligned-triangle');
// Output:
*     *
**   **
*** ***

Example 3: diamond and hollow-diamond

generatePattern('diamond', [5], 'hollow-diamond');
// Output:
  *     *
 ***   * *
***** *   *
 ***   * *
  *     *

Example 4: alternating-rectangle and spaced-alternating-rectangle

generatePattern('alternating-rectangle', [4, 3], 'spaced-alternating-rectangle');
// Output:
**** ****
---- ----
****

Notes

  • Always assume that the dimensions will apply equally well to both patterns
  • Row Alignment: If one pattern has shorter rows, spaces must be added to the shorter rows so that the patterns align correctly.
  • Trailing Spaces: There must be no trailing spaces at the end of any row in the output.
  • Separation: A single space (' ') must separate the two patterns in each row.
  • Edge Cases: If secondStyle is not provided, the function should behave exactly as it did before, generating only the first pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment