截断 Javascript 中的字符串,混淆最后两个测试字符串

Truncating a string in Javascript, confusion of last two test strings

我正在为 freecodecamp 做一个项目。

项目规则:

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

Note that inserting the three dots to the end will add to the string length.

However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.

我的测试字符串包括:

truncateString("A-tisket a-tasket A green and yellow basket", 11) should return "A-tisket...".

truncateString("Peter Piper picked a peck of pickled peppers", 14) should return "Peter Piper...".

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return "A-tisket a-tasket A green and yellow basket".

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return "A-tisket a-tasket A green and yellow basket".

truncateString("A-", 1) should return "A...".

truncateString("Absolutely Longer", 2) should return "Ab...".

现在我已经使用 ternary 运算符解决了大部分问题,所以它相当简单,除了最后两个 ('A-', 1)('Absolutely Longer', 2),我的问题是,如何我能否成功完成截断并 return 最后两个字符串的预期输出?

来源:

function truncateString(str, num) {
  return str.length > num ? str.slice(0, num - 3) + '...' : str;
}

像这样:

function truncateString(str, num) {
  var result = str;

  if(str.length - 3 > num) {
    result = str.length > num ? str.slice(0, num - 3) + '...' : str;
  }
  return result;
}

truncateString("A-", 1)
// "A-"

truncateString("A-tisket a-tasket A green and yellow basket", 11)
// "A-tisket..."

您需要处理 num 小于或等于 3 的情况。您可以添加另一个条件来执行此操作:

function truncateString(str, num) {
  return str.length > num ? str.slice(0, num >= 3 ? num - 3 : num) + '...' : str;
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 11));
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 14));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2));
console.log(truncateString("A-", 1));
console.log(truncateString("Absolutely Longer", 2));

这会有所帮助。不完美但可以工作并且应该很容易让初学者理解

function truncateString(str, num) {
        var res = "";
        if (str.length > num) {
          if (num > 3) {
            res = str.slice(0, num - 3) + "...";
          } else {
            res = str.slice(0, num) + "...";
          }
          return res;
        }
        return str;

      }

      truncateString("A-tisket a-tasket A green and yellow basket", 11);

我注意到 2018 年的任务略有变化: 如果字符串(第一个参数)比给定的最大字符串长度(第二个参数)长,则截断该字符串(第一个参数)。 Return 以 ... 结尾的截断字符串。

解决方案是:

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) { 
    return str.slice(0, num) + '...';
  } else {
    return str; 
  }

}
truncateString("A-tisket a-tasket A green and yellow basket", 8);

说明: 首先我们检查字符串中的字母数是否大于给定的数字。如果是这样,我们将字符串的第一部分剪掉并在其中添加“...”。我们从第一个字母 (0) 开始删除给定数量的字母。

我对这个问题的简短解决方案。

const truncateString = (str, num) => str.length > num ? 
                                     str.substr(0, num) + "..." : 
                                     str;