首页 > 解决方案 > 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;
}

标签: javascript

解决方案


来自String.prototype.sliceMDN 文档

开始索引

开始提取的从零开始的索引。如果为负,则将其视为 strLength + (beginIndex),其中 strLength 是字符串的长度(例如,如果 beginIndex 为 -3,则将其视为 strLength - 3)。如果 beginIndex 大于或等于字符串的长度,则 slice() 返回一个空字符串。

结束索引

可选的。结束提取的从零开始的索引。此索引处的字符将不包括在内。如果 endIndex 被省略, slice() 将提取到字符串的末尾。如果为负,则将其视为 strLength + endIndex,其中 strLength 是字符串的长度(例如,如果 endIndex 为 -3,则将其视为 strLength - 3)。


推荐阅读