A Pen by Natasha Dykes on CodePen.
Last active
May 16, 2017 00:43
-
-
Save sashak007/d0936fb28e628313445ff626851a5aae to your computer and use it in GitHub Desktop.
Recursion Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Clear console messages. | |
console.clear(); | |
// Exercise 1: Write a recusrive sum function. | |
// sum will take an array of numbers and add every | |
// number in the array. | |
// i.e. sum([1,2,3,4]) returns 10 (1 + 2 + 3 + 4). | |
function sum(arr) { | |
if (arr.length === 0 ) { | |
return 0; | |
} | |
const secondToLastValue = arr.pop(); | |
const total = secondToLastValue + sum(arr); | |
return total; | |
} | |
test('Sum', function(assert) { | |
assert.equal(sum([1, 2, 3, 4, 5, 6]), 21); | |
assert.equal(sum([]), 0); | |
assert.equal(sum([0,0,0]), 0); | |
}); | |
// Exercise 2: Write a recursive factorial function. | |
// factorial takes a single number and then computes | |
// the factorial of that number. | |
// i.e. factorial(4) returns 24 (1 * 2 * 3 * 4). | |
function factorial(n) { | |
// needs handle negative cases | |
if (n === 1 || n === 0) { | |
return 1; | |
} | |
return n * factorial(n-1); | |
} | |
test('Factorial', function(assert) { | |
assert.equal(factorial(0), 1); | |
assert.equal(factorial(3), 6); | |
assert.equal(factorial(5), 120); | |
assert.equal(factorial(10), 3628800); | |
}); | |
// Exercise 3: Write a recursive function that reverses a // string. | |
// reverseString takes in a string and returns the reverse | |
// of that string. i.e. reverseString('abc') returns | |
// 'cda'. | |
function reverseString(str) { | |
let reversedStr =''; | |
for (let i = str.length; i >= 0; i--) { | |
reversedStr += str.charAt(i); | |
} | |
return reversedStr; | |
} | |
test('Reverse string', function(assert) { | |
assert.equal(reverseString('abcdef'), 'fedcba'); | |
assert.equal(reverseString('racecar'), 'racecar'); | |
assert.equal(reverseString(''), ''); | |
}); | |
// Exercise 4: Write a recursive isEven function. | |
// isEven takes a number and returns true if it is even, | |
// false if it is odd. i.e. isEven(4) returns true, | |
// isEven(5) returns false. | |
function isEven(num) { | |
if (num % 2 === 0) { | |
return true; | |
} | |
return false; | |
} | |
test('Even', function(assert) { | |
assert.isTrue(isEven(0)); | |
assert.isFalse(isEven(1)); | |
assert.isTrue(isEven(4)); | |
assert.isFalse(isEven(7)); | |
assert.isFalse(isEven(11)); | |
assert.isTrue(isEven(42)); | |
assert.isFalse(isEven(43)); | |
}); | |
// Exercise 5: Write a recursive range function with exclusive bounds. | |
// range takes a start and ending number. It will return | |
// and array with all numbers inbetween this start and | |
// end number, exluding them. i.e. range(2, 5) returns | |
// [3, 4], range(5, 6) returns []. | |
function range(start, end) { | |
const rangeArr = []; | |
if (start > end) { | |
return 'Sorry, starting value needs to be less than the end value.'; | |
} | |
if(start === end || start + 1 === end) { | |
return rangeArr; | |
} | |
for (var i = start+1; i< end; i++) { | |
rangeArr.push(i); | |
} | |
return rangeArr; | |
} | |
test('Range', function(assert) { | |
assert.deepEqual(range(3, 9), [4, 5, 6, 7, 8]); | |
assert.deepEqual(range(2,2), []); | |
assert.deepEqual(range(2,3), []); | |
assert.deepEqual(range(2,4), [3]); | |
// assert.deepEqual(range(4,2), []); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script> | |
<script src="https://rawgit.com/efuquen/798c7718f54e7204289fd307845f3e98/raw/d9fcf59f9202ed00e56b2ca97d5c0d7a238a407b/chai-assert-exercise-helper.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment