javascript - 如何使用 ES6 脚本的模板文字
问题描述
我一直在尝试解决关于hackerrank 的模板文字问题。它在我的本地 IDE 上运行良好,但在 Hackerrank IDE 上出现错误。这是代码二加两个数字并使用模板文字打印结果。
const sum = () => {
let a=1;
let b=2;
console.log(`The sum of ${a} and ${b} is ${a + b}`);
}
module.exports = {sum}
但它正在产生以下错误。
npm WARN template-literals@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
audited 522 packages in 6.264s
found 611 vulnerabilities (378 low, 233 high)
run `npm audit fix` to fix them, or `npm audit` for details
> template-literals@1.0.0 test /projects/challenge
> mocha test --reporter mocha-junit-reporter
The sum of 1 and 2 is 3
npm ERR! Test failed. See above for more details.
解决方案
我想补充一下我的看法。
const sum = (a,b) => {
return `The sum of ${a} and ${b} is ${a + b}`;
};
module.exports = {sum}
分号在这些类型的代码中很重要
推荐阅读
- c# - C# 使用图形去除颜色
- javascript - 在 JavaScript 中,如何用逗号分割字符串“aa,bb\\,cc,dd”,但前提是前一个字符不是反斜杠?
- javascript - 如何在 JavaScript 中将字符串转换为对象?
- imagemagick - 如何使用 imagemagick 将 .psd 文件转换为 jpg、pdf 和 png (RGB/CMYK)
- java - 为什么我不能在 java 中调用 nextLine() 方法两次?
- c# - 如何在 Android WebView 中禁用一键缩放?
- python - 使用 Python 的时间戳更新 Googlesheet 单元格
- sql - 使用表示变量在 OBIEE 中转换直接 sql 查询的日期
- java - 登录用户和注册用户登录和注册功能
- c - 使用 C 代码连接到 Monetdb 时出现链接错误