diff --git a/Data_Structures/Arrays/Searching/README.md b/Data_Structures/Arrays/Searching/README.md index 225e99c..2e55457 100644 --- a/Data_Structures/Arrays/Searching/README.md +++ b/Data_Structures/Arrays/Searching/README.md @@ -1,17 +1,28 @@ ![](http://i.imgur.com/BgUMUGU.png) # Searching -This MD file serves as example/template. It will be filled up soon. - -(for Bucky to fill up) +In order to iterate an array more efficiently sometimes we have to search for the value instead of the place. The function **indexof()** helps us with that # Course Documentation -(this for me to fill up with every element that you use in lessons. syntax explaination and links for more) -## Element to explain +## .indexof() +In our example: + + + let items = [40, 68, 58, 12, 80, 37, 13, 63, 42, 3, 58]; + console.log(items.indexOf(12)); // 3 + console.log(items.indexOf(99)); // -1 (not found) + console.log(items.indexOf(58)); // 2 + console.log(items.lastIndexOf(58)); // 10 + + +it is pretty forward that we use the function **.indexOF()** to find a specific value that is stored in our array. +For finding the value "a" in the array "anarray" we are using the following syntax: + + anarray.indexOF("a") -(for example console.log) + ##Javascript functions @@ -125,4 +136,5 @@ stack in order to remove things from it. - [Twitter](https://twitter.com/bucky_roberts) - [Google+](https://plus.google.com/+BuckyRoberts) - [reddit](https://www.reddit.com/r/thenewboston/) + > Written with [StackEdit](https://stackedit.io/). \ No newline at end of file diff --git a/Data_Structures/Arrays/Sorting/README.md b/Data_Structures/Arrays/Sorting/README.md index 96465a2..89626e8 100644 --- a/Data_Structures/Arrays/Sorting/README.md +++ b/Data_Structures/Arrays/Sorting/README.md @@ -1,17 +1,44 @@ ![](http://i.imgur.com/BgUMUGU.png) # Sorting -This MD file serves as example/template. It will be filled up soon. +Sometimes we need to sort the stored values of our array. In order to do that we usually use the .sort() function: + -(for Bucky to fill up) + console.log(items.sort()); # Course Documentation -(this for me to fill up with every element that you use in lessons. syntax explaination and links for more) +## .sort() + +According to [w3schools](http://www.w3schools.com/jsref/jsref_sort.asp): + + "The sort() method sorts the items of an array. +The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). +By default, the sort() method sorts the values as strings in alphabetical and ascending order. +This works well for strings ("Apple" comes before "Banana"). **However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".** +Because of this, the sort() method will produce an incorrect result when sorting numbers. +You can fix this by providing a "compare function" (See "Parameter Values" below)." + + let items = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5]; + console.log(items.reverse()); + + // not what we wanted, because it sorts the numbers as if they are all strings + console.log(items.sort()); + +###compareFunction -## Element to explain +Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like: -(for example console.log) + function(a, b){return a-b} + +When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. + + items.sort(function (a, b) { + return a - b; + }); + console.log(items); + +[Read more about sort() function on W3schools](http://www.w3schools.com/jsref/jsref_sort.asp). ##Javascript functions @@ -124,4 +151,6 @@ stack in order to remove things from it. - [Twitter](https://twitter.com/bucky_roberts) - [Google+](https://plus.google.com/+BuckyRoberts) - [reddit](https://www.reddit.com/r/thenewboston/) + + > Written with [StackEdit](https://stackedit.io/). \ No newline at end of file