Freecodecamp: 变量应定义为“let”而不是“const”,因为它正在发生变化。

创建于 2017-03-03  ·  3评论  ·  资料来源: freeCodeCamp/freeCodeCamp



使用扩展运算符就地评估数组


https://beta.freecodecamp.com/en/challenges/es6/use-the-spread-operator-to-evaluate-arrays-inplace

问题描述


arr2 变量被定义为常量,但应该能够在练习中重新定义。

浏览器信息

  • 浏览器名称,版本:Google Chrome,版本 56.0.2924.87(64 位)
  • 操作系统:OS X El Capitan 10.11.6
  • 手机、台式机或平板电脑:台式机

潜在的解决方案


有两个选项可以解决这个挑战:

  1. 删除// change code below this line// change code above this line以便用户可以将解决方案...arr1直接添加到arr2实例中。
    解决方案是:
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
const arr2 = [...arr1];
  1. arr2的初始实例化改为: let arr2 = [];
    解决方案是:
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [];
// change code below this line
arr2 = [...arr1];
// change code above this line

这两种解决方案目前都通过了给定的测试。 我想清理这个练习,以便用户更容易理解。

最有用的评论

@Ethan-Arrowood 好收获!

实际上,这是可以解决的 - 但这很可能是预期的方式...... :smile:

copy-contents-of-array

我建议我们稍微改变challengeSeed的格式来解决这个问题:

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];

const arr2 = [];  // Change this line.

arr1.push('JUN');
console.log(arr2); // arr2 should not be affected

所有3条评论

@Ethan-Arrowood 好收获!

实际上,这是可以解决的 - 但这很可能是预期的方式...... :smile:

copy-contents-of-array

我建议我们稍微改变challengeSeed的格式来解决这个问题:

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];

const arr2 = [];  // Change this line.

arr1.push('JUN');
console.log(arr2); // arr2 should not be affected

我认为我们应该添加使用 .push() 函数的提示。 或者暗示使用来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 的东西。
今晚我会继续做这个改变。

@Ethan-Arrowood 我们正在删除 MDN 链接,以便为挑战提供提示功能,而不是简单地指向用户在学习时可能会觉得有点令人生畏的资源。

尽管如此,非常感谢您的问题和您的贡献。

此页面是否有帮助?
0 / 5 - 0 等级