Skip to content

Commit fd8d475

Browse files
Merge pull request #170 from JanluOfficial/main
[Snippet] Added code snippet for Greatest Common Divisor in JS
2 parents 11625c1 + 65140cc commit fd8d475

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

public/consolidated/javascript.json

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@
8585
"description": "Converts RGB color values to hexadecimal color code.",
8686
"author": "jjcantu",
8787
"tags": [
88-
"javascript",
8988
"color",
90-
"conversion",
91-
"utility"
89+
"conversion"
9290
],
9391
"contributors": [],
9492
"code": "function rgbToHex(r, g, b) {\n const toHex = (n) => {\n const hex = n.toString(16);\n return hex.length === 1 ? '0' + hex : hex;\n };\n \n return '#' + toHex(r) + toHex(g) + toHex(b);\n}\n\n// Usage:\nconsole.log(rgbToHex(255, 128, 0)); // Output: \"#ff8000\"\nconsole.log(rgbToHex(0, 255, 0)); // Output: \"#00ff00\"\n"
@@ -366,6 +364,22 @@
366364
}
367365
]
368366
},
367+
{
368+
"categoryName": "Mathematical Functions",
369+
"snippets": [
370+
{
371+
"title": "Greatest Common Divisor",
372+
"description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.",
373+
"author": "JanluOfficial",
374+
"tags": [
375+
"math",
376+
"division"
377+
],
378+
"contributors": [],
379+
"code": "function gcd(a, b) {\n while (b !== 0) {\n let temp = b;\n b = a % b;\n a = temp;\n }\n return a;\n}\n\n// Usage:\ngcd(1920, 1080); // Returns: 120\ngcd(1920, 1200); // Returns: 240\ngcd(5,12); // Returns: 1\n"
380+
}
381+
]
382+
},
369383
{
370384
"categoryName": "Number Formatting",
371385
"snippets": [
@@ -407,10 +421,8 @@
407421
"description": "Converts bytes into human-readable file size format.",
408422
"author": "jjcantu",
409423
"tags": [
410-
"javascript",
411424
"format",
412-
"size",
413-
"utility"
425+
"size"
414426
],
415427
"contributors": [],
416428
"code": "function formatFileSize(bytes) {\n if (bytes === 0) return '0 Bytes';\n \n const k = 1024;\n const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n \n return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];\n}\n\n// Usage:\nconsole.log(formatFileSize(1234)); // Output: \"1.21 KB\"\nconsole.log(formatFileSize(1234567)); // Output: \"1.18 MB\"\n"
@@ -506,13 +518,11 @@
506518
"description": "Creates a deep copy of an object or array without reference.",
507519
"author": "jjcantu",
508520
"tags": [
509-
"javascript",
510521
"object",
511-
"clone",
512-
"utility"
522+
"clone"
513523
],
514524
"contributors": [],
515-
"code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] }\n"
525+
"code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: 'original' but cloned\n"
516526
},
517527
{
518528
"title": "Filter Object",
@@ -758,9 +768,9 @@
758768
"description": "Generates a UUID (v4) string.",
759769
"author": "jjcantu",
760770
"tags": [
761-
"javascript",
762771
"uuid",
763-
"utility"
772+
"generate",
773+
"string"
764774
],
765775
"contributors": [],
766776
"code": "function generateUUID() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n const r = Math.random() * 16 | 0;\n const v = c === 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\n// Usage:\nconsole.log(generateUUID()); // Output: \"a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5\"\n"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Greatest Common Divisor
3+
description: Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.
4+
author: JanluOfficial
5+
tags: math,division
6+
---
7+
8+
```js
9+
function gcd(a, b) {
10+
while (b !== 0) {
11+
let temp = b;
12+
b = a % b;
13+
a = temp;
14+
}
15+
return a;
16+
}
17+
18+
// Usage:
19+
gcd(1920, 1080); // Returns: 120
20+
gcd(1920, 1200); // Returns: 240
21+
gcd(5,12); // Returns: 1
22+
```

0 commit comments

Comments
 (0)