Compass Offset Help

So i think the issue is my very bad coding.

I'm sending an array from a magnetometer
[247,231,336]

X = 247
y = 231
z = 336

Im trying to get compass heading from X,Y using the following.

var X = msg.payload[0];
var Y = msg.payload[1];
function calcAngleDegrees(X,Y)
{
return Math.atan2(X,Y) * 180 / Math.PI;
}
msg.payload = (calcAngleDegrees(X,Y));
return msg.

The behavior is not a 360 degrees. Splitting the 360 into 3 segments of the pie segment being 33.3%
2 segments start at 0 then go to 60. then the last segment is 89 and doesn't change.

Any pointers on the code i'm using?

This is the code from honeywell
Direction

(y>0) = 90 - [arcTAN(x/y)]*180/πDirection
(y<0) = 270 - [arcTAN(x/y)]*180/πDirection
(y=0, x<0) = 180.0Direction
(y=0, x>0) = 0.0

I'm not sure how to write this in NodeRED.

Cheers guys.