-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchSortedArray.js
More file actions
27 lines (22 loc) · 850 Bytes
/
searchSortedArray.js
File metadata and controls
27 lines (22 loc) · 850 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
/*
Given a sorted array of integers, write a function called search, that accepts a value and returns the index where the value passed to the function is located. If the value is not found, return -1
EXAMPLES
search([1,2,3,4,5,6],4) // 3
search([1,2,3,4,5,6],6) // 5
search([1,2,3,4,5,6],11) // -1
*/
function searchArr(arr, num) {
if (Array.isArray(arr) && typeof num === 'number') {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === num) {
return i; // return index where the num is located
}
}
return -1; // if the value is not found in the array
} else {
return -1; // for invalid inputs
}
}
console.log(searchArr([1, 2, 3, 4, 5, 6], 6)); // Expected output: 5
//input "num"..I'm looking for where this "num" is located in array.
//output: index of "num"