Created
April 10, 2015 03:16
-
-
Save magician11/92af987c9632f53ff658 to your computer and use it in GitHub Desktop.
A negabinary filter for AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var negabinaryApp = angular.module('negabinaryApp', []); | |
negabinaryApp.filter('negabinary', function() { | |
return function (decimal) { | |
if (isNaN(decimal)) return "not a number"; | |
var negabinary = []; | |
var base = -2; | |
var remainder; | |
while(decimal != 0) { | |
remainder = decimal % base; | |
decimal = Math.ceil(decimal / base); | |
negabinary.push(remainder >= 0 ? remainder : -remainder); | |
} | |
return negabinary.reverse().join(''); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment