CRT-600 practice question 30 of 30

Which two code snippets show working examples of a recursive function? Choose 2 answers

Full set: 223 questions
Answer options
A: Let countingDown = function(startNumber) { If ( startNumber >0) { console.log(startNumber) ; return countingDown(startNUmber); } else { return startNumber; }}; Correct answer
B: Function factorial ( numVar ) { If (numVar < 0) return; If ( numVar === 0 ) return 1; return numVar -1;
C: Const sumToTen = numVar => { If (numVar < 0) Return; return sumToTen(numVar + 1)};
D: Const factorial =numVar => { If (numVar < 0) return; If ( numVar === 0 ) return 1; return numVar * factorial ( numVar - 1 ); }; Correct answer
Correct Answer:
A. Let countingDown = function(startNumber) { If ( startNumber >0) { console.log(startNumber) ; return countingDown(startNUmber); } else { return startNumber; }};; D. Const factorial =numVar => { If (numVar < 0) return; If ( numVar === 0 ) return 1; return numVar * factorial ( numVar - 1 ); };
Explanation

A recursive function is one that calls itself. For it to be a "working" example, it must have two key components: a base case that stops the recursion and a recursive step that moves the input closer to the base case. Snippet D is a classic, working example of a recursive factorial function. It has a base case (numVar === 0) and a recursive step (factorial(numVar - 1)) that correctly reduces the problem until the base case is met. Snippet A demonstrates a valid recursive structure. It has a base case (startNumber <= 0) and a recursive call. Although the snippet contains a typo (startNUmber) and the recursive call as written would cause an infinite loop, it is the only other option that correctly models the fundamental structure of a recursive function, which is likely the question's intent.

Why Incorrect

B. This function is not recursive. It performs a simple calculation and returns a value without ever calling itself.
C. This function results in infinite recursion for any non-negative starting number. The recursive step sumToTen(numVar + 1) increases the value, moving it away from the base case (numVar < 0), which will never be reached.

References

1. Mozilla Developer Network (MDN) Web Docs: In the guide on JavaScript functions

recursion is defined by the presence of a base case and a recursive call that alters the input to eventually meet the base case. Snippet D perfectly matches this definition. Snippet C fails because its recursive call moves away from the base case.

Source: MDN Web Docs

"Functions - Recursion".

2. Eloquent JavaScript

3rd Edition by Marijn Haverbeke (A resource used in many university curricula): Chapter 3

"Functions

" section on "Recursion

" explains that a function calling itself must be careful not to "run out of stack space." This happens when the recursion is too deep or infinite. Snippet C is an example of this flaw

while Snippet D is presented as a correct implementation.

Source: Haverbeke

M. (2018). Eloquent JavaScript (3rd ed.). No Starch Press. Chapter 3

"Recursion".

3. Stanford University

CS106B Courseware

"Recursive Functions": Course materials emphasize that a recursive solution must solve a "smaller

simpler" version of the same problem. Snippet D does this by calling factorial(numVar - 1). Snippet A

assuming the typo is corrected to startNumber - 1

also follows this principle. Snippet C violates this by attempting to solve a "larger" problem (numVar + 1).

Source: Stanford University

CS106B Course Materials

"Topic 6: Recursion".

Scroll to Top