I wrote
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(3));
the codes is calculate factorial.
But I wrote the code in jsfiddle and run, nothing happens. And I wrote html like
<script>
function factorial(n) {
・
・
console.log(factorial(3));
</script>
and when it is browsed in GoogleChrome,nothing shows in console.I want to see 6 in console.log.6 is calculated by 1*2*3. What is wrong in my codes?How should I fix this?
It runs for me. Try putting your script tag inside <body>
.
<html>
<head><title>Does it run?</title></head>
<body>
<h1>Test</h1>
<script>
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(3));
</script>
</body>
</html>