From 3518ad4bb188fa7038ffec5795b9ffef387df5fa Mon Sep 17 00:00:00 2001 From: Paulson J Paul Date: Thu, 21 Jan 2021 13:30:22 +0530 Subject: [PATCH 1/2] Add Linear Search JavaScript Version --- searching/js/linear-search.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 searching/js/linear-search.js diff --git a/searching/js/linear-search.js b/searching/js/linear-search.js new file mode 100644 index 00000000..434e218f --- /dev/null +++ b/searching/js/linear-search.js @@ -0,0 +1,18 @@ +// Linear Search JavaScript Version: searches for a specific item in an array and returns the index of the item. + +function linearSearch(arr, x) { + // Loop through the array + for (let i = 0; i < arr.length; i++) { + // Checks if the item in the array = search item. If yes, return the index. If not, move to next item. + if (arr[i] === x) { + return i; + } + } + // After looping if the search item is not found, return -1. + return -1; +} + +// Log to the console and have fun! +console.log(linearSearch([2, 5, 8], 5)); +console.log(linearSearch([2, 6, 9, 15], 2)); +console.log(linearSearch([2, 3, 4, 10], 8)); From 4cde61bc7f9f320a06aa601dd5067d02aab94689 Mon Sep 17 00:00:00 2001 From: Paulson J Paul Date: Thu, 21 Jan 2021 13:33:53 +0530 Subject: [PATCH 2/2] Add JavaScript Linear Search to README --- searching/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/searching/README.md b/searching/README.md index 445b9284..b9f2ef6d 100644 --- a/searching/README.md +++ b/searching/README.md @@ -11,3 +11,7 @@ 1. [Linear Search](python/linear-search.py) 2. [Binary Search](python/binary-search.py) +### JavaScript + +1. [Linear Search](js/linear-search.js) +