FreeCodeCampChina / FreeCodeCampChina/freecodecamp.cn
条件一直不通过
- Dominant language
- CSS
- Stars
- 37.8k
- Forks
- 1.4k
- PR merge metrics
- No merged PRs in 30d
Description
Challenge [Truncate a string](https://freecodecamp.cn/challenges/truncate-a-string#?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20%E8%AF%B7%E6%8A%8A%E4%BD%A0%E7%9A%84%E4%BB%A3%E7%A0%81%E5%86%99%E5%9C%A8%E8%BF%99%E9%87%8C%0A%20%20var%20stra%20%3D%200%3B%0A%20%20if%20(str.length%20%3E%20num)%20%7B%0A%20%20%20%20stra%20%3D%20str.replace(str.slice(num%20-3)%2C%20%22...%22%20)%3B%0A%20%20%20%20%2F%2Fvar%20strb%20%3D%20str.substr(0%2C%20num)%3B%0A%20%20%7D%20else%20if%20(num%20%3C%203)%20%7B%0A%20%20%20%20stra%20%3D%20str.replace(str.slice(num%2C%20num)%2C%20%22...%22%20)%3B%0A%20%20%7D%0A%20%20%0A%20%20else%20%7B%0A%20%20%20%20stra%20%3D%20str.replace(str.slice(num)%2C%20%22%22%20)%3B%0A%20%20%7D%0A%20%20return%20stra%3B%0A%7D%0A%0A%2F%2Ftruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%20%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22.length)%3B%0A%2F%2Ftruncate(%22A-%22%2C%201)%3B%0Atruncate(%22Absolutely%20Longer%22%2C%202)%3B) has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
```javascript
function truncate(str, num) {
// 请把你的代码写在这里
var stra = 0;
if (str.length > num) {
stra = str.replace(str.slice(num -3), "..." );
//var strb = str.substr(0, num);
} else if (num < 3) {
stra = str.replace(str.slice(num, num), "..." );
}
else {
stra = str.replace(str.slice(num), "" );
}
return stra;
}
//truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);
//truncate("A-", 1);
truncate("Absolutely Longer", 2);
```
truncate("A-", 1) 应该返回 "A...". 和 truncate("Absolutely Longer", 2)这两个条件一直满足不了,请帮忙看一下问题出在哪里?谢谢!
Contributor guide
Research direction
Start with the linked “Truncate a string” challenge and run the reported calls for `truncate("A-", 1)` and `truncate("Absolutely Longer", 2)`. Compare their results with the expected outputs in the report, then verify the other challenge cases; done means all conditions pass and these examples return the stated strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100