From c23ca90b8301bd7eefabda5681250b515ea714e0 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 28 Mar 2019 23:52:08 +0200 Subject: [PATCH] update binarySearch function for es6 recursive (#101) --- .../ES6/02_recursive_binary_search.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js b/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js index d42af05..9fb9e96 100644 --- a/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js +++ b/01_introduction_to_algorithms/ES6/02_recursive_binary_search.js @@ -6,7 +6,7 @@ * @param {number} high Highest limit of search in the list * @return {(number | null)} Number if the value is found or NULL otherwise */ -const binarySearch = ( list, item, low, high ) => { +const binarySearch = ( list, item, low = 0, high = list.length - 1 ) => { let arrLength = list.length; while ( low <= high ) { let mid = Math.floor((low + high) / 2); @@ -36,8 +36,6 @@ const binarySearch = ( list, item, low, high ) => { const createArr = ( n ) => Array.from({length: n}, (v, k) => k + 1); const myList = createArr( 100 ); -let low = 0; -let high = myList.length - 1; -console.log( binarySearch( myList, 3, low, high ) ); // 2 -console.log( binarySearch( myList, -1, low, high ) ); // null +console.log( binarySearch( myList, 3 ) ); // 2 +console.log( binarySearch( myList, -1 ) ); // null