Skip to content

Commit 358ad8c

Browse files
committed
Add more JavaScript snippets
1 parent e4fafc3 commit 358ad8c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

public/data/javascript.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,63 @@
141141
],
142142
"tags": ["javascript", "data", "utility"],
143143
"author": "realvishalrana"
144+
},
145+
{
146+
"title": "Check if String is a Palindrome",
147+
"description": "Checks whether a given string is a palindrome.",
148+
"code": [
149+
"function isPalindrome(str) {",
150+
" const cleanStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();",
151+
" return cleanStr === cleanStr.split('').reverse().join('');",
152+
"}",
153+
"",
154+
"// Example usage:",
155+
"console.log(isPalindrome('A man, a plan, a canal, Panama')); // Output: true"
156+
],
157+
"tags": ["javascript", "check", "palindrome", "string"],
158+
"author": "axorax"
159+
},
160+
{
161+
"title": "Count Words in a String",
162+
"description": "Counts the number of words in a string.",
163+
"code": [
164+
"function countWords(str) {",
165+
" return str.trim().split(/\\s+/).length;",
166+
"}",
167+
"",
168+
"// Example usage:",
169+
"console.log(countWords('Hello world! This is a test.')); // Output: 6"
170+
],
171+
"tags": ["string", "manipulation", "word count", "count"],
172+
"author": "axorax"
173+
},
174+
{
175+
"title": "Trim a String to a Specified Length",
176+
"description": "Trims a string to a specified length and appends '...' if it exceeds that length.",
177+
"code": [
178+
"function trimString(str, maxLength) {",
179+
" return str.length > maxLength ? str.slice(0, maxLength) + '...' : str;",
180+
"}",
181+
"",
182+
"// Example usage:",
183+
"console.log(trimString('Hello, world!', 5)); // Output: 'Hello...'"
184+
],
185+
"tags": ["string", "javascript", "trim"],
186+
"author": "axorax"
187+
},
188+
{
189+
"title": "Remove All Whitespace",
190+
"description": "Removes all whitespace from a string.",
191+
"code": [
192+
"function removeWhitespace(str) {",
193+
" return str.replace(/\\s+/g, '');",
194+
"}",
195+
"",
196+
"// Example usage:",
197+
"console.log(removeWhitespace('Hello world!')); // Output: 'Helloworld!'"
198+
],
199+
"tags": ["string", "whitespace"],
200+
"author": "axorax"
144201
}
145202
]
146203
},

0 commit comments

Comments
 (0)