我如何在 JS 代码中编写自然数的总和公式我尝试了这段代码,但无法理解出了什么问题

问题描述

    let sumOfSquareNum = (num) =>{
        let S =  (num(num + 1)(2*num + 1 )/6)
       
      alert (`The result of sum of 50 is ${S} where input is ${num}` )
      }
    sumOfSquareNum(5)

解决方法

您需要在隐含的位置插入 * 否则您将得到一个 TypeError,因为它由于缺少运算符而在语法上不正确。

const sumOfSquareNum = (num) => {
  const S = (num * (num + 1) * (2 * num + 1) / 6)

  alert(`The result of sum of 50 is ${S} where input is ${num}`)
}
sumOfSquareNum(5);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...