| An exercise to create a binary search without using libraries, just vanilla js. |
Originally started out with vanilla JS using if-else statements.... but then found this solution using switch cases. I don't yet have the knowledge or the tests to prove this is faster, but from the author's explanation, it makes sense to me why it would/could be as array sizes get larger. Updated: In the link above, I misunderstood the discussion regarding switch cases and if statements. Apparently, switch is faster in most cases but significantly faster ONLY when the number of conditions is large, so it really has nothing to do with the size of the array evaluated.
The steps taken below were what got to MVP. Anything after this initial paragraph is something I thought of to make this better.
Have this working but want to write validations for confirming array has integers only.... Now, succesfully evaluating using this article about checking if objects in an array fullfill a condition and the JavaScript isInteger method.
Added functionality to display the option to find a number, only after a correct/valid array has been entered. - line 54 of itemsAreInt function, which is triggered only after array has been evaluated as valid array.
This was originally STEPS TO TAKE
- is array valid - DONE
- request input - DONE
- sort array - DONE
- find middle number - DONE
- compare middle number to input - DONE switch case starting line 62
- if middle number < input, get last half - DONE - switch case line 69
- if middle num < input, get last half - DONE - switch case line 71
- find middle num of first/last half - DONE - recursively with switch case until num is found
- compare, create new array, loop - DONE - no longer creating new array thanks to solution mentioned above; recursively eval original array in switch case lines 69-72
All you need to do to run/test this one is open example.html in your browser and follow the prompts.
Author Mark Ellis
- Initial article to refresh myself on what binary search is
- Refresh my memory on how to write to HTML
- Sort Array
- Mozilla JS Reference pages
- Switch cases over if-else
- Test whether input is actually in array before any other code runs This would be more efficient