Exercism Javascript Gigasecond - 我不懂数学

Lau*_*rie 0 javascript ruby math

谁能告诉我为什么在 Javascript 中这是由 Math.pow(10, 12) 或 1000000000000 而不是 Math.pow(10, 9) 或 1000000000 解决的。解决方案在 ruby​​ 中如此清晰,在 JS 中如此混乱。我的 Js 解决方案:

var gigDate = Math.pow(10, 12);

function Gigasecond(dateIn) {
  this.dateIn = dateIn
};

Gigasecond.prototype.date = function(){
  return new Date(this.dateIn.getTime() + gigDate);
};  

module.exports = Gigasecond;
Run Code Online (Sandbox Code Playgroud)

我的红宝石代码:

class Gigasecond
  VERSION = 1
  def self.from(date)
    date + (10**9)
  end
end
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

也许阅读一些精美的手册会有所帮助。在 JavaScript 中,getTime返回毫秒

Date.prototype.getTime()
[...]
返回值
一个数字,表示 1970 年 1 月 1 日 00:00:00 UTC 和给定日期之间经过的毫秒数。

但是在 Ruby 中,Time#+希望在 RHS 上看到几秒钟

时间+数字?time
Addition — 将一些秒数(可能是小数)添加到 time 并将该值作为新的 Time 对象返回。

有你的 10 3差异。

标准库中不同的语言,不同的接口,非常简单。