Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active December 10, 2024 07:42
Show Gist options
  • Save craftybones/df34cdca20ad41faf64791253556a87b to your computer and use it in GitHub Desktop.
Save craftybones/df34cdca20ad41faf64791253556a87b to your computer and use it in GitHub Desktop.

Hollow Diamond Pattern

Instructions

Your task is to extend the functionality of the generatePattern(style, dimensions) function to support a new pattern type: hollow-diamond.

For this assignment, the style parameter will be "hollow-diamond", and the dimensions parameter will be an array containing a single number [size] where:

  • size specifies the width and height of the diamond (both are the same).
  • Only odd numbers are valid for size:
    • If size is an even number, your function should generate a hollow diamond for the closest odd number below it.
    • For example, if size = 4, the function should generate a hollow diamond of size 3.

Pattern Description

  • The hollow diamond is symmetrical, with a maximum width of size at its middle row.
  • Each row consists of a single * at the beginning and end of the row (except the middle row, which is filled with *).
  • Spaces ( ) are used to maintain the diamond's shape.
  • Important: There should be no trailing spaces after the * on any row.
  • If size is 1, the hollow diamond will be a single *.
  • If size is 0 or less, the hollow diamond is considered empty, and the function should return an empty string.

Examples

generatePattern("hollow-diamond", [3]);
// Output:
  *
 * *
  *

generatePattern("hollow-diamond", [7]);
// Output:
    *
   * *
  *   *
 *     *
  *   *
   * *
    *

generatePattern("hollow-diamond", [4]); // Closest odd size is 3
// Output:
  *
 * *
  *

generatePattern("hollow-diamond", [1]);
// Output:
*

generatePattern("hollow-diamond", [0]);
// Output: (empty string)

Notes

  • No trailing spaces: Each row must end precisely after the last *. Do not leave unnecessary spaces at the end of any row.
  • The hollow diamond pattern should properly handle edge cases:
    • If size is 1, the function should return a single *.
    • If size is 0 or negative, the function should return an empty string.
    • If size is even, generate a hollow diamond using the closest odd number below it.
  • Each line of the output should be separated by a newline character (\n).

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