Your task is to extend the functionality of the generatePattern(style, dimensions) function to support a new pattern type: diamond.
For this assignment, the style parameter will be "diamond", and the dimensions parameter will be an array containing a single number [size] where:
sizespecifies the width and height of the diamond (both are the same).- Only odd numbers are valid for
size:- If
sizeis an even number, your function should generate a diamond for the closest odd number below it. - For example, if
size = 4, the function should generate a diamond of size3.
- If
- The diamond is symmetrical, with a maximum width of
sizeat its middle row. - Spaces (
) are used to center-align the rows so that the diamond is properly shaped. - There should be no trailing spaces after the
*on any row. - If
sizeis0or less, the diamond is considered empty, and the function should return an empty string.
generatePattern("diamond", [3]);
// Output:
*
***
*
generatePattern("diamond", [5]);
// Output:
*
***
*****
***
*
generatePattern("diamond", [4]); // Closest odd size is 3
// Output:
*
***
*
generatePattern("diamond", [1]);
// Output:
*
generatePattern("diamond", [0]);
// Output: (empty string)- The diamond pattern should properly handle edge cases:
- If
sizeis1, the function should return a single*. - If
sizeis0or negative, the function should return an empty string. - If
sizeis even, generate a diamond using the closest odd number below it.
- If
- The number of
*in each row increases by 2 for the top half, reachessizein the middle row, and decreases symmetrically for the bottom half. - Ensure that the function works for both small and large values of
size. - Each line of the output should be separated by a newline character (
\n).