1- /*
2- * Linear search or sequential search is a method for finding a target
3- * value within a list. It sequentially checks each element of the list
4- * for the target value until a match is found or until all the elements
5- * have been searched.
1+ /**
2+ * Linear search or sequential search is a method for finding a target value within a list.
3+ * It sequentially checks each element of the list for the target value until a match is found
4+ * or until all the elements have been searched.
5+ * @function SearchArray
6+ * @param {number } searchNum - The number to search for in the array.
7+ * @param {number[] } ar - The array in which to search for the number.
8+ * @param {(output: string) => void } [output=(v) => console.log(v)] - Optional callback function to handle output messages.
9+ * @returns {void }
10+ *
11+ * @example
12+ * // Example usage:
13+ * const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
14+ * SearchArray(3, ar) // Output: The element was found at 3
15+ * SearchArray(4, ar) // Output: The element was found at 4
16+ * SearchArray(11, ar) // Output: The element not found
617 */
718function SearchArray ( searchNum , ar , output = ( v ) => console . log ( v ) ) {
819 const position = Search ( ar , searchNum )
@@ -13,7 +24,18 @@ function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
1324 }
1425}
1526
16- // Search “theArray” for the specified “key” value
27+ /**
28+ * Search for a key in an array using linear search.
29+ * @function Search
30+ * @param {number[] } theArray - The array to search.
31+ * @param {number } key - The key to search for in the array.
32+ * @returns {number } - The index of the key in the array if found, otherwise -1.
33+ *
34+ * @example
35+ * const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
36+ * const index1 = Search(ar, 3) // index1 will be 2
37+ * const index2 = Search(ar, 10) // index2 will be -1
38+ */
1739function Search ( theArray , key ) {
1840 for ( let n = 0 ; n < theArray . length ; n ++ ) {
1941 if ( theArray [ n ] === key ) {
@@ -24,8 +46,3 @@ function Search(theArray, key) {
2446}
2547
2648export { SearchArray , Search }
27-
28- // const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
29- // SearchArray(3, ar)
30- // SearchArray(4, ar)
31- // SearchArray(11, ar)
0 commit comments