diff --git a/snippets/javascript/factorial.js b/snippets/javascript/factorial.js new file mode 100644 index 0000000..2afa03d --- /dev/null +++ b/snippets/javascript/factorial.js @@ -0,0 +1,10 @@ +function factorial(n){ + let result = 1; + for(i=2;i<=n; i++){ + result *= i; + } + return result; +} + +// Exapmle +console.log(factorial(5)); diff --git a/snippets/javascript/fibonacci.js b/snippets/javascript/fibonacci.js new file mode 100644 index 0000000..3eea0bd --- /dev/null +++ b/snippets/javascript/fibonacci.js @@ -0,0 +1,11 @@ +function fibonacci(n) { + const fib = [0, 1]; + for (let i = 2; i < n; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + } + return fib.slice(0, n); +} + +// Example +console.log(fibonacci(10)); + diff --git a/snippets/javascript/palindrome.js b/snippets/javascript/palindrome.js new file mode 100644 index 0000000..f419bf1 --- /dev/null +++ b/snippets/javascript/palindrome.js @@ -0,0 +1,14 @@ +//A palindrome is a word, phrase, number, or sequence that reads the same backward as forward +//Example: "madam", "racecar", "A man, a plan, a canal, Panama" +//This code checks if the given string is plindrome or not + +function isPalindrome(str) { + let reversed = ""; + for (let i = str.length - 1; i >= 0; i--) { + reversed += str[i]; + } + return str === reversed; +} + +console.log(isPalindrome("madam")); // true +console.log(isPalindrome("hello")); // false \ No newline at end of file diff --git a/snippets/javascript/reverse_linked_list.js b/snippets/javascript/reverse_linked_list.js new file mode 100644 index 0000000..aba7246 --- /dev/null +++ b/snippets/javascript/reverse_linked_list.js @@ -0,0 +1,25 @@ +class Node { + constructor(value) { + this.value = value; + this.next = null; + } +} + +const reverseLinkedList = head => { + let prev = null; + let current = head; + while (current) { + const next = current.next; + current.next = prev; + prev = current; + current = next; + } + return prev; +}; + +// Example +const a = new Node(1); +const b = new Node(2); +const c = new Node(3); +a.next = b; b.next = c; +console.log(reverseLinkedList(a)); // returns reversed list \ No newline at end of file diff --git a/snippets/javascript/sort.js b/snippets/javascript/sort.js new file mode 100644 index 0000000..457e9f7 --- /dev/null +++ b/snippets/javascript/sort.js @@ -0,0 +1,52 @@ +// Bubble Sort + +const bubbleSort = arr => { + for (let i = 0; i < arr.length - 1; i++) { + for (let j = 0; j < arr.length - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; + } + } + } + return arr; +}; +//Example +console.log(bubbleSort([5,3,8,4,2])); + + +//Quick sort + +const quickSort = arr => { + if (arr.length <= 1) return arr; + const pivot = arr[arr.length - 1]; + const left = arr.filter(x => x < pivot); + const right = arr.filter(x => x > pivot); + const mid = arr.filter(x => x === pivot); + return [...quickSort(left), ...mid, ...quickSort(right)]; +}; +//Example +console.log(quickSort([10,80,30,90,40,50,70])); + +// Merge Sort + +const mergeSort = arr => { + if (arr.length <= 1) return arr; + + const mid = Math.floor(arr.length / 2); + const left = mergeSort(arr.slice(0, mid)); + const right = mergeSort(arr.slice(mid)); + + return merge(left, right); +}; + +const merge = (left, right) => { + const result = []; + let i = 0, j = 0; + while (i < left.length && j < right.length) { + if (left[i] < right[j]) result.push(left[i++]); + else result.push(right[j++]); + } + return result.concat(left.slice(i)).concat(right.slice(j)); +}; +// Example +console.log(mergeSort([38,27,43,3,9,82,10]));