<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yong Liang</title>
    <description>The latest articles on DEV Community by Yong Liang (@yongliang24).</description>
    <link>https://dev.to/yongliang24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F138176%2Fa50d86f9-fcca-4ea7-95e3-2abf1f2412c5.png</url>
      <title>DEV Community: Yong Liang</title>
      <link>https://dev.to/yongliang24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yongliang24"/>
    <language>en</language>
    <item>
      <title>JS Heaps</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Mon, 02 Sep 2019 06:54:36 +0000</pubDate>
      <link>https://dev.to/yongliang24/js-heaps-4748</link>
      <guid>https://dev.to/yongliang24/js-heaps-4748</guid>
      <description>&lt;p&gt;/* Heaps is a type of tree.&lt;/p&gt;

&lt;p&gt;Binary Heaps is similar to Binary Search Tree, but with different rules.&lt;br&gt;
Each node can have most two child nodes. No order to the left or right,&lt;br&gt;
as long as they are smaller or larger than the parent node.&lt;/p&gt;

&lt;p&gt;MaxBinaryHeaps- parent nodes are always larger than child nodes.&lt;/p&gt;

&lt;p&gt;MinBinaryHeaps- parent nodes are always smaller than child nodes.&lt;/p&gt;

&lt;p&gt;Binary Heaps evens out the left and right and the tree is always compact&lt;br&gt;
rather than one sided.&lt;/p&gt;

&lt;p&gt;*/&lt;/p&gt;

&lt;p&gt;/*Use array or list to present a heap tree&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;special rules: 
from a parent node, for any index in an array, the left child is placed in 2*index+1,
the right child is placed in 2*index+2.

to find the parent node from child nodes, use Math.floor((index -1)/2)

much like stack/queue, heap can be implemented with array
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;*/&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Coming from Javascript, Arrays in Java are confusing?</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Fri, 23 Aug 2019 05:35:52 +0000</pubDate>
      <link>https://dev.to/yongliang24/coming-from-javascript-arrays-in-java-were-confusing-17la</link>
      <guid>https://dev.to/yongliang24/coming-from-javascript-arrays-in-java-were-confusing-17la</guid>
      <description>&lt;p&gt;Coming from Javascript, arrays in Java were confusing to me. Below I would like to discuss how arrays work in Java.&lt;/p&gt;

&lt;h3&gt;
  
  
  Array Declaration
&lt;/h3&gt;

&lt;p&gt;In Java, an array has to declare with a type. Elements in it has to match the array type as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//integer array example, both declarations will work

int [] myArray; 
int myArray [];

//string array example

String [] strArray;
String strArray [];

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Give array a size
&lt;/h3&gt;

&lt;p&gt;The size of an array will not change dynamically in Java, so we have to define a size before using it. ArrayList is a better option when we need a dynamic size for a collection of datas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//using the arrays from the examples above, we can define a size for them

myArray = new int[10];

//we can also perform the declaration and size with one line of code

int [] myArray = new int[10];

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Array literal
&lt;/h3&gt;

&lt;p&gt;When we know what elements that an array will have, we can use array literal to declare an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the size of the array is determined by the number of elements were defined

int myArray = new int[]{1,2,3,4,5,6,7,8};

//in newer version of Java, we can skip the new int[]

int myArray = {1,2,3,4,5,6,7,8};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  My favorite loop to use to get the array elements
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int eachNumber : myArray){

 System.out.println(eachNumber);
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How to Write Unit Tests with JUnit 5 in Java (Eclipse)</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Sat, 17 Aug 2019 22:02:39 +0000</pubDate>
      <link>https://dev.to/yongliang24/how-to-write-unit-tests-with-junit-5-in-java-eclipse-341h</link>
      <guid>https://dev.to/yongliang24/how-to-write-unit-tests-with-junit-5-in-java-eclipse-341h</guid>
      <description>&lt;h2&gt;
  
  
  What is JUnit?
&lt;/h2&gt;

&lt;p&gt;JUnit is a Java testing framework. It can be used for both Unit Testing and UI Testing. It is a great tool and fun to use. In this article, I would like to demonstrate how to use this framework in Eclipse IDE for beginners. &lt;/p&gt;

&lt;p&gt;Junit 5 comes with Eclipse so all we need to do is add it to our project when we first creating the project. &lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;add Junit to the project.&lt;/li&gt;
&lt;li&gt;add new file -&amp;gt; create a new class file.&lt;/li&gt;
&lt;li&gt;add new file -&amp;gt; create JUnit Test file.&lt;/li&gt;
&lt;li&gt;write some methods in the class.&lt;/li&gt;
&lt;li&gt;create an object of that class inside the Junit Test file.&lt;/li&gt;
&lt;li&gt;use the object to invoke class methods to test inputs and outputs.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This class has one method which will return a string argument.
public class HelloWorld {

     public String printThis(String str){

               return str;
        }

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//The JUnit file would look like this

class HelloWord{
//instantiate a HelloWorld object called hello.
   HelloWorld hello = new HelloWorld();

//test method names tends to be descriptive 
   @Test
   void should_return_a_same__passing_string_value(){

//one way to think about constructing a test is use these 3 steps

       //given (the inputs)
       String cake = "chocolate cake";

       //when (the actions)
       String cakeTest = "chocolate cake";

       //then (the results)

       assertEquals(cakeTest, hello.printThis(cake) );
//this will pass the test if both values are equal.
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are the steps to perform a most basic unit test.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Tricky Javascript Stuff 01</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Sun, 11 Aug 2019 06:59:41 +0000</pubDate>
      <link>https://dev.to/yongliang24/tricky-javascript-stuff-01-1065</link>
      <guid>https://dev.to/yongliang24/tricky-javascript-stuff-01-1065</guid>
      <description>&lt;h3&gt;
  
  
  1. Javascript has two types of value for empty values - null and undefined. What are the differences for these two?
&lt;/h3&gt;

&lt;p&gt;Undefined -&lt;/p&gt;

&lt;p&gt;When the value of a variable is not defined, such that when we create a variable without assigning value, the variable is defined as "undefined". When we check the type with typeOf, the type is also undefined.&lt;/p&gt;

&lt;p&gt;Some ways to get undefined:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;declare a variable without giving it value&lt;/li&gt;
&lt;li&gt;a function returns an empty value&lt;/li&gt;
&lt;li&gt;not passing any value to a function's parameter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Javascript has a global variable called undefined and its value and type are shown as undefined.&lt;/p&gt;

&lt;p&gt;Null -&lt;/p&gt;

&lt;p&gt;Null indicates no value. It is a primitive type and we can assign null to variables. When we check typeOf null it returns as object, however, it's not really an object because we can not add properties to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. == vs === || is the more the better?
&lt;/h3&gt;

&lt;p&gt;The double == comparison operator will not check types, only checks values. It will return true for 8 == "8", even though 8 is a number and "8" is a string.&lt;/p&gt;

&lt;p&gt;The tripe === operator will check both type and values. Therefore, 8 === "8" will return false since their types are different.&lt;/p&gt;

&lt;p&gt;NaN is a not a number type, along with null and undefined, these three are unique types, and will not equal to other types. &lt;/p&gt;

&lt;p&gt;Nan does not even equal to itself. NaN == Nan or NaN === NaN both will return false.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://www.thatjsdude.com/interview/js2.html#nullVsUndefined"&gt;https://www.thatjsdude.com/interview/js2.html#nullVsUndefined&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Common Chrome Debug Tools Overview</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Wed, 31 Jul 2019 23:04:50 +0000</pubDate>
      <link>https://dev.to/yongliang24/common-chrome-debug-tools-overview-2m2j</link>
      <guid>https://dev.to/yongliang24/common-chrome-debug-tools-overview-2m2j</guid>
      <description>&lt;h3&gt;
  
  
  Debugger
&lt;/h3&gt;

&lt;p&gt;Debugger allows us to set break points to force the code executions at a certain point. Once the program hits the break point and stops the execution, we can then examine the javascript values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Console.log()
&lt;/h3&gt;

&lt;p&gt;console.log() method displays a message to the console. The content of the message depends on what we pass to the console.log() method. For example, passing console.log("testing") will write "testing" to the console. We can pass objects, arrays, any type of data to console.log() and check their values. &lt;/p&gt;

&lt;h3&gt;
  
  
  Console.dir()
&lt;/h3&gt;

&lt;p&gt;The console.dir() method displays all the properties of an object in a list manner and allows us to see the content of the child objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Console.table()
&lt;/h3&gt;

&lt;p&gt;The console.table() method displays datas in a table format. The first argument is mandatory, which can be an array or object. The second optional argument specifies the number of columns the table should display.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Remove Duplicates From Arrays in Javascript</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Sat, 27 Jul 2019 16:14:31 +0000</pubDate>
      <link>https://dev.to/yongliang24/remove-duplicates-from-arrays-in-javascript-354m</link>
      <guid>https://dev.to/yongliang24/remove-duplicates-from-arrays-in-javascript-354m</guid>
      <description>&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;There are several ways to remove duplicates from arrays in JS. This article will demonstrate a method using loop and indexOf() and another method using Array.from() and Set().&lt;/p&gt;

&lt;h3&gt;
  
  
  Method #1
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;create a new empty array&lt;/li&gt;
&lt;li&gt;iterate through the original array&lt;/li&gt;
&lt;li&gt;use indexOf to check whether the new array already has a same value&lt;/li&gt;
&lt;li&gt;if the value is not found in the new array, then push it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Objective: remove the duplicate values in array1
let array1 = [1,2,3,3,4,2,5,7,1]

let array2 = []

for(let eachValue of array1){
    if(array2.indexOf(eachValue) === -1){
      array2.push(eachValue)
    }
}

console.log(array2) //returns [ 1, 2, 3, 4, 5, 7 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that array.indexOf returns -1 if no value is found. In that case we will push the value into the empty array we have created because it is a unique value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method #2
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set is a javascript built-in method, which allows to store unique values.&lt;/li&gt;
&lt;li&gt;since set returns collection of unique values in an object type, we need to convert this object back to an array.&lt;/li&gt;
&lt;li&gt;Array.from() method will convert the Set() object back to an array. This creates a new, shallow copied array.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Objective: remove the duplicate values in array1

let array1 = [1,2,3,3,4,2,5,7,1]

array1 = Array.from(new Set(array1))


console.log(array1) //returns [ 1, 2, 3, 4, 5, 7 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also use the array.filter() and indexOf() to return only unique values.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Solve the Two Sum problem with Javascript</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Thu, 18 Jul 2019 12:13:26 +0000</pubDate>
      <link>https://dev.to/yongliang24/solve-the-two-sum-problem-with-javascript-4jlk</link>
      <guid>https://dev.to/yongliang24/solve-the-two-sum-problem-with-javascript-4jlk</guid>
      <description>&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;Given an array of numbers and a target number, find the sum of two numbers from the array that is equal to the target number. May not sum the same index twice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brutal Force Solution
&lt;/h3&gt;

&lt;p&gt;Use a nested for loops to check all possible sums in the array and match the target number as it literates. Time Complexity for this solution is O(n^2).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function twoSum(numArray, numTarget)
{
 const arraySize = numArray.length;

 for(let i = 0; i &amp;lt; arraySize; i++){

     for(let j= i + 1; j &amp;lt; arraySize; j++){

       if(numTarget - numArray[i] === numArray[j]){

          return `The sum of indices ${i} and ${j} equals ${numTarget}` 
        }
     }
  }
 return "target not found."
}

//calling 

twoSum([1,2,3], 5)

//returns 'The sum of indices 1 and 2 equals 5'

twoSum([1,2,3], 6) //returns "target not found"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Use key/value Pairs Solution
&lt;/h3&gt;

&lt;p&gt;Instead of thinking the sum of index1 + index2 = target, we can think of index2 = target - index1, then look for index2 in the key/value object which we will store the array values as keys, and array index as value. &lt;/p&gt;

&lt;p&gt;The lookup time in objects or hash tables is O(1) constant time, therefore, this implementation improves the time complexity to O(N).&lt;/p&gt;

&lt;p&gt;Pseudo code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create an empty object&lt;/li&gt;
&lt;li&gt;iterate the array with a for loop&lt;/li&gt;
&lt;li&gt;let num2 = target - num1
(the num2 is the number we want to lookup for in the object)&lt;/li&gt;
&lt;li&gt;on each iteration add the value/pair to the object&lt;/li&gt;
&lt;li&gt;if the num2 is in the object, we have found it, return the indexes and the sum
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function twoSum(numArray, target){

  const numObject = {} //create an empty object

  for(let eachNum in numArray){

    const otherNum = target - numArray[eachNum]

    if(otherNum in numObject){

      return `${otherNum} + ${numArray[eachNum]} = ${target}`
    }

    numObject[numArray[eachNum]] = eachNum

//adding key/value has to go after the if statement to avoid adding the same index twice.
  }

  return "target not found"
}

twoSum([1,2,3,4,5], 8) //output: '3 + 5 = 8'
twoSum([1,2,3,4,5], 10) //"target not found"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Javascript Built-in sort() method</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Fri, 12 Jul 2019 20:33:17 +0000</pubDate>
      <link>https://dev.to/yongliang24/javascript-built-in-sort-method-18ia</link>
      <guid>https://dev.to/yongliang24/javascript-built-in-sort-method-18ia</guid>
      <description>&lt;h2&gt;
  
  
  How does JS built-in sort() method work?
&lt;/h2&gt;

&lt;p&gt;the sort() method will sort arrays in place and return a sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. &lt;/p&gt;

&lt;p&gt;The sort() method accepts a compareFunction(a,b) as argument. In order to sort things propertly, we need to define what the compareFunction does.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

var array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

//the results are not sorted correctly.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The default sort() compares elements as strings, so 21 is greater than 10000 because 2 is &amp;gt; 1 for example. In order to fix this we can use the compareFunction.&lt;/p&gt;

&lt;h3&gt;
  
  
  The compareFunction
&lt;/h3&gt;

&lt;p&gt;These are the rules I found from Mozilla's documentation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If compareFunction(a, b) is less than 0, sort a to an index lower than b (i.e. a comes first).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behavior, thus, not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If compareFunction(a, b) is greater than 0, sort b to an index lower than a (i.e. b comes first).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned, then the sort order is undefined.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
   return a - b;
 });
 console.log(numbers);

// [1, 2, 3, 4, 5]


//to sort by the length of strings

function lengthCompare(a,b){
    return a.length - b.length 
}

console.log(["grapefruitPlusKiwi", "apple", "pie", "orange", "pineapple", "banananananana"].sort(lengthCompare))

//["pie", "apple", "orange", "pineapple", "banananananana", "grapefruitPlusKiwi"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort"&gt;source from mozilla&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Reverse a Given String in Javascript</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Sat, 06 Jul 2019 03:38:39 +0000</pubDate>
      <link>https://dev.to/yongliang24/how-to-reverse-a-given-string-in-javascript-alc</link>
      <guid>https://dev.to/yongliang24/how-to-reverse-a-given-string-in-javascript-alc</guid>
      <description>&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;Create a function which takes a string as argument and return a reversed version of that string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1
&lt;/h3&gt;

&lt;p&gt;1.Convert the string into an array so that we can iterate through each character. &lt;/p&gt;

&lt;p&gt;2.Iterate the array backward and push each character into a new array. We can use the built-in reverse() function or write one ourselves.&lt;/p&gt;

&lt;p&gt;3.Finally use .join() to convert the array back to a string and return it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This solution uses all javascript built-in methods

function reverseString(str){

    let strArray = str.split("") 
    //it's important not to add space on the quotation mark here.

    strArray = strArray.reverse()

    return strArray.join("") //also no space on quotation mark here
}

reverseString("amazon google") //call the function

"elgoog nozama" //the returned string
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Write our own reverse method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverseString(str){

    let revArray = [];
    let strArray = str.split("")

    // Looping from the end to the beginning
    for(let i = strArray.length; i &amp;gt;= 0; i--) {

       //push each character into the new array
        revArray.push(strArray[i]); 
    } 

    // Joining the array elements 
    return revArray.join(''); 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of using the built-in .split() to converse a string to array, We can also use the for ... of loop. This way we can iterate through each character of a string instead of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function stackReverse(str){
    let strArray = [];

    for(let eachLetter of str){
        strArray.push(eachLetter)
    }

    strArray.reverse();

    return strArray.join("")

}

stackReverse("google amazon") //call the function

"nozama elgoog" //return value
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is my finding for how to reverse a string in javascript, hope you've enjoyed it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How does Bubble Sort work?</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Thu, 27 Jun 2019 00:02:38 +0000</pubDate>
      <link>https://dev.to/yongliang24/how-does-bubble-sort-work-6g7</link>
      <guid>https://dev.to/yongliang24/how-does-bubble-sort-work-6g7</guid>
      <description>&lt;h3&gt;
  
  
  What is bubble sort?
&lt;/h3&gt;

&lt;p&gt;Bubble sort is an algorithm where the largest value will bubble up to the top of a list, continue sorting this way we will have an ascending sorted array or list. We will use nested loops to compare each current items with the rest of the items in the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understand how to swap two elements using Javascript
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//swapping function takes an array, and two indexes

 function swap2(arr, index1, index2){
     let temp = arr[index1]; //let a temp variable to hold index1's value
     arr[index1] = arr[index2]; //replace index1 with index2' value
     arr[index2] = temp;//finally replace index2 with temp, which was index1
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the ES2015 we can do swapping using this syntax&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const swap = (arr, index1, index2) =&amp;gt; {     
[arr[index1], arr[index2]] = [arr[index2], arr[index1]]
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Implement bubble sort using nested array - Javascript
&lt;/h3&gt;

&lt;p&gt;Sudo Code:&lt;br&gt;
1.iterate the array reversely&lt;br&gt;
2.create a counter variable&lt;br&gt;
3.iterate the inner loop to compare each item&lt;br&gt;
4.swap the items if the current item is greater&lt;br&gt;
5.when swap happens, set noSwap to false so that it won't break the loop&lt;br&gt;
6.when noSwap never set to false, loop breaks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function BubbleSort(arr){
    for(let i = arr.length; i &amp;gt;0; i--){ 
        let noSwap = true; 
        for(let j =0; j &amp;lt; (i-1); j++){ 
            if(arr[j] &amp;gt; arr[j+1]){ 
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j+1]= temp;
                noSwap = false;
            }
        }
        if(noSwap){break;}
    }
    return arr;
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The time complexity for bubble sort is O(n^2), making it less popular compare to merge sort and other sorting algorithms.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What are Linear and Binary search in Javascript?</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Tue, 18 Jun 2019 00:25:31 +0000</pubDate>
      <link>https://dev.to/yongliang24/what-are-linear-and-binary-search-in-javascript-3144</link>
      <guid>https://dev.to/yongliang24/what-are-linear-and-binary-search-in-javascript-3144</guid>
      <description>&lt;h3&gt;
  
  
  How do Linear Search &amp;amp; Binary Search work?
&lt;/h3&gt;

&lt;p&gt;A linear search checks each items in a collection of data one at a time in order to find the searching item. For example, when we search an item in an array, the worse case is that we have to literate through the whole array, which gives us an O(n) performance. An example of linear search would look like this in Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Linear Search 
let arr = [1,4,3,9,5,2,7,8]

//this function accepts an array and an element to be searched
function linearSearchArray(arr, targetElement){

 for(let eachItemIndex in arr){
   if(arr[eachItemIndex] === targetElement){
     return eachItemIndex;
  }
 }
 return -1;
}

linearSearchArray(arr, 9) //will return 3 because 9 has an index of 3 in the array.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Binary Search
&lt;/h3&gt;

&lt;p&gt;A binary search is a little more complicated and only works on a sorted array. Binary search in a nutshell is that we first look at the middle element in a sorted array, then compare it with the item we are searching for, if the middle element is greater than the searching item, then we know that half of the elements in the array are greater than the item we are searching for, therefore we can drop that half of the array and search only the other half. We continue this operation until we find the element we are looking for. &lt;/p&gt;

&lt;p&gt;In a worse case, as we shrink the array, eventually there will be only one element left to check, if that isn't the matching element then the element we are looking for is not in the array. Because we drop half of the array each time we check, the size of the array is divided by 2 every time we conduct a comparison. Hence the time complexity of a binary search is O(log n). It is much faster than linear search on average. Here is one way to implement a binary search in Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Binary Search 

let arr = [1,4,3,9,5,2,7,8]

//this function takes an array and an element we want to find arguments

function binarySearchArray(arr, target){

//define the positions we will work with
  let startPosition =0, endPosition = arr.length -1, midPosition = 0;

//use a while loop to keep dividing and looking for the target
 while(startPosition &amp;lt;= endPosition){

//define and update the middle position
       midPostion = Math.floor((startPosition + endPosition) /2);

//break the loop when target is equal to the middle element
//otherwise we update either the start or the end position so that half //of the array will be dropped.

       if(target == arr[midPostion]){
           console.log("target found at index:", midPostion);
           return true;
         }
        else if(arr[midPostion] &amp;lt; target){

            startPosition = midPostion +1;
            //console.log("target not yet found");
        }
        else{
            endPosition = midPostion -1;
            //console.log("target not yet found");
        }

    }

    return false;

}

//call the function
binarySearchArray(arr, 9)
//this returns target found at index: 3 true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Stack and Queue in Javascript: Use Array or LinkedList?</title>
      <dc:creator>Yong Liang</dc:creator>
      <pubDate>Mon, 10 Jun 2019 23:10:52 +0000</pubDate>
      <link>https://dev.to/yongliang24/stack-and-queue-in-javascript-548n</link>
      <guid>https://dev.to/yongliang24/stack-and-queue-in-javascript-548n</guid>
      <description>&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Stack and Queue are collection of datas, similar to arrays or lists, but follow different principles in design. Stack and Queue mainly focus on adding and removing one item at a time to a list. They both are important data structures and have different uses in our applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack:
&lt;/h3&gt;

&lt;p&gt;Stack - is a concept that creates a collection of data uses &lt;br&gt;
First-in-last-out or last-in-first-out principle. Which means that items that first added to the list will be the last one to be removed.&lt;/p&gt;

&lt;p&gt;Let's say I use array.push() 10 times to add 10 items to a new array, when I do array.pop(), the first items to pop is the last item I pushed in. This concept is very useful to implement something like undo/redo functions. when we do an undo, we undo the last action. Website histories is also a good use of a Stack because the last page we visit displays at top of the list.&lt;/p&gt;

&lt;p&gt;The most basic Stack should have a method to add and a method to remove items from a list that follows the First-in-last-out principle. In Javascript we can use push to add an item to the end of the list, and pop to remove the item from the end of the list. Another way to do it is to use unshift/shift to add/remove from the beginning of the list. &lt;/p&gt;

&lt;p&gt;Keep in mind that when we remove/add anything at the beginning of an array, all elements in it need to re-index. This does not happen when we add/remove items at the end of an array. So push/pop will outperform unshift/shift for Stack implementation. A Stack using array would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack{
    constructor(){
        stackArray =[]
    }

    push(element){
        this.stackArray.push(element)
    }

    pop(){
        if(this.stackArray.length === 0){
            return "underFlow"
        }
        this.stackArray.pop()
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In Javascript, as array's length grows, the array doubles its size under hood, automatically and dynamically. This can waste a lot of space in memory for a Stack, because we only add/remove one item at a time with Stack. Due to this, a SinglyLinkedList is more ideal to create a Stack. Optimized for space complexity and Unshift/Shift methods are constant time O(1) (same as push/pop in array) in a SinglyLinkedList.&lt;/p&gt;

&lt;h3&gt;
  
  
  Queue:
&lt;/h3&gt;

&lt;p&gt;Queue - Unlike Stack, Queue uses a concept of first-in-first-out or last-in-last-out principles. The first item added to a list is the first item to be removed when requests. Similar to how printers work, the first request sent to it is the first page to be printed. Queue is useful for anything like telephone wait line, restaurant wait line etc...&lt;/p&gt;

&lt;p&gt;In Javascript, one way to implement a Queue using array is to use push() to add items, and shift() to remove item, as this will satisfy the concept of first-in-first-out principle. However, because shift() remove the first item in an array and all indexes need to shift over, this compromises the time complexity, achieving only O(n) for removing.&lt;/p&gt;

&lt;p&gt;Fortunately, we can use a singlyLinkedList again to create a Queue. Both push() and shift() methods are constant time O(1) in singlyLinkedList. This allows our Queue to have a faster performance for adding/removing items, as well as a dynamic length list to save space in memory.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
