TypeError: Math["floor"] is not a function

1.6k Views Asked by At

I'm learning Javascript, so this question may seem ludicrous for most JS coders. I'm reading Javascript: The good parts, and I can't make this piece of code work:

Function.prototype.method = function(name,func){
    this.prototype[name] = func;
    return this;
}
Number.method('integer', function(){
    return Math[ this <0? 'ceiling' : 'floor'](this);
});
document.writeln(Math.floor(3.4)+"");
document.writeln((-10/3).integer());

As you probably guessed it, the first document.writeln function displays "3" as it should be, but the second one displays nothing and the error is: "TypeError: Math["floor"] is not a function" althoug it is indeed a function.

I'm pretty sure this is stupid, but I don't find why it doesn't work. Thanks for your time.

Fabien

2

There are 2 best solutions below

1
On BEST ANSWER

turn 'ceiling' to 'ceil' and it run well, I tested it:

Number.method('integer', function(){
    return Math[ this <0? 'ceil' : 'floor'](this);
});
0
On

"floor" is indeed a Math function. But your code returns the "ceiling" which supposed to be "ceil"