Nathan Firth

  • Archive
  • RSS

JavaScript Weirdness

I was just listening to the lightning talk by Gary Bernhardt from CodeMash 2012 and he brought up some really interesting (and funny) points about JavaScript.

For example:

[] + []; 

””

{} + {};

NaN

[] + {};

[object Object]

{} + []; 

0

So why is it that if you add two empty Arrays you get an empty string, but if you add two objects you get “not a number”. Array + Object is an Object but Object plus Array is 0???

    • #javascript
    • #weirdness
  • 3 weeks ago
  • 1
  • Permalink
  • Share
    Tweet

JavaScript: Clock Angle Problem

Earlier today I was presented with the problem of calculating the degrees between the hour hand and the minute hand of an analog clock. At first this seemed like a fairly complicated task, however after playing with it for a bit the solution became apparent. But now after looking up the answer on Google I feel really embarrassed on how easy the solution really is.

It’s amazing how complicated you can make a solution, when most of the time it can be solved so easily once you have the basic logic worked out.

Solution:

// h = 1..12, m = 0..59
function angle(h,m) {
hAngle = 0.5 * (h * 60 + m);
mAngle = 6 * m;
angle = Math.abs(hAngle - mAngle);
angle = Math.min(angle, 360 - angle);
return angle;
}
    • #JavaScript
    • #Math
    • #angle
    • #clock
    • #clock angle problem
    • #problem
  • 3 weeks ago
  • Permalink
  • Share
    Tweet

Socialite.js

JavaScript for implementing social sharing buttons asynchronously. Support for Twitter, Google+, Facebook and LinkedIn

    • #social
    • #sharing
    • #socialite.js
    • #socialite
    • #javascript
    • #jquery
    • #js
    • #plugin
    • #facebook
    • #twitter
    • #google+
    • #linkedin
  • 1 month ago
  • Permalink
  • Share
    Tweet

About

I'm an internet entrepreneur, web developer, designer, lover of Jesus from San Diego. Originally from Sweden. I have a beautiful wife, and two amazing kids.
Follow @nathanfirth

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco but customized by Nathan Firth.

Powered by Tumblr