PDA

View Full Version : Is a number whole? and Is a number a multiple of x?



techno_race
08-20-2008, 03:18 AM
Here are a couple functions I keep using to validate numbers.
Is a number whole?

function isWhole(x)
{
var number = x;
var rounded = Math.round(number);
if (number == rounded)
{
//What do do if the number is whole
}
else
{
//What to do if the number is not whole
//or the number is not a number
}
}
Just replace the commented lines with useful code.
I find it extremely useful to use something like return "The number was whole.";
This way, you can use <? echo 'var input = '.$_GET['input'].';'; ?>
var result = isMultiple(input);
To call this function, use:

isMultiple(number)
Is a number a multiple of x?
This is a little trickier.

function isMultiple(y,z)
{
var dividee = y;
var divider = z;
var divided = dividee / divider;
var dividem = Math.round(divided);
if (divided == dividem)
{
//What do do if the number is a multiple
}
else
{
//What to do if the number is not a multiple
//or the number is not a number
}
}
Do the same with the commented code.
To call this one, use:

isMultiple(number,is_a_multiple_of_?)
(is_a_multiple_of_? should be the number to check if number is a multiple of.)

mburt
08-20-2008, 03:25 AM
For the whole thing, I'm assuming this is easier:


Number.prototype.whole = function() { return this.toString().indexOf(".") > -1 ? false : true; }

Usage:


var num = 4.5294;
if (num.whole()) {
//do something
} else {
//do something else
}

mburt
08-20-2008, 03:29 AM
In addition:


Number.prototype.multipleOf = function(x) { return (x / this).whole() ? true : false; }

mburt
08-20-2008, 03:31 AM
Update:

Number.prototype.multipleOf = function(x) { return (x/this).whole() ? (arguments[1] ? (x/this) : true) : false; }

Optional flag, which returns the remainder.

techno_race
08-20-2008, 03:33 AM
Who said it wasn't?
Mine would be easier for beginner coders to understand.
Which is pretty much the point.
Go figure.

mburt
08-20-2008, 03:34 AM
Once again updated, I forgot to include the arguments[1] for the false:


Number.prototype.multipleOf = function(x) { return arguments[1] ? this/x : ((this/x).whole() ? true : false); }

mburt
08-20-2008, 03:35 AM
Well the readers don't need to know what's going on, I just think:


(x).whole();

and


(x).multipleOf(y);

are easy to use.

mburt
08-20-2008, 03:40 AM
Oops... I had my variables mixed up too:


Number.prototype.multipleOf = function(x) { return arguments[1] ? x/this : ((x/this).whole() ? true : false); }

Twey
08-20-2008, 04:43 AM
Number.prototype.isMultipleOf(x) {
return !(this % x);
};

Number.prototype.isWhole() {
return this.isMultipleOf(1);
};... of course actually bothering to write functions for these starts to seem a little silly...

mburt
08-20-2008, 01:53 PM
Lol, I KNEW you would come along and do that (another way of writing the function). But I see what you're saying, they are really simple.

Nile
08-29-2008, 01:07 AM
Thought I'd just add a factorial in :):

<script type="text/javascript">
Number.prototype.Fact = function(){
var i = 1;
for(x=1;x<this;x++){
i += x * i;
}
return i;
};
var num = 5;
alert(num.Fact());
</script>

In a few seconds, mburt is gonna post saying "I've got a shorter version Nile.", and then twey is gonna be like "Mine is only 2 lines though."

mburt
08-29-2008, 01:50 AM
Number.prototype.Fact = function() {
for (i = 1, x = 1;i < this;i++) {
x += i * x;
}
return x;
};

Haha... Only one line (I think) in difference.

Nile
08-29-2008, 11:52 AM
Twey's turn. Lol, that's basically the same thing as mine. :)

Twey
08-29-2008, 03:38 PM
Quick 'n' dirty:
Number.prototype.fact = function() {
for (var i = 1, r = 1; i <= this; r *= i++);
return r;
};One-liner:
Number.prototype.fact = function() { for (var i = 1, r = 1; i <= this; r *= i++); return r; };
Safe:
Number.prototype.fact = function() {
if (this < 0) return NaN;
for (var i = 1, r = 1; i <= this; r *= i++);
return r;
};
Using my toolkit:
var fact = (function(F, O) {
return F.compose(F.curry(F.reduce)(O.multiply), F.curry(F.range)(1));
})(Functional. Functional.Operator);Without shortcuts:
var fact = Functional.compose(Functional.curry(Functional.reduce)(Functional.Operator.multiply), Functional.curry(Functional.range)(1));In Python:
import operator
def fact(x):
return reduce(operator.mul, range(1, x + 1))In Haskell:
fact = product . (enumFromTo 1)Any more questions? :p

mburt
08-29-2008, 06:17 PM
Wow... I like that first function. I never think of using loops like that. But I guess you just look at it like this:


for (variables;condition(s);assigment)

Is your toolkit on the web anywhere Twey?

Twey
08-29-2008, 07:04 PM
Not yet. You can do most of it with Functional Javascript (http://osteele.com/sources/javascript/functional/).

You can do anything you like within a for loop. If I'd wanted to, I could have written it like this:
Number.prototype.fact = function() {
for (var i = 0, r = 1; r *= ++i, i < this; );
return r;
};A for loop is divided into three sections. They're commonly labelled 'initialisation', 'condition', and 'incrementation' because that's what they generally do, but those labels are stylistic: you can put anything you like in there. Pragmatically speaking, they could be given the better (but admittedly less stylish) titles of 'code that runs once when the loop is first encountered', 'code that runs before each iteration of the loop', and 'code that runs after each iteration of the loop'. The first two have some special properties: the 'initialisation' section allows 'var' statements, and the 'condition section' can terminate the loop if its return value is falsy. The 'incrementation' section is free-form.

This said, the 'initialisation', 'condition', 'incrementation' style is usually the least messy. There are some handy variants, though, like:
for (var i = 0, e; e = a[i++]; ) // leaves 'e' bound to the current value; terminates if it hits a falsy element.

mburt
08-29-2008, 08:20 PM
Thanks for the lesson on for-loops haha :D. I think I understand it a bit better now.

Nile
08-31-2008, 02:59 AM
@Twey, I almost came up with your first solution before you posted, infact I had the same thing except I had one thing wrong, don't remember what it was.