Skip to content

Instantly share code, notes, and snippets.

View IsraaMoqbel's full-sized avatar
⚔️

Israa Mo IsraaMoqbel

⚔️
View GitHub Profile
@IsraaMoqbel
IsraaMoqbel / max-heapify.js
Created January 16, 2023 15:33
Building max heapify
// Recursive implementation
function heapify(arr, itemIndex, length) {
// we add 1 because we start from 0 index
let left = 2 * itemIndex + 1;
let right = left + 1;
let largest = itemIndex;
if (left < length && arr[left] >= arr[largest]) {
largest = left;
}
if (left < length && arr[right] >= arr[largest]) {
function mergeSort(arr) {
if (arr.length <= 1) {
return arr
}
const middlePoint = Math.floor(arr.length / 2);
const rightArray = arr.slice(middlePoint);
const leftArray = arr.slice(0, middlePoint);
return merge(mergeSort(leftArray), mergeSort(rightArray));
const arr = [4, 9, 10, 3, 7];
const sort = (arr) => {
// starting from the 2nd index
let i = 1;
// loop through the array
while (i < arr.length) {
// loop back to make sure previous items are sorted (previous elements are less than the current value),
// if previous element is not sorted(less) we switch
while (arr[i] < arr[i-1]) {
@IsraaMoqbel
IsraaMoqbel / getFirstUniqueItem.js
Created August 3, 2022 22:48
Get first unique item in array
function getFirstUnique(arr) {
return arr.find(e => arr.indexOf(e) === arr.lastIndexOf(e));
}
console.log(getFirstUnique([3, 1, 2, 3, 3, 3]));
@IsraaMoqbel
IsraaMoqbel / getIntegersInRange.js
Created July 26, 2022 22:27
Get integers between x & y using recursion.
/*
Write a JavaScript program to get the integers in range (x, y).
Example : range(2, 9)
Expected Output : [3, 4, 5, 6, 7, 8]
*/
function getBetween(x, y) {
if (x == y) {
return [x]
} else {
@IsraaMoqbel
IsraaMoqbel / getFactorial.js
Created July 26, 2022 22:08
Calculate the factorial of a number. In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120
function factorial(n) {
if (n === 1) {
return 1;
}
return n * factorial(n - 1)
}
console.log(factorial(4)); // 24
@IsraaMoqbel
IsraaMoqbel / sessionScheduling.js
Created July 26, 2022 21:06
Create schedule for number of sessions giving right dates and days for the new sessions schedule distributed on number of sessions per week with specific weekdays starting from specific date
//Old task in node.js aimed for scheduling sessions for trainees in a gym subscription system
// TODO create schedule for number of sessions
// giving right dates and days for the new sessions schedule
// distributed on number of sessions per week with specific weekdays
// starting from specific date
const moment = require('moment');
// TODO use moment weekday method
var weekday = new Array(7);
@IsraaMoqbel
IsraaMoqbel / getPrimeNumbersUntilMax.js
Last active July 26, 2022 21:37
Get prime numbers until max number
function getPrimes(max) {
let primesArray = [];
for (let n = 2; n < max; n++) {
let isPrime = true;
for (let i = 2; i < n*n, i != n; i++) {
if(n % i === 0) {
isPrime = false;
break;
}
}
@IsraaMoqbel
IsraaMoqbel / getUniqueArray.js
Last active August 3, 2022 22:59
Get all unique values in a JavaScript array (remove duplicates)
function getUniqueElementsinArray(arr) {
// Check if the given value is the first occurring. If not, it must be a duplicate and will be filtered
return arr.filter((e, index) => arr.indexOf(e) === index);
}
console.log(getUniqueElementsinArray([1, 1, 2, 3, 3, 3, 3])); // [1, 2, 3]
function getUniqueElementsUsingSet(arr) {
// Set only accepts unique values
return Array.from(new Set(arr));
@IsraaMoqbel
IsraaMoqbel / Fibonacci.js
Last active May 31, 2023 06:33
Fibonacci using JS generator
function* fibonacci () {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (n of fibonacci()) {
// truncate the sequence at 1000