gpt4 book ai didi

javascript - 如何返回特定索引处的字符?

转载 作者:行者123 更新时间:2023-11-30 11:18:13 26 4
gpt4 key购买 nike

我正在尝试编写一个接受数组和字符串的函数。该函数需要使用数组方法.indexOf,找到传入的字符串在数组中的索引。然后它需要使用 .charAt 方法在字符串中找到该索引处的字符并返回该字符。我对此很困惑,不确定我到底需要做什么。非常感谢任何帮助!

这是我正在尝试的:

cipherize = (arr, str) => {
let index = arr.indexOf(str)
return str.charAt(0)
}

它必须通过这些测试:

should return "l" when called as cipherize(["books", "computers", "paper", "tablets"], "tablets")
should return "" when called as cipherize(["blue", "green", "yellow", "purple", "red"], "red")

最佳答案

@Stoney,只需将 0 替换为 index 即可。

> const cipherize = (arr, str) => {
... let index = arr.indexOf(str)
... return str.charAt(index)
... }
undefined
> cipherize(["books", "computers", "paper", "tablets"], "tablets");
'l'
>
> cipherize(["blue", "green", "yellow", "purple", "red"], "red")
''
>

关于javascript - 如何返回特定索引处的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com