已解决
0043【Edabit ★☆☆☆☆☆】【递归求字符串的长度】Recursion: Length of a String
来自网友在路上 171871提问 提问时间:2023-10-28 15:44:09阅读次数: 71
最佳答案 问答题库718位专家为你答疑解惑
0043【Edabit ★☆☆☆☆☆】【递归求字符串的长度】Recursion: Length of a String
language_fundamentals
recursion
strings
Instructions
Write a function that returns the length of a string. Make your function recursive.
Examples
length("apple") // 5
length("make") // 4
length("a") // 1
length("") // 0
Notes
- N/A
Solutions
function length(str) {if(str.length == 0){return 0;}return 1 + length(str.slice(1));
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertEquals(length('shipment'), 8)
Test.assertEquals(length('apple'), 5)
Test.assertEquals(length('make'), 4)
Test.assertEquals(length('a'), 1)
Test.assertEquals(length(''), 0)
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"0043【Edabit ★☆☆☆☆☆】【递归求字符串的长度】Recursion: Length of a String":http://eshow365.cn/6-26913-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!