Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made Number.prototype.[chr,even,odd] getters. Ref #4

This makes a lot more sense to me. Any opinions?
  • Loading branch information...
commit ede890a31eb1cad52d8f3bcd30e5c0afa8cc60e3 1 parent b7d785a
Oliver Morgan authored
Showing with 35 additions and 6 deletions.
  1. +18 −6 lib/number.js
  2. +17 −0 test/test.Number.js
View
24 lib/number.js
@@ -1,6 +1,24 @@
Object.defineProperties(Number.prototype, {
+ chr: { get: function() {
+
+ return String.fromCharCode(this);
+
+ }, enumerable: false, configurable: true },
+
+ odd: { get: function() {
+
+ return (this % 3) == 0;

Should be "return (this % 2) == 1;"

Otherwise this is going to be incorrect on any even multiple of 3 (ex. 6, 12, 18, etc.) and any odd non-multiple of 3 (ex. 1, 5, 7, 11, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ }, enumerable: false, configurable: true },
+
+ even: { get: function() {
+
+ return (this % 2) == 0;
+
+ }, enumerable: false, configurable: true },
+
gcd: { value: function(num /*, num2, ... */) {
return Object.values(arguments).reduce(function(a, b) {
@@ -55,12 +73,6 @@ Object.defineProperties(Number.prototype, {
}, writable: true, enumerable: false, configurable: true },
- chr: { value: function() {
-
- return String.fromCharCode(this);
-
- }, writable: true, enumerable: false, configurable: true },
-
abs: { value: function() {
return Math.abs(this);
View
17 test/test.Number.js
@@ -16,6 +16,23 @@ module.exports = [
result((1.5).round(), 2, 'API Preservation');
result((1).round(1).toString(), '1.0', 'Normal');
result((15).round(-1), '20', 'Normal');
+ },
+
+ function CHR(result) {
+
+ result((72).chr, 'H', 'Getter');
+ },
+
+ function Even(result) {
+
+ result((3).even, false, 'Odd');
+ result((2).even, true, 'Even');
+ },
+
+ function Odd(result) {
+
+ result((3).odd, true, 'Odd');
+ result((2).odd, false, 'Even');
}
];

3 comments on commit ede890a

@LantisEscudo

Should be "return (this % 2) == 1;"

Otherwise this is going to be incorrect on any even multiple of 3 (ex. 6, 12, 18, etc.) and any odd non-multiple of 3 (ex. 1, 5, 7, 11, etc.).

@tomcss

That is pretty bad.

Your 'odd' function will only check to see whether a number is divisible by 3, not whether a number is odd. It will return true for 6 or 12 which are even numbers, and false for 5 and 7 which are odd numbers.

The correct approach is either:

return (this % 2) == 1;

or

return (!even(this));

@ollym
Owner

I fixed this last night - I had never even needed to create an odd function before so did it half asleep and not really thinking! ha

Didn't take long to find though - just return ! this.even;

@tomcss
Please sign in to comment.
Something went wrong with that request. Please try again.