Q:

Javascript + Maths - Get a bearing angle from a point A to B properly

Asking on the internet I found a formula to find the bearing angle from point A to point B, and let's say that for now it's half working.
//Creates to points A and B and set their coordinates
var A = {
    x : 250,
    y : 250
};

var B = {
    x : 100,
    y : 250
};

var angle = Math.acos(
              (A.y - B.y) / 
              Math.sqrt(
                  Math.pow(A.x - B.x, 2) + 
                  Math.pow(A.y - B.y, 2)
              ) * (180 / Math.PI);

Fiddle here: https://jsfiddle.net/7js7e90k/4/
Right now the point B (GREEN DOT) is to the left of the point A (WHITE DOT), the angle shown should be 270°, but it shows 90°. And if you set the point B to the right (changing the object coordinates) you will see that it will also show 90°.
I need it to show the proper angle relative to the north (0°) but it wont go beyond 180°, something needs to change in the formula.

A:

Since Math.acos() returns a value between 0 and π rather than between 0 and 2π, you can check if point A is to the left or right of point B and add an extra 180˚ if A is right of B.
Try this:
var ref_angle = Math.acos((A.y - B.y) / Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2))) * (180 / Math.PI);

if (B.x >= A.x) {
    var angle = ref_angle
}
else {
    angle = ref_angle + 180
}

UPDATED:
If you want to avoid using the conditional, you will need to use Math.atan2(). However Math.atan2() measues the angle relative to the positive x-axis (east) and returns values between -180˚ and 180˚. I've modified the formula below get the angle relative to the positive y-axis (north) and to return values between 0˚ and 360˚:
var angle = ( ( ( -(Math.atan2((A.x-B.x),(A.y-B.y))*(180/Math.PI)) % 360) + 360) % 360); 

Check out this UPDATED working fiddle: https://jsfiddle.net/g7shjjxq/5/

