Tianhe Gao

truncate-string

[Basic Algorithm Scripting: Truncate a String | freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string)

​## 我写的,用了 substring(),concat()

```js function truncateString(str, num) { if (str.length <= num) { return str }

return str.substring(0, num).concat('…') }

console.log(truncateString('A-tisket a-tasket A green and yellow basket', 8)) ```

​## slice()

```js function truncateString(str, num) { if (str.length <= num) { return str } return str.slice(0, num).concat('…') }

console.log(truncateString('A-tisket a-tasket A green and yellow basket', 8)) ```


No notes link to this note