我刚刚在YouTube上观看5个视频,了解了什么是递归。
这个想法更清晰,但不够清楚,我猜我自己的递归函数只在控制台中显示一次发誓。
JSFiddle:http://jsfiddle.net/05xxxbet/
/*
* @param {string} swear - your insult
* @param {number} times - how many times to repeat
*/
var recursiveSwear = function(swear, times){
if(times == 1)
return swear;
else
return recursiveSwear(swear, times - 1);
};
console.log(recursiveSwear("You bastard!", 6));
// Desired output:
// You Bastard!
// You Bastard!
// You Bastard!
// You Bastard!
// You Bastard!
// You Bastard!
//Actual output:
// You Bastard!
答案 0 :(得分:4)
你的功能很好,但你没有记录"发誓"
var recursiveSwear = function(swear, times){
if(times == 1)
return swear;
else
console.log(swear); //log them here
return recursiveSwear(swear, times - 1);
};
console.log(recursiveSwear("Shut up!", 6));
答案 1 :(得分:3)
在进行递归工作时,通常需要将当前值与递归调用的返回值结合使用。在这种情况下,您可以按如下方式附加它们:
var recursiveSwear = function(swear, times){
if(times == 1)
return swear;
else
return swear + ' ' + recursiveSwear(swear, times - 1);
};
console.log(recursiveSwear("Oh darn!~", 6));
答案 2 :(得分:2)
递归函数的主体实际上没有任何类型的输出。你不应该在2015-06-29T20:50:16.244+0100|Warning: StandardWrapperValve[Jersey Web Application]: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.NoClassDefFoundError: org/owasp/esapi/ESAPI
at com.pododdle.util.ToJSON.toJSONArray(ToJSON.java:60)
at com.pododdle.dao.MySQL.runDBQuery(MySQL.java:55)
at com.pododdle.service.CategoryService.getAllCategories(CategoryService.java:10)
at com.pododdle.resources.CategoryResource.getCategories(CategoryResource.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
条款中移动你的console.log
吗? e.g:
else