Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Search/LinearSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* value within a list. It sequentially checks each element of the list
* for the target value until a match is found or until all the elements
* have been searched.
*
* @see https://en.wikipedia.org/wiki/Linear_search
*/
function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
const position = Search(ar, searchNum)
Expand Down
35 changes: 35 additions & 0 deletions Search/test/LinearSearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Search as linearSearch } from '../LinearSearch'

const tests = [
{
test: {
arr: [1, 2, 300, 401, 450, 504, 800, 821, 855, 900, 1002],
target: 900
},
expectedValue: 9
},
{
test: {
arr: [1, 104, 110, 4, 44, 55, 56, 78],
target: 104
},
expectedValue: 1
},
{
test: {
arr: [-4, 5, 50, 77, 821, 85, 99, 100],
target: 192
},
expectedValue: -1
}
]

describe('Linear Search', () => {
it.each(tests)(
'linearSearch($test.arr, $test.target) => $expectedValue',
({ test, expectedValue }) => {
const { arr, target } = test
expect(linearSearch(arr, target)).toBe(expectedValue)
}
)
})