Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: loiane/javascript-datastructures-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: tonyjemba/javascript-datastructures-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Nov 12, 2021

  1. Added Array class

    tonyjemba committed Nov 12, 2021
    Copy the full SHA
    7d3a798 View commit details
Showing with 60 additions and 0 deletions.
  1. +60 −0 src/js/data-structures/models/array.js
60 changes: 60 additions & 0 deletions src/js/data-structures/models/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export default class MadeArray{
constructor(){
//initiates MadeArray object with an empty object with length 0
this.length = 0;
this.elements = {};
}
/**
*
* @param {number} index -index of the element to return
* @returns {any}
*/
getElement(index){
return this.elements[index];
}
/**
*
* @param {any} element -element to push at the end of the array
* @returns {number}
*/
push(element){
this.elements[this.length] = element;
this.length++;
return this.length;
}
/**
*
* @returns {any} - last element that is removed from the array
*/
pop(){
const element_to_pop = this.elements[this.length - 1];
delete this.elements[this.length - 1];
this.length--;
return element_to_pop;
}
/**
*
* @param {number} index -index of the element to delete
* @returns {any} -element that has been deleted
*/
delete(index){
const element_to_delete = this.elements[index];
this.shiftElements(index);
return element_to_delete;
}
/**
*
* @param {number} index -index of the element that we should start shifting from
*/
shiftElements(index){
//element in the position on index is overwrited and deleted
for(let x = index; x < this.length-1; x++){
this.elements[x] = this.elements[x+1];
}
//delete the last element not to appear two times
delete this.elements[this.length - 1];

this.length--;
}
}