File tree Expand file tree Collapse file tree 5 files changed +103
-1
lines changed Expand file tree Collapse file tree 5 files changed +103
-1
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 1- //An implementation of a binary search algorithm to search for data in an array in javascript
1+ //An implementation of a binary search algorithm to search for data in an array in javascript -
22
33function binary_search ( array , user_search_string ) {
44 let low = 0 ;
Original file line number Diff line number Diff line change 1+ class Queue {
2+
3+ constructor ( ) {
4+ this . items = [ ] ;
5+ }
6+
7+ enqueue ( element ) {
8+ this . items . push ( element ) ;
9+ }
10+
11+ dequeue ( ) {
12+ return this . items . shift ( ) ;
13+ }
14+
15+ isEmpty ( ) {
16+ return this . items . length === 0 ;
17+ }
18+
19+ front ( ) {
20+ return this . items [ 0 ] ;
21+ }
22+
23+ printQueue ( ) {
24+ console . log ( this . items . toString ( ) ) ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ class Stack {
2+ items = [ ] ;
3+
4+ constructor ( ) {
5+ this . items = [ ] ;
6+ }
7+ push ( element ) {
8+ this . items . push ( element ) ;
9+ }
10+ pop ( ) {
11+ if ( this . items . length === 0 ) return "underflow, stack empty" ;
12+ return this . items . pop ( ) ;
13+ }
14+ peek ( ) {
15+ return this . items [ this . items . length - 1 ] ;
16+ }
17+ isEmpty ( ) {
18+ // return true if stack is empty
19+ return this . items . length === 0 ;
20+ }
21+
22+ printStack ( ) {
23+ var str = "" ;
24+ for ( var i = 0 ; i < this . items . length ; i ++ ) str += this . items [ i ] + " " ;
25+ return str ;
26+ }
27+ }
28+
29+ var stack = new Stack ( ) ;
30+
31+ // testing isEmpty and pop on an empty stack
32+
33+ // returns false
34+ console . log ( stack . isEmpty ( ) ) ;
35+
36+ // returns Underflow
37+ console . log ( stack . pop ( ) ) ;
38+
39+ // Adding element to the stack
40+ stack . push ( 10 ) ;
41+ stack . push ( 20 ) ;
42+ stack . push ( 30 ) ;
43+
44+ // Printing the stack element
45+ // prints [10, 20, 30]
46+ console . log ( stack . printStack ( ) ) ;
47+
48+ // returns 30
49+ console . log ( stack . peek ( ) ) ;
50+
51+ // returns 30 and remove it from stack
52+ console . log ( stack . pop ( ) ) ;
53+
54+ // returns [10, 20]
55+ console . log ( stack . printStack ( ) ) ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Get the smallest number on an array of numbers
3+ * @param {Array } n array of numbers
4+ */
5+ function getMin ( n ) {
6+ const array = Array . from ( n ) ;
7+ let min ;
8+
9+ //////
10+ for ( let i = 0 ; i < array . length ; i ++ ) {
11+ if ( min === undefined || array [ i ] < min ) {
12+ min = array [ i ]
13+ } else if ( ! array [ i ] < min ) {
14+ return 'array props are the same ' + array
15+ }
16+ }
17+ ///////
18+ return min ;
19+ }
20+
21+ console . log ( getMin ( [ 50 , 50 , 50 , 50 ] ) ) ;
You can’t perform that action at this time.
0 commit comments