diff --git a/README.md b/README.md index 7fa6597..9eda69d 100644 --- a/README.md +++ b/README.md @@ -1,100 +1,7 @@ -# Hack Reactor: Recursion in JavaScript +# [Hack Reactor](https://www.hackreactor.com): Recursion in JavaScript Workshop (Solutions) -You're here to solidify your understanding of recursion -- a fundamental programming concept -- in JavaScript. +Hey there! Welcome to the `solutions` branch of this workshop. Here you will find reference solutions to the practice exercises for Hack Reactor's Recursion in JavaScript workshop. -*IMPORTANT*: Completion of this workshop is no guarantee of admission into the Hack Reactor immersive program, nor does it have any influence in the admissions process. +**IMPORTANT:** Please do not refer to these solutions until you've given each exercise set a valiant effort. Please also note that only the `Basic Requirements` portions for each section may have solutions included. -## Prerequisites - -This workshop is designed for folks in the midst of studying intermediate-level JavaScript who have a few months of foundational experience. You will be expected to understand the basics of the Document Object Model (DOM) as applied to HTML. - -## Textbook - -No textbook is required for this workshop. All materials are included in this GitHub repo. - -## Technical requirements - -Laptop, Google Chrome browser and a text editor. If you do not have a text editor, we recommend Visual Studio Code, Sublime Text, or Atom. - -# How to use this repository - -### Let's get started... - -Run the SpecRunner.html file in a browser. This document currently shows 4 failing tests. - -- The `spec` folder holds all the failing tests that are being displayed in SpecRunner.html. -- The `src` folder holds the functions that are being called to run the tests. -- Your task is to edit the files in `src` to complete the functions and get the tests to pass. -- These files are just javascript files so you can use `console.log` to help debug and inspect these functions. - -## Recursion Review - -Recursion is a technique for solving problems wherein a function makes calls to itself. By doing so, it can complete a small amount of the processing, and delegate the rest of the problem to the recursive calls. - -Consider the following function: - -```js -var eat = function(meal){ - console.log('meal before bite:', meal); - console.log('now eating', meal.pop()); - if(meal.length){ - eat(meal); - } else { - console.log('done with the meal!'); - } -} -``` - -Which produces this output: - -```js -eat(['soup', 'potatoes', 'fish']); -// => meal before bite: ["soup", "potatoes", "fish"] -// => now eating fish -// => meal before bite: ["soup", "potatoes"] -// => now eating potatoes -// => meal before bite: ["soup"] -// => now eating soup -// => done with the meal! -``` - -You can use recursion on problems where smaller parts of the problem look the same as the larger problem as a whole. - -In this sprint, you'll be practicing writing recursive functions, building up to the reimplementation of a JavaScript browser method that involves recursion (getElementsByClassName). In so doing, don't use the things you're reimplementing, or any other built-in shortcuts that make these problems trivial. (You'll probably know if you're cheating, but feel free to ask us if you're not sure.) - -(Curious fact: many browsers don't have any of these functions in them, and people do need to reimplement them. When we reimplement new browser functionality in older browsers, it's called a "polyfill".) - -## Exercises - -More detailed instructions and examples for each exercise can be found in the individual `.js` files in the `src` folder. - -### 1: sumArray - -- [ ] Implement `sumArray` with your own code in `src/sumArray.js` - -### 2: power - -- [ ] Implement `power` with your own code in `src/power.js` - -### 3: nthFibonacci - -- [ ] Implement `nthFibonacci` with your own code in `src/nthFibonacci.js` - -### 4: getElementsByClassName - -- [ ] Implement `getElementsByClassName` with your own code in `src/getElementsByClassName.js` - - [ ] You should use `document.body`, `element.childNodes`, and `element.classList` -- NOTE: You may also use methods from the [underscore](https://underscorejs.org) library for assitance, but are not required to do so. -- You can view the MDN documentation for getElementsByClassName [here](https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName) - -#### Failing Tests Example - -![failing tests](images/failing-tests.png) - -#### Passing Tests Example - -![passing tests](images/passing-tests.png) - -### Don't forget.. - -You should throroughly read all of code in front of you and aim to understand line-by-line what is happening. \ No newline at end of file +**On Semantics:** Perfectly valid solutions can be written in a different style than the ones you find here. Please don't feel like your solutions need to have the exact same structure as those found herein; there are often numerous ways to solve the same problem in programming. diff --git a/src/getElementsByClassName.js b/src/getElementsByClassName.js index ce169b0..ddc028f 100644 --- a/src/getElementsByClassName.js +++ b/src/getElementsByClassName.js @@ -6,6 +6,19 @@ // But instead we're going to implement it from scratch: -var getElementsByClassName = function(className) { +var getElementsByClassName = function(name) { // Your code here -}; \ No newline at end of file + var results = []; + + function checkNode(node) { + var parts = node.className.split(' '); + if (parts.indexOf(name) >= 0) results.push(node); + + for (var i = 0; i < node.children.length; i++) { + checkNode(node.children[i]); + } + } + + checkNode(document.body); + return results; +} diff --git a/src/nthFibonacci.js b/src/nthFibonacci.js index a66be73..2b2b233 100644 --- a/src/nthFibonacci.js +++ b/src/nthFibonacci.js @@ -16,7 +16,10 @@ // nthFibonacci(4); // => 3 // etc... -var nthFibonacci = function(n) { +var nthFibonacci = function (n) { // Your code here + if (n < 2) { + return n; + } + return nthFibonacci(n - 1) + nthFibonacci(n - 2); }; - diff --git a/src/power.js b/src/power.js index 4f48746..c0d3dcb 100644 --- a/src/power.js +++ b/src/power.js @@ -14,4 +14,8 @@ var power = function(base, exponent) { // Your code here + if (exponent === 1) { + return base; + } + return base * power(base, exponent - 1); }; \ No newline at end of file diff --git a/src/sumArray.js b/src/sumArray.js index e0d85e7..ffc1a90 100644 --- a/src/sumArray.js +++ b/src/sumArray.js @@ -18,4 +18,8 @@ var sumArray = function(arr) { // Your code here + if (arr.length === 1) { + return arr[0]; + } + return arr.pop() + sumArray(arr); }; \ No newline at end of file