Skip to content

Commit

Permalink
feat(ayan-b#1): JS linear search - input
Browse files Browse the repository at this point in the history
  • Loading branch information
jarpi committed Oct 3, 2018
1 parent 54e6101 commit c9de3b3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion javascript-linear-search/javascript-linear-search-3.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
const readline = require('readline')

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

rl.question('Type the array elements, ie: 12345 ', (elements) => {
rl.question('Type the element to search ', (n) => {
const l = elements.length
const found = linearSearch(elements.split(''), n)
console.log(found > -1 ? `Element found at ${(l-found)}` : `No element found`)
rl.close();
})
})

const linearSearch = (arr, n) => {
return arr.length ? arr[0] === n || linearSearch(arr.slice(1), n) : false
return arr.length ? (arr[0] === n ? arr.length : linearSearch(arr.slice(1), n)) : -1
}

0 comments on commit c9de3b3

Please sign in to comment.