小编Cor*_*man的帖子

提高 JavaScript 函数的速度

我在 CodeWars 上找到了一个任务,我设法解决了它,但是,提交后说:

执行超时:(12000 毫秒)

当我尝试测试该函数是否通过时,但我猜它太慢了。在你谴责我没有自己找到答案之前。我并不真正关心将其作为回复提交,但我不知道如何使其更快,这就是我在这里的原因。这是函数:

const ls = [0, 1, 3, 6, 10]

const partsSums = (ls) => {
    const sum = []
    for(let i = 0, len = ls.length; i < len + 1; i++) {
        let result = ls.slice(i).reduce( (accumulator, currentValue) => accumulator + currentValue, 0)
        sum.push(result)
    }
    return sum
}
Run Code Online (Sandbox Code Playgroud)

以下是说明:

让我们考虑这个例子(以通用格式编写的数组):

ls = [0, 1, 3, 6, 10]
Run Code Online (Sandbox Code Playgroud)

其以下部分:

ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sum function

29
推荐指数
2
解决办法
1442
查看次数

API 密钥不以“SG”开头。发送网格

我正在尝试在 Heroku NodeJS 应用程序中设置 SendGrid 插件。我创建了 API 密钥并将其设置为环境变量。

整个 API 密钥类似于:SG.actualValue.bbb_cccccc

我做的第一个设置我将整个密钥设置为我的 SENDGRID_API_KEY 并且我收到了这个错误:

API 密钥不以 SG 开头。

因此,我意识到错误并取消设置环境变量并将其重新设置为整个键的 actualValue 部分。

但是,我仍然遇到相同的错误。我尝试再次做同样的事情或重新启动终端(实际上是整个笔记本电脑)。

这是我试图从 SendGrid 设置页面运行的测试代码:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个新密钥并进行设置,但出现相同的错误。我尝试将其设置为整个密钥,但没有“.SG”或只是 bbb_ccccc 部分。先感谢您。

javascript email heroku node.js sendgrid

8
推荐指数
3
解决办法
7529
查看次数

标签 统计

javascript ×2

arrays ×1

email ×1

function ×1

heroku ×1

node.js ×1

sendgrid ×1

sum ×1