diff --git a/curriculum/challenges/_meta/basic-javascript/meta.json b/curriculum/challenges/_meta/basic-javascript/meta.json index 639d5a9f2f..568e2d9633 100644 --- a/curriculum/challenges/_meta/basic-javascript/meta.json +++ b/curriculum/challenges/_meta/basic-javascript/meta.json @@ -443,6 +443,10 @@ [ "5cc0bd7a49b71cb96132e54c", "Use Recursion to Create a Range of Numbers" + ], + [ + "5cd9a70215d3c4e65518328f", + "Use Recursion to Create a Countdown" ] ], "helpRoom": "HelpJavaScript", diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md new file mode 100644 index 0000000000..7b9978ee3d --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md @@ -0,0 +1,91 @@ +--- +id: 5cd9a70215d3c4e65518328f +title: Use Recursion to Create a Countdown +challengeType: 1 +--- + +## Description +
+ +Continuing from the previous challenge, we provide you another opportunity to create a recursive function to solve a problem. + +
+ +## Instructions +
+ +We have defined a function called countdown with two parameters. The function should take an array in the myArray parameter and append the numbers n through 1 based on the n parameter. +For example, calling this function with n = 5 will pad the array with the numbers [5, 4, 3, 2, 1] inside of it. +Your function must use recursion by calling itself and must not use loops of any kind. + +
+ +## Tests +
+ +``` yml +tests: + - text: After calling countdown(myArray, -1), myArray should be empty. + testString: assert.isEmpty(padArray([], -1)); + - text: After calling countdown(myArray, 10), myArray should contain [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] + testString: assert.deepStrictEqual(padArray([], 10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); + - text: After calling countdown(myArray, 5), myArray should contain [5, 4, 3, 2, 1] + testString: assert.deepStrictEqual(padArray([], 5), [5, 4, 3, 2, 1]); + - text: Your code should not rely on any kind of loops (for or while or higher order functions such as forEach, map, filter, or reduce.). + testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)); + - text: You should use recursion to solve this problem. + testString: assert(removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)\;/)); +``` + +
+ +## Challenge Seed +
+ +
+ +```js + + +//Only change code below this line +function countdown(myArray, n){ + return; +} + +``` + +
+ +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +function padArray(arr, n){ + countdown(arr, n); + return arr; +} + +``` + +
+ +
+ +## Solution +
+ +```js +//Only change code below this line +function countdown(myArray, n){ + if(n <= 0){ + return; + } + else{ + myArray.push(n); + countdown(myArray, n - 1); + } +} +``` + +