diff --git a/js/dius00_dataStructure_Tree.js b/js/dius00_dataStructure_Tree.js new file mode 100644 index 00000000..bcc7412c --- /dev/null +++ b/js/dius00_dataStructure_Tree.js @@ -0,0 +1,89 @@ + // Javascript ES6 syntax code sample for Tree (non-binary) data structure + +/* +* Creates the Tree +* if a value is given to the constructor +* the element will be initialized as an instance of Tree +*/ +class Tree { + constructor(value) { + if(value !== undefined){ + this.value = value; + this.children = []; + } + } + +/* +* Method to add a new child to the Tree +*/ + addChild(value) { + const newTree = new Tree(value); // Generates a new Tree element + this.children.push(newTree); // Adds the newTree (child) to the children Array + return newTree; // Returns the new Tree + } + +/* +* Method to check if a value exists in the tree +*/ + contains(value) { + // initializes the result as false + let result = false; + + /* + * declare a searchValue function that travels recursivly + * through the Tree and his Childre + */ + function searchValue(tree) { + // if the current Tree value is equal to the one we are looking for + // change the result to true + if (tree.value === value) result = true; + // if the Tree has children + if (tree.children !== []) { + // for each of them call an instance of the searchValue function + for (const child of tree.children) searchValue(child); + } + } + // call the searchValue function on or parent Tree + searchValue(this); + + // returns the result + return result; + } + + /* + * Method to remove and + * return all the (children) Trees if it they a certain value + */ + remove(value) { + // initializes the array that will contain all the removed trees + const removedTrees = []; + /* + * declare a removeValue function that travels recursivly + * through the Tree and his Children, if it finds a child + * with the matching value, it pushed it to removedTrees + * and removes it from the list of children + */ + function removeValue(tree) { + // if a tree has children + if (tree.children !== []) { + // iterate through the children + for (const child of tree.children) { + // if the child(Tree) has a matching value + if (child.value === value) { + // pushes the child(Tree) into removedTrees + removedTrees.push(child); + // removes the child(Tree) from the parent children array + tree.children.splice(tree.children.indexOf(removedValue), 1); + } + } + // afterwards iterates through the remaining children + // by calling removeValue recursivly + for (const child of tree.children) removeValue(child); + } + } + // calls removeValue + removeValue(this); + // returns the removed value + return removedValue; + } +} \ No newline at end of file diff --git a/js/dius00_dataStructure_circularLinkedList.js b/js/dius00_dataStructure_circularLinkedList.js new file mode 100644 index 00000000..df4d07f8 --- /dev/null +++ b/js/dius00_dataStructure_circularLinkedList.js @@ -0,0 +1,56 @@ +// Javascript ES6 syntax code sample for Circular Linked List + + +/* +* Creates the Linked List +* if a value is given to the constructor +* the element will be initialized as a Node (below) +*/ +class CircularLinkedList { + constructor(value){ + if(value !== undefined){ + this.sentinel = new Node(value); // a "sentinel" node has to be set in order control Read/Write operation + } + } +/* +* Method to add a new Node to the list +*/ + addNode(value){ + const newNode = new Node(value); // Generates a new Node element + if(!this.sentinel) { // if there is no head make the new Node the head of the list + this.sentinel = newNode; + this.sentinel.next = newNode; + this.sentinel.prev = newNode; + return; + } else { // if there is a head, link the new Node to the tail of the list + newNode.next = this.sentinel; + newNode.prev = this.sentinel.prev.prev; + this.sentinel.prev.next = newNode; + this.sentinel.prev = newNode; + } + } + +/* +* Method to find a specific Node in the list +*/ + + findNode(value) { + if(this.sentinel){ + let iterator = this.sentinel; + while(true) { + if (iterator.value === value) return iterator; + iterator = iterator.next; + if(iterator === this.sentinel) break; + } + } + return null; + } +} + +class Node { + constructor(value){ + this.value = value; + this.next = null; + this.prev = null; + } +} diff --git a/js/dius00_leapYear.js b/js/dius00_leapYear.js new file mode 100644 index 00000000..ad946629 --- /dev/null +++ b/js/dius00_leapYear.js @@ -0,0 +1,35 @@ +// A year is a Leap Year + +// on every year that is evenly divisible by 4 +// except every year that is evenly divisible by 100 +// unless the year is also evenly divisible by 400 + + +function isLeap (num) { + // if a number ends with X00 it is divible by 100 + if(num.toString().substring(2,) == '00'){ + // if a number is divisible by 100 and 400 it is a leap year and return true + if(num % 400 == 0) return true; + // if a number is divisible by 100 and not by 400 it is not a leap year and return false + else return false; + } + // if a number is not divisible by 100 + else{ + // if it is divisible by 4 it is a leap year and return false + if(num%4 == 0) return true; + // if it is not divisible by 4 it is not a leap year + else return false; + } +}; + +// examples + +console.log(isLeap(2100)) // ==> false + +console.log(isLeap(2000)) // ==> true + +console.log(isLeap(2020)) // ==> true + +console.log(isLeap(1975)) // ==> false + +console.log(isLeap(312)) // ==> true