无法通过递归函数的断言测试
Can't pass an assert test for a Recursion Function
var compareStr = function(str1, str2) {
if (str1.slice(1) !== str2.slice(1)) {
return false;
}
if (str1.slice(1) === '' && str2.slice(1) === '') {
return true;
}
return compareStr(str1.slice(1), str2.slice(1));
}
我正在使用递归在两个字符串参数之间创建一个字符串比较函数。我正在针对各种摩卡测试测试我的功能。尤其是这一关我过不了
AssertionError: expected 1 to be above 1
指向此代码:
it('should use recursion by calling self', function() {
compareStr('house', 'houses');
expect(compareStr.callCount).to.be.above(1);
});
现在我认为正在发生的事情是,如果 str1
和 str2
与第一个元素不相等,那么我的函数 returns 为假。在我看来,这是一个很好的边缘案例,因为在那之后执行该功能毫无意义。这个断言测试是否在至少通过一次递归后寻找 false
的结果?这意味着递归需要至少发生一次才能通过。
我认为测试希望您编写一个逐个字符比较字符串的函数,在这种情况下,您需要到达 houses
的最终 s
以递归到完成:
var compareStr = function(str1, str2) {
console.log('compareStr called');
return str1.length <= 1 || str2.length <= 1
? str1 === str2
: compareStr(str1.slice(1), str2.slice(1));
};
console.log(compareStr('house', 'houses'));
var compareStr = function(str1, str2) {
if (str1.slice(1) !== str2.slice(1)) {
return false;
}
if (str1.slice(1) === '' && str2.slice(1) === '') {
return true;
}
return compareStr(str1.slice(1), str2.slice(1));
}
我正在使用递归在两个字符串参数之间创建一个字符串比较函数。我正在针对各种摩卡测试测试我的功能。尤其是这一关我过不了
AssertionError: expected 1 to be above 1
指向此代码:
it('should use recursion by calling self', function() {
compareStr('house', 'houses');
expect(compareStr.callCount).to.be.above(1);
});
现在我认为正在发生的事情是,如果 str1
和 str2
与第一个元素不相等,那么我的函数 returns 为假。在我看来,这是一个很好的边缘案例,因为在那之后执行该功能毫无意义。这个断言测试是否在至少通过一次递归后寻找 false
的结果?这意味着递归需要至少发生一次才能通过。
我认为测试希望您编写一个逐个字符比较字符串的函数,在这种情况下,您需要到达 houses
的最终 s
以递归到完成:
var compareStr = function(str1, str2) {
console.log('compareStr called');
return str1.length <= 1 || str2.length <= 1
? str1 === str2
: compareStr(str1.slice(1), str2.slice(1));
};
console.log(compareStr('house', 'houses'));