简体   繁体   English

TypeError:Math.floor()不是函数

[英]TypeError: Math.floor() is not a function

I have a Node.js Discord Bot, written in discord.js, and I want to make a turn based fighting system, so I made a damage calculation function. 我有一个用discord.js编写的Node.js Discord Bot,我想制作一个基于回合的战斗系统,所以我做了一个伤害计算功能。

var damage = parseFloat( Math.floor( Math.random() * skill.dmg/5 ) + skill.dmg )
//some other factors, none causing the error
damage = Math.floor( damage )

the code is quite simple, but it's erroring with a 代码很简单,但是它的错误

TypeError: Math.floor(...) is not a function TypeError:Math.floor(...)不是函数

I've checked every other post, done what they did, but nothing worked, I've cleared the cache, I've checked for the camelCase, ... 我已经检查了所有其他帖子,完成了他们的工作,但没有任何效果,我已经清除了缓存,我已经检查了camelCase,...

What should I do? 我该怎么办?

The main function code: 主要功能代码:

var damage = parseFloat( Math.floor( Math.random() * skill.dmg/5 ) + skill.dmg )
damage += weapons[ user.inv.armor.weapon ].damage
var crit = ( ( Math.floor( Math.random() * 100 ) + skill.crit ) > 100 ? ( Math.random() + 1 ).toFixed( 1 ) : 1 )
damage *= crit
if ( !tags.includes( 'ignorant' ) ) {
    damage -= enemy.stats.res
    damage *= parseFloat( "0." + ( 100 - enemy.res[ tags[1] ] ) )
    damage -= shields[ enemy.inv.armor.shield ].res
}
damage = Math.floor( damage )damage = Math.floor( damage )
( monster ? enemy.hp -= damage : enemy.profile.hp -= damage )

Math.floor does indeed exists, it's not a problem with Math . Math.floor确实存在,它不是Math的问题。 If Math.floor wasn't a function the error would be: 如果Math.floor不是函数,则错误为:

TypeError: Math.floor is not a function

But you're getting 但是你得到了

TypeError: Math.floor(...) is not a function

Which means you're doing: 这意味着你在做什么:

Math.floor(damage)();

So post the code after damage = Math.floor( damage ) which most likely will be (...) , so we can pinpoint the exact error. 因此,在damage = Math.floor( damage )之后发布代码,这很可能是(...) ,因此我们可以精确定位错误。

 try { Math.floors(5); // Added extra S on purpose } catch(e){ console.log(e.message); } try { Math.floor(5)(); } catch(e){ console.log(e.message); } 

Update 更新

The error was triggered in the following code: 在以下代码中触发了错误:

damage = Math.floor( damage ) 
( monster ? enemy.hp -= damage : enemy.profile.hp -= damage )

What you were doing, is calling the result of Math.floor which is a number. 你在做什么,是调用 Math.floor的结果,这是一个数字。

damage = Math.floor( damage ); // ; this bad boy was all that was missing.
monster ? enemy.hp -= damage : enemy.profile.hp -= damage;

This is why semicolons are important! 这就是分号很重要的原因!

Do you recommend using semicolons after every statement in JavaScript? 您是否建议在JavaScript中的每个语句后使用分号?

yes, I absolutely do 是的,我绝对做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM