/    Sign up×
Community /Pin to ProfileBookmark

Hi everyone. Can you please make a javascript code that uses recursion when adding two inputted numbers? i have a hard time understanding recursion.. ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@OfekmeisterSep 23.2010 — [CODE]<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
<!--

function Factorial(n)
{

if (n == 0) // Base case, where you want it to stop
{

return 1;

}

return (n * Factorial(n-1));

}


document.write(Factorial(4));

//-->
</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@rnd_meSep 23.2010 — [CODE]function sum(){
var a=arguments;
return a.length > 1 ?
a[0] + sum.apply(0, [].slice.call(a).slice(1) ) :
a[0] ;
}

alert([
sum(1,2) == 3 ,
sum(1,2,3,4) == 10 ,
sum(5,-10) == -5
]);[/CODE]


bonus:
[CODE]
function factorial(n){return n?n--*factorial(n):1}[/CODE]
×

Success!

Help @jomawie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...