首页 > 解决方案 > 我错过了什么?拉姆达挑战

问题描述

我正在尝试进行 Lambda 挑战,输出是它所要求的,但它仍然不正确?

问题是:“修改函数以将给定字符串转换为数组,其中数组中的每个字符与字符串中的索引相同,然后返回它。”

function convertStringToArray(s) {
var output = Array.from("hello");
return output;
}

/* Do not modify code below this line */

const exampleString = 'hello';
const stringAsArray = convertStringToArray(exampleString);

console.log(stringAsArray, '<-- should be ["h", "e", "l", "l", "o"]');

输出

["h", "e", "l", "l", "o"] <-- should be ["h", "e", "l", "l", "o"]

我做了它想要的,为什么我被卡住了?

标签: javascript

解决方案


您的代码采用s- 它忽略的参数。

我高度怀疑 lambda 挑战正在将不同的值传递给您的函数(仅用"hello"作示例)并且您的代码失败了,因为它只有在is["h", "e", "l", "l", "o"]时才会返回。s"world"

尝试在函数的第一行使用s而不是。"hello"


推荐阅读