Unexpected syntax error: unexpected number

Unexpected syntax error: unexpected number

我正在尝试创建一个函数来创建带小数的随机数,但我在使用 1e+decimalPlaces 时遇到语法错误。

函数是:

_$.randomNumberWithDecimals = function(min, max, decimals) {
    var random = Math.random();
    random = random * 1e+decimals;
};

并且出现错误:

random = random * 1e+decimals;

我的编辑给我的错误是: Unexpected 1. 我在 google 开发工具中得到的错误是:SyntaxError: Unexpected token ILLEGAL
谁能解释一下这个错误,以及如何解决这个问题?

你是说 Math.pow(10,decimals) 吗?

您不能只是将代码拼凑在一起并期望它能正常工作。当您写 1e+decimals 时,您可能 认为 它会理解 "stick them together like 1e5 and make it work",但事实并非如此,因为它意味着……好吧,真的没什么。 1e 单独不是一个有效的数字,这就是你得到错误的原因。

1e 是非法的。您可能想要 1e11e2、... (n)e(m)e.

左右需要两个数字

您也可以尝试 Math.pow(10, decimals)10 ** decimals(不幸的是,主流浏览器尚不支持后者)。