These can be called through the int object on ruby:
Or, if you call the add_methods_to_number_prototype
method, you can call each applicable method as a property on any number.
Note: because Javascript will interpret a .
after a number as a float, you cannot call the properties directly on the number or you will get a syntax error. Instead, you can call the property like this:
The examples below will always use the prototype version, unless otherwise specified.
export default class _Integer {
constructor () {
this.even = even;
this.even_questionmark = this.even;
this.gcd = gcd;
this.gcdlcm = gcdlcm;
this.lcm = lcm;
this.next = next;
this.odd = odd;
this.odd_questionmark = this.odd;
this.pred = pred;
this.succ = this.next;
this.times = times;
this.upto = upto;
this.to_i = to_i;
}
}
function even(num) {
num = num || this;
return num % 2 === 0;
}
function gcd(number_1, number_2) {
if (isUndefined(number_1)) number_1 = this;
if (number_2) {
return gcd(number_2, number_1 % number_2);
} else {
return Math.abs(number_1);
}
}
Returns an array. The first element is the greatest common divisor and the second is the least common multiple.
function gcdlcm(number_1, number_2) {
if (isUndefined(number_1)) number_1 = this;
let greatest_common_divisor = gcd(number_1, number_2);
let least_common_multiple = lcm(number_1, number_2);
return [greatest_common_divisor, least_common_multiple];
}
function lcm(number_1, number_2) {
if (isUndefined(number_1)) number_1 = this;
let top_of_equation = Math.abs(number_1 * number_2);
let bottom_of_equation = gcd(number_1, number_2);
return top_of_equation / bottom_of_equation;
}
function next(num) {
num = num || this;
return num + 1;
}
function odd(num) {
num = num || this;
return num % 2 != 0;
}
function pred(num) {
num = num || this;
return num - 1;
}
function times(num, block) {
num = num || this;
_times(num, block);
}
function upto(original_number, upto_number, block) {
original_number = original_number || this;
for (var i = original_number; i <= upto_number; i++) {
block(i);
}
}