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
Empty file added .eslintrc.json
Empty file.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ jspm_packages

# Optional REPL history
.node_repl_history
node_modules
node_modules




17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Table of Contents
[1- code challange class 2 array shift ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/3)
# data-structures-and-algorithms
# Challenge Summary
To write a code that take two parameter sorted array and search key return the index of the array’s element that is equal to the search key, or -1 if the element does not exist.

[2- code challange class 3 array binary search ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/2)
## Challenge Description

learning what is binary search

## Approach & Efficiency

i search about binary search then i try to understand it then i write the code

## Solution
![uml](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/blob/array-binary-search/assets/array-binary-serach.jpg)

[
10 changes: 10 additions & 0 deletions __test__/array-binary-search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const validate = require('../challanges/array-binary-search/array-binary-search.js');
describe('Validitor Module',()=>{
it('validate if the element inside the array it will return the index of that element',()=>{
expect(validate([1,2,3],3)).toEqual(2)
})
it('validate if the element is not inside the array return -1',()=>{
expect(validate([1,2,3],5)).toEqual(-1)
})
})
Binary file added assets/array-binary-serach.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions challanges/array-binary-search/array-binary-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

function binarySearch(arr, searchKey) {
let first = 0;
let end = arr.length - 1;
while (first <= end) {
let midElement = Math.floor((first + end) / 2);
if (arr[midElement] === searchKey){ return midElement }
else if (arr[midElement] < searchKey)
{ first = midElement + 1;}
else{
end = midElement - 1;};
}
return -1;
}
module.exports = binarySearch;
Loading