javascript - i 和 0 在这里做什么?谁能解释一下?
问题描述
我正在接受代码大战的挑战,我正在寻找解决方案,但我不明白。有人可以解释一下s
正在做什么o
吗?
function duplicateEncode(word) {
word = word.toLowerCase();
var c = '', s = '', o = '';
for (i = 0; i < word.length; i++) {
s = word.slice(i + 1);
o = s + word.slice(0, i);
c += (o.indexOf(word[i]) > -1) ? ')' : '(';
}
return c;
}
解决方案
来自String.prototype.slice
MDN 文档:
开始索引
开始提取的从零开始的索引。如果为负,则将其视为 strLength + (beginIndex),其中 strLength 是字符串的长度(例如,如果 beginIndex 为 -3,则将其视为 strLength - 3)。如果 beginIndex 大于或等于字符串的长度,则 slice() 返回一个空字符串。
结束索引
可选的。结束提取的从零开始的索引。此索引处的字符将不包括在内。如果 endIndex 被省略, slice() 将提取到字符串的末尾。如果为负,则将其视为 strLength + endIndex,其中 strLength 是字符串的长度(例如,如果 endIndex 为 -3,则将其视为 strLength - 3)。
推荐阅读
- c# - 如何找到数组的最小值和最大值以及数组的平方和反转的数组?
- asp.net - 如何使用 Azure 服务发送约会
- java - 如何通过 RestAssured 配置和发送多部分请求
- python - 我可以使用 2 个 for 循环和一个 if 语句进行嵌套列表理解吗?
- laravel - 目标类 [App\Http\Controllers\EventsController] 不存在
- c++ - 从我的二维数组中的一列返回字符串值被压缩成一个字符串(在 Mac os 上打开 Windows txt 文件)
- reactjs - 如何在反应测试库中将参数传递给 getByText?
- scala - 将日志重定向到 Scala 中的文件
- architecture - 读取 CQRS 中的模型和关系数据
- tcpdf - TCPDF Header 和 Top Margin 仅在第一页上,然后没有 header 和不同的 top margin 在其余页面上