-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear.js
More file actions
27 lines (23 loc) · 772 Bytes
/
Copy pathlinear.js
File metadata and controls
27 lines (23 loc) · 772 Bytes
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
//Verify the inputs/problems
//Creating a function that checks for the value of the user inout in an ordered array.
//ensure all input are numbers
//Think about the problem
//The first logical step will be to search the value of every element in the array, checking if the value of any element in the array is greater than the user input.
//Write out a first version
function linearSearch(array, value){
if(!Array.isArray(array, value)){
return null;
}
for(let i=0; i< array.length; i++){
if(array[i] === value){
return i;
}else if(array[i] > value){
return -1
}
}
return -1
}
//Verify the results
console.log(linearSearch([1,2,4,6,7,8,9], 9))
//Derive the time complexity.
//O(n) => Linear