Last active
August 9, 2016 04:21
-
-
Save MariaSzubski/95b64237141ace25d287 to your computer and use it in GitHub Desktop.
Calculate numbers.
This file contains hidden or 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
| function parity(val){ | |
| return (val % 2 === 0) ? 'even' : 'odd'; | |
| } | |
| // Fixes negative number mod | |
| function mod(x, m){ | |
| return (x % m >= 0) ? (x % m) : (x % m + m); | |
| } | |
| function squared(num) { | |
| return num * num; | |
| } | |
| function percent(ofNum, is) { | |
| return (is / ofNum) * 100 + '%'; | |
| } | |
| function areaCircle(rad) { | |
| var val = squared(rad) * Math.PI; | |
| return Math.round(val * 100) / 100; | |
| } | |
| function binary(val){ | |
| // Convert result of convert() to an integer and reverse the digits | |
| return parseInt(convert(val).split("").reverse().join("")); | |
| // Find binary digits | |
| function convert(val){ | |
| // Use modulo division to find each digit & convert it to a string | |
| var digit = (val % 2).toString(); | |
| // Recursive division | |
| return (val/2 < 1) ? ((val % 2).toString()) : (digit + convert(Math.floor(val/2))); | |
| } | |
| } | |
| // Convert the result to an array of integers | |
| var binArr = binary(n).split("").reverse().map(x => parseInt(x, 10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment