Skip to content

Commit b9c0727

Browse files
committed
Added the JS code on how to perform Linear Search and also created a test file where all the test cases are being mentioned under the searching folder
1 parent 5bebc4b commit b9c0727

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

.github/dsa.jpeg

-1 Bytes
Loading

.github/logo.png

-2 Bytes
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {linearSearch} = require('./LinearSearch');
2+
3+
describe('Linear Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8];
5+
describe('When element to find is at 1st position ', () => {
6+
it('Linear search', () => {
7+
expect(linearSearch(array, 1)).toEqual(0);
8+
});
9+
});
10+
describe('When element to find is at last position ', () => {
11+
it('Linear search', () => {
12+
expect(linearSearch(array, 8)).toEqual(7);
13+
});
14+
});
15+
describe('When element to find is at random position ', () => {
16+
it('Linear search', () => {
17+
expect(linearSearch(array, 3)).toEqual(2);
18+
});
19+
});
20+
describe('When element to find is no present in array ', () => {
21+
it('Linear search', () => {
22+
expect(linearSearch(array, 10)).toEqual(null);
23+
});
24+
});
25+
})

src/_Searching_/LinearSearch/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function linearSearch(arr, val) {
2+
for (let i = 0; i < arr.length; i++) {
3+
if (arr[i] === val) return i;
4+
}
5+
return null;
6+
}
7+
// MediaSourceHandle.exp
8+
// MediaSourceHandle.expo
9+
module.exports = {
10+
linearSearch
11+
};

0 commit comments

Comments
 (0)