satyacode
Background
Back to js-functions

07-mehndi-pattern.js

JavaScript
1/**
2 * 🎨 Mehndi Pattern Maker - Recursion
3 *
4 * Mehndi artist hai tu! Intricate patterns banane hain using RECURSION.
5 * Yahan loops use karna MANA hai — sirf function khud ko call karega
6 * (recursive calls). Har function mein base case aur recursive case hoga.
7 *
8 * Functions:
9 *
10 *   1. repeatChar(char, n)
11 *      - Repeat char n times using recursion (NO loops, NO .repeat())
12 *      - Base case: n <= 0 => return ""
13 *      - Recursive: char + repeatChar(char, n - 1)
14 *      - Agar char not a string or empty, return ""
15 *
16 *   2. sumNestedArray(arr)
17 *      - Sum all numbers in an arbitrarily nested array
18 *      - e.g., [1, [2, [3, 4]], 5] => 15
19 *      - Skip non-number values
20 *      - Base case: empty array => 0
21 *      - Agar input not array, return 0
22 *
23 *   3. flattenArray(arr)
24 *      - Flatten an arbitrarily nested array into a single flat array
25 *      - e.g., [1, [2, [3, 4]], 5] => [1, 2, 3, 4, 5]
26 *      - Agar input not array, return []
27 *
28 *   4. isPalindrome(str)
29 *      - Check if string is palindrome using recursion
30 *      - Case-insensitive comparison
31 *      - Base case: string length <= 1 => true
32 *      - Compare first and last chars, recurse on middle
33 *      - Agar input not string, return false
34 *
35 *   5. generatePattern(n)
36 *      - Generate symmetric mehndi border pattern
37 *      - n = 1 => ["*"]
38 *      - n = 2 => ["*", "**", "*"]
39 *      - n = 3 => ["*", "**", "***", "**", "*"]
40 *      - Pattern goes from 1 star up to n stars, then back down to 1
41 *      - Use recursion to build the ascending part, then mirror it
42 *      - Agar n <= 0, return []
43 *      - Agar n is not a positive integer, return []
44 *
45 * Hint: Every recursive function needs a BASE CASE (when to stop) and a
46 *   RECURSIVE CASE (calling itself with a smaller/simpler input).
47 *
48 * @example
49 *   repeatChar("*", 4)        // => "****"
50 *   sumNestedArray([1, [2, [3]]]) // => 6
51 *   flattenArray([1, [2, [3]]]) // => [1, 2, 3]
52 *   isPalindrome("madam")     // => true
53 *   generatePattern(3)        // => ["*", "**", "***", "**", "*"]
54 */
55export function repeatChar(char, n) {
56  if(n <= 0 || typeof char !== "string"){
57    return ""
58  }
59  return char + repeatChar(char, n-1)
60}
61
62export function sumNestedArray(arr) {
63  if(!Array.isArray(arr)) return 0
64  if(arr.length === 0) return 0
65  
66  const [first, ...rest]= arr;
67
68  if(Array.isArray(first)){
69    return sumNestedArray(first) +sumNestedArray(rest)
70  }else if(typeof first === "number"){
71    return first + sumNestedArray(rest)
72  }else{
73    return sumNestedArray(rest)
74  }
75}
76
77export function flattenArray(arr) {
78  if(!Array.isArray(arr)){
79    return []
80  }
81  if(arr.length === 0) return []
82
83  const [first, ...rest] = arr
84
85  if(Array.isArray(first)){
86    return[...flattenArray(first), ...flattenArray(rest)]
87  }else{
88    return [first, ...flattenArray(rest)]
89  }
90}
91
92export function isPalindrome(str) {
93  if(typeof str !== "string") return false
94  str = str.toLowerCase()
95  if(str.length <= 1) return true
96
97  if(str[0] !== str[str.length - 1]) return false
98  return isPalindrome(str.slice(1,-1))
99}
100
101export function generatePattern(n) {
102  if (n <= 0 || typeof n !== "number" || !Number.isInteger(n)) return [];
103  if (n === 1) return ["*"];
104  const smaller = generatePattern(n - 1);          
105  const ascending = smaller.slice(0, n - 1);       
106  return [...ascending, "*".repeat(n), ...ascending.slice().reverse()];
107}
108