forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_search.js
41 lines (37 loc) · 1.04 KB
/
linear_search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* Algorithm: Linear Search
* Author: Davide Pollicino (@omonimus1)
* Date: 25/06/2020
* Last Modified by: Davide Pollicino (@omonimus1) 26/06/2020
*
* Linear search: compare every element for the beginning to the en
* with our target (element to find). It returns true if the target * is found, false otherwise
*/
/*
* Search in a list a target using linear search algorithm
*/
function linear_search(arr, target) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == target) return "Found";
}
return "Not Found";
}
/*
* @Test : Search an element that exists in the list
* Output expected: Found
*/
function linear_search_pass_test() {
var test1 = [1, 2, 3, 4, 5, 6, 7];
console.log(linear_search(test1, 6));
}
/*
* @Test : Search an element that do not exists in the list
* Output expected: 'Not Found'
*/
function linear_search_failure_test() {
var test1 = [1, 2, 4, 5];
console.log(linear_search(test1, 9));
}
// Entry file of the js application
linear_search_pass_test();
linear_search_failure_test();