Skip to content

markellisdev/cspace-binary-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binary Search Exercise

An exercise to create a binary search without using libraries, just vanilla js.

Thoughts/Solution Process

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.

Edited original list here to actual STEPS TAKEN

This was originally STEPS TO TAKE

  1. is array valid - DONE
  2. request input - DONE
  3. sort array - DONE
  4. find middle number - DONE
  5. compare middle number to input - DONE switch case starting line 62
  6. if middle number < input, get last half - DONE - switch case line 69
  7. if middle num < input, get last half - DONE - switch case line 71
  8. find middle num of first/last half - DONE - recursively with switch case until num is found
  9. 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

Instructions

All you need to do to run/test this one is open example.html in your browser and follow the prompts.

References Used

Not used but perhaps should

About

Binary search challenge

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published