|
563 | 563 | "utility" |
564 | 564 | ], |
565 | 565 | "author": "realvishalrana" |
| 566 | + }, |
| 567 | + { |
| 568 | + "title": "Check if String is a Palindrome", |
| 569 | + "description": "Checks whether a given string is a palindrome.", |
| 570 | + "code": [ |
| 571 | + "function isPalindrome(str) {", |
| 572 | + " const cleanStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();", |
| 573 | + " return cleanStr === cleanStr.split('').reverse().join('');", |
| 574 | + "}", |
| 575 | + "", |
| 576 | + "// Example usage:", |
| 577 | + "console.log(isPalindrome('A man, a plan, a canal, Panama')); // Output: true" |
| 578 | + ], |
| 579 | + "tags": [ |
| 580 | + "javascript", |
| 581 | + "check", |
| 582 | + "palindrome", |
| 583 | + "string" |
| 584 | + ], |
| 585 | + "author": "axorax" |
| 586 | + }, |
| 587 | + { |
| 588 | + "title": "Count Words in a String", |
| 589 | + "description": "Counts the number of words in a string.", |
| 590 | + "code": [ |
| 591 | + "function countWords(str) {", |
| 592 | + " return str.trim().split(/\\s+/).length;", |
| 593 | + "}", |
| 594 | + "", |
| 595 | + "// Example usage:", |
| 596 | + "console.log(countWords('Hello world! This is a test.')); // Output: 6" |
| 597 | + ], |
| 598 | + "tags": [ |
| 599 | + "string", |
| 600 | + "manipulation", |
| 601 | + "word count", |
| 602 | + "count" |
| 603 | + ], |
| 604 | + "author": "axorax" |
| 605 | + }, |
| 606 | + { |
| 607 | + "title": "Remove All Whitespace", |
| 608 | + "description": "Removes all whitespace from a string.", |
| 609 | + "code": [ |
| 610 | + "function removeWhitespace(str) {", |
| 611 | + " return str.replace(/\\s+/g, '');", |
| 612 | + "}", |
| 613 | + "", |
| 614 | + "// Example usage:", |
| 615 | + "console.log(removeWhitespace('Hello world!')); // Output: 'Helloworld!'" |
| 616 | + ], |
| 617 | + "tags": [ |
| 618 | + "string", |
| 619 | + "whitespace" |
| 620 | + ], |
| 621 | + "author": "axorax" |
566 | 622 | } |
567 | 623 | ] |
568 | 624 | }, |
|
820 | 876 | "performance" |
821 | 877 | ], |
822 | 878 | "author": "dostonnabotov" |
| 879 | + }, |
| 880 | + { |
| 881 | + "title": "Get Contrast Color", |
| 882 | + "description": "Returns either black or white text color based on the brightness of the provided hex color.", |
| 883 | + "code": [ |
| 884 | + "const getContrastColor = (hexColor) => {", |
| 885 | + " // Expand short hex color to full format", |
| 886 | + " if (hexColor.length === 4) {", |
| 887 | + " hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;", |
| 888 | + " }", |
| 889 | + " const r = parseInt(hexColor.slice(1, 3), 16);", |
| 890 | + " const g = parseInt(hexColor.slice(3, 5), 16);", |
| 891 | + " const b = parseInt(hexColor.slice(5, 7), 16);", |
| 892 | + " const brightness = (r * 299 + g * 587 + b * 114) / 1000;", |
| 893 | + " return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";", |
| 894 | + "};", |
| 895 | + "", |
| 896 | + "// Usage:", |
| 897 | + "console.log(getContrastColor('#fff')); // Output: #000000 (black)", |
| 898 | + "console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)", |
| 899 | + "console.log(getContrastColor('#ff6347')); // Output: #000000 (black)", |
| 900 | + "console.log(getContrastColor('#f4f')); // Output: #000000 (black)" |
| 901 | + ], |
| 902 | + "tags": [ |
| 903 | + "color", |
| 904 | + "hex", |
| 905 | + "contrast", |
| 906 | + "brightness", |
| 907 | + "utility" |
| 908 | + ], |
| 909 | + "author": "yaya12085" |
823 | 910 | } |
824 | 911 | ] |
825 | 912 | }, |
|
0 commit comments