Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/javascript/factorial.js
Original file line number Diff line number Diff line change
@@ -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));
11 changes: 11 additions & 0 deletions snippets/javascript/fibonacci.js
Original file line number Diff line number Diff line change
@@ -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));

14 changes: 14 additions & 0 deletions snippets/javascript/palindrome.js
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions snippets/javascript/reverse_linked_list.js
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions snippets/javascript/sort.js
Original file line number Diff line number Diff line change
@@ -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]));