Debugging Javascript

Posted on

You may be able to get away without debugger with Java, but not javascript. As a dynamic web scripting language, javascript creates amazing visual effects on a web page; at the same time it is easy to come up with unexpected results which makes you scratch your head and ask “how did this happen?”. It is very hard to find out the problem by just looking at the code because the values are changing in various ways that you overlooked. By saying that, knowing how to debug javascript is crucial for everyone wants to program with javascript. First of all, I want to thank the wonderful debuggers which come with the browser.

Debugging in Firefox

If a defect is found in Firefox, it is the best case it can be since you can use the most famous and powerful debugger called Firebug. With Firebug, you are able to trace down the problematic code with the least amount of time. The second icon shown on the top menu is the element selector so you can hover the element and look at the dom.

Debugging in Chrome

Chrome seems to have better performance than Firefox and it has a similar debugger – Developer Tools. There are more advanced features come with Developer Tools while there are many extensions available for Firebug. For basic debugging features, it is more of a personal preference to use Firebug or Developer Tools.


IE is very unforgiving for any tiny programming mistakes. Sadly, the debugger in IE, which happened to be called as Developer Tools also, is not as powerful as the other two but still better than without it. The best thing of it is there is a clear cache button and the worst thing is it doesn’t show most of the related javascript files. One thing you should be aware of is that even thought IE 9 provides options for previous version modes, it’s not accurate so use the real version of IE to do the testing.

Although the functionality and design for different debuggers vary, they all try to provide those basic debugging features, like console, style, and network. The first thing you should check when you start the debugging process is to check if there are some major errors shown on the console cause it may interrupt the rendering of javascript. Also check the Net/Network tab of the debugger to see if all the required files are loaded correctly in case you missed the path of the file. If there are REST calls, check the request and response for the calls in the console tab.

The most common way to debug is to use console.log and set the break points in javascript files. Console.log comes in handy but you have to define all the variables that you are interested in. Setting break points are likely to be used so you can do further investigation. Also, you can add the variables to watch list and trace down the code with the stack when you set the break points. If you need to change the styling of the web page, it’s much better to do it in debugger so you can see the result instantly. Pro JavaScript Techniques by John Resig is a reference for the usage of different debuggers.

In the end, programming is such an interesting thing that makes your patience shines as it involves thinking processes in every phase of software development from design to testing. Debugging is just a process of correcting incorrect outcome discovered in certain circumstances. It takes time to trace down the code and find out the root cause of the defect. Depends on your experience and time with the code base, you may get an idea about what you should look into first. It is why programmers have higher job satisfaction than in other fields. You just feel good during the process of making better software.

Calculate the Distance between Two Locations Using Java

Posted on

This programming question occurred to me when I was preparing the solution for an introductory programming class. I have found this programming problem very interesting and I think it is a starting stone for many different applications that requires geographic information.

It is not an hard problem but the key is to understand the conversion between angle and radian during the calculation. First, you will need to get the latitude and longitude of the two cities. In this example, I will use San Jose and Los Angeles. I got the geographic information from

San Jose: lat=37.220997, lon=-121.816406
Los Angeles: lat=33.920268, lon=-118.872070

Secondly, the formula to calculate the distance is given in the Wikipedia:

Below is the source code of the program:

public class Distance
   public static void main(String[] args)
      Scanner in = new Scanner(;

      System.out.print("Enter latitude and longitude for the first location: ");
      double x1 = Math.toRadians(in.nextDouble());
      double y1 = Math.toRadians(in.nextDouble());
      System.out.print("Enter latitude and longitude for the second location: ");
      double x2 = Math.toRadians(in.nextDouble());
      double y2 = Math.toRadians(in.nextDouble());

      double sec1 = Math.sin(x1)*Math.sin(x2);
      double dl=Math.abs(y1-y2);
      double sec2 = Math.cos(x1)* Math.cos(x2);
      //sec1,sec2,dl are in degree, need to convert to radians
      double centralAngle = Math.acos(sec1+sec2*Math.cos(dl));
      //Radius of Earth: 6378.1 kilometers
      double distance =  centralAngle * 6378.1;
      System.out.println("The distance is " + distance+" kilometers.");

The distance between San Jose and Los Angeles is 453.9025838385787 kilometers.

Pointers in C Programming

Posted on
  1. Why pointer?

    We can use the pointer variable to directly access the memory location. Pointers are passed by reference as function’s parameters so the value can be set anywhere.

  2. How to define and use a pointer variable?

    The name of a pointer variable beginnes with the “*” and a pointer variable can only be first assigned memory addresses, 0, or NULL. After assigning an address of a variable to a pointer variable, the same type of content as the variable can be assign to the pointer variable using “*” so the variable get the content and it doesn’t affect the point at all.

    #include <stdio.h>
       int myNum = 30;
        //Allocate a memory location for the pointer variable but it's not pointing to anything yet
       int *prtNum;
       // Assign the memory location of myNum as the content of the pointer variable.
       prtNum = &myNum;
       int yourNum = *prtNum;  // yourNum gets the value of the pointer, not the address.
       *prtNum = 20;            // Change the value of myNum to 20 because prtNum has the address to myNum.
       // Prints the memory address using "%p".
       printf ("prtNum's memory address is %p and data is %d.\nmyNum's  memory address is %p and data is %d.\nyourNum's memory address is %p and data is %d.", prtNum, prtNum, &myNum, myNum, &yourNum, yourNum);

    The output of the program is:

    prtNum's memory address is 0x7fff5fbff78c and data is 20.
    myNum's  memory address is 0x7fff5fbff78c and data is 20.
    yourNum's memory address is 0x7fff5fbff77c and data is 30.

    Note: Throughout this example, the memory location of the prtNum is never changed since it’s initialized. prtNum points to the address of myNum so they share the same value.

    When using prtNum directly, no “*” in front of the variable name, but with the “*” if it’s used as a media.