Skip to content

Commit

Permalink
adding tests for all except es2015 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
elie committed May 25, 2017
1 parent 01ed464 commit 72c657c
Show file tree
Hide file tree
Showing 15 changed files with 904 additions and 55 deletions.
11 changes: 6 additions & 5 deletions advanced-array-methods/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
<script type="text/javascript" src="../jasmine/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="../jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="../jasmine/lib/jasmine-core/boot.js"></script>
<script src="filter-exercises"></script>
<script src="forEach-exercises"></script>
<script src="map-exercises"></script>
<script src="reduce-exercises"></script>
<script src="some-every-exercises"></script>
<script src="filter-exercises.js"></script>
<script src="forEach-exercises.js"></script>
<script src="map-exercises.js"></script>
<script src="reduce-exercises.js"></script>
<script src="some-every-exercises.js"></script>
<script src="spec/array.spec.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions advanced-array-methods/reduce-exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function extractValue(arr, key){
function vowelCount(str){
var vowels = "aeiou";
return str.split('').reduce(function(acc,next){
if(vowels.indexOf(next.toLowerCase() !== -1)){
if(vowels.indexOf(next.toLowerCase()) !== -1){
if(acc[next]){
acc[next]++;
} else {
Expand All @@ -23,7 +23,7 @@ function addKeyAndValue(arr, key, value){
return arr.reduce(function(acc,next,idx){
acc[idx][key] = value;
return acc;
},arr.slice());
},arr);
}

function partition(arr, cb){
Expand Down
264 changes: 264 additions & 0 deletions advanced-array-methods/spec/array.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
// forEach Exercises

describe("#doubleValues", function(){
it("doubles values in an array", function(){
expect(doubleValues([1,2,3])).toEqual([2,4,6])
});
it("works for negative numbers", function(){
expect(doubleValues([1,-2,-3])).toEqual([2,-4,-6])
});
});

describe("#onlyEvenValues", function(){
it("returns a new array of only even values", function(){
expect(onlyEvenValues([1,2,3,4,5,6])).toEqual([2,4,6])
});
});

describe("#showFirstAndLast", function(){
it("returns an array of only the first and last characters in an array", function(){
expect(showFirstAndLast(['elie','colt','matt','tim'])).toEqual(['ee','ct','mt','tm'])
});
});

describe("#addKeyAndValue", function(){
it("adds a key and value to an array of objects", function(){
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
var updatedArr = [{name: 'Elie', title:'instructor'}, {name: 'Tim', title:'instructor'}, {name: 'Matt', title:'instructor'}, {name: 'Colt', title:'instructor'}]
expect(addKeyAndValue(arr,'title','instructor')).toEqual(updatedArr)
});
});

describe("#vowelCount", function(){
it("returns an object with the keys as vowels and the values as the count", function(){
expect(vowelCount('elie')).toEqual({e:2,i:1});
expect(vowelCount('tim')).toEqual({i:1});
expect(vowelCount('matt')).toEqual({a:1});
expect(vowelCount('hmmm')).toEqual({});
expect(vowelCount('i am awesome and so are you')).toEqual({i: 1, a: 4, e: 3, o: 3, u: 1});
});
});

// map Exercises

describe("#doubleValues", function(){
it("doubles values in an array", function(){
expect(doubleValues([1,2,3])).toEqual([2,4,6])
});
it("works for negative numbers", function(){
expect(doubleValues([1,-2,-3])).toEqual([2,-4,-6])
});
});

describe("#valTimesIndex", function(){
it("returns a new array with each value multiplied by the index", function(){
expect(valTimesIndex([1,2,3])).toEqual([0,2,6])
});
it("works for negative numbers", function(){
expect(valTimesIndex([1,-2,-3])).toEqual([0,-2,-6])
});
});

describe("#extractValue", function(){
it("returns a new array with the value of each key in an array objects", function(){
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
expect(extractValue(arr,'name')).toEqual(['Elie', 'Tim', 'Matt', 'Colt'])
});
});

describe("#extractFulName", function(){
var arr = [{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]
it("returns a new array with the value of each key in an array objects", function(){
expect(extractFullName(arr,'name')).toEqual(['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele'])
});
});

// filter exercises

describe("#filterByValue", function(){
var arr = [{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}]
it("returns a new array of objects that contain a key", function(){
expect(filterByValue(arr, 'isCatOwner')).toEqual([{first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Colt', last:"Steele", isCatOwner: true}])
});
});

describe("#find", function(){
var arr = [1,2,3,4,5];
it("returns the first value found in an array", function(){
expect(find(arr,3)).toEqual(3)
});
it("returns undefined if the value is not found", function(){
expect(find(arr,10)).toEqual(undefined)
});
});

describe("#findInObj", function(){
var arr = [{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}]
it("returns the first value found in an array", function(){
expect(findInObj(arr,'isCatOwner',true)).toEqual({first: 'Tim', last:"Garcia", isCatOwner: true})
});
it("returns undefined if the value is not found", function(){
expect(findInObj(arr,'isCatOwner',false)).toEqual(undefined)
});
});

describe("#removeVowels", function(){
it("removes all vowels from a string", function(){
expect(removeVowels('elie')).toEqual('l')
expect(removeVowels('TIM')).toEqual('tm')
expect(removeVowels('ZZZZZZ')).toEqual('zzzzzz')
});
});

describe("#doubleOddNumbers", function(){
it("returns an array of all odd numbers doubled", function(){
expect(doubleOddNumbers([1,2,3,4,5])).toEqual([2,6,10])
expect(doubleOddNumbers([4,4,4,4,4])).toEqual([])
});
});

// some + every

describe("#hasOddNumber", function(){
it("returns true if there is at least one odd number in the array", function(){
expect(hasOddNumber([1,2,2,2,2,2,4])).toEqual(true)
});
it("returns false if there are no odd numbers in the array", function(){
expect(hasOddNumber([2,2,2,2,2,4])).toEqual(false)
});
});

describe("#hasAZero", function(){
it("returns true if the number contains at least one 0", function(){
expect(hasAZero(3332123213101232321)).toEqual(true)
});
it("returns false if the number does not have any zeros", function(){
expect(hasAZero(1212121)).toEqual(false)
});
});

describe("#hasOnlyOddNumbers", function(){
it("returns true if every number in the array is odd", function(){
expect(hasOnlyOddNumbers([1,3,5,7])).toEqual(true)
});
it("returns false if there is one or more even numbers ", function(){
expect(hasOnlyOddNumbers([1,2,3,5,7])).toEqual(false)
});
});

describe("#hasNoDuplicates", function(){
it("returns true if there are no duplicates in the array", function(){
expect(hasNoDuplicates([1,2,3,1])).toEqual(false)
});
it("returns false if there are duplicates in the array", function(){
expect(hasNoDuplicates([1,2,3])).toEqual(true)
});
});

describe("#hasCertainKey", function(){
var arr = [{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}]
it("returns true if every object in the array contains a certain key", function(){
expect(hasCertainKey(arr,'first')).toEqual(true)
});
it("returns false if every object in the array does not contain a certain key", function(){
expect(hasCertainKey(arr,'isCatOwner')).toEqual(false)
});
});

describe("#hasCertainValue", function(){
var arr = [{title: "Instructor", first: 'Elie', last:"Schoppik"}, {title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true}, {title: "Instructor", first: 'Matt', last:"Lane"}, {title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}]
it("returns true if every object in an array has the same value for some key", function(){
expect(hasCertainValue(arr,'title','Instructor')).toEqual(true)
});
it("returns false if every object in an array does not have the same value for some key", function(){
expect(hasCertainValue(arr,'first','Elie')).toEqual(false)
});
});

// reduce exercises

describe("#extractValue", function(){
it("returns a new array with the value of each key in an array objects", function(){
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
expect(extractValue(arr,'name')).toEqual(['Elie', 'Tim', 'Matt', 'Colt'])
});
});

describe("#vowelCount", function(){
it("returns an object with the keys as vowels and the values as the count", function(){
expect(vowelCount('elie')).toEqual({e:2,i:1});
expect(vowelCount('tim')).toEqual({i:1});
expect(vowelCount('matt')).toEqual({a:1});
expect(vowelCount('hmmm')).toEqual({});
expect(vowelCount('i am awesome and so are you')).toEqual({i: 1, a: 4, e: 3, o: 3, u: 1});
});
});

describe("#addKeyAndValue", function(){
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}];
var updated = [{title: 'Instructor', name: 'Elie'}, {title: 'Instructor', name: 'Tim'}, {title: 'Instructor', name: 'Matt'}, {title: 'Instructor', name: 'Colt'}];
it("adds a key and value to each object in an array of objects", function(){
expect(addKeyAndValue(arr, 'title', 'Instructor')).toEqual(updated)
});
});

describe("#partition", function(){
function isEven(val){
return val % 2 === 0;
}
function isLongerThanThreeCharacters(val){
return val.length > 3;
}
var arr = [1,2,3,4,5,6,7,8];
var names = ['Elie', 'Colt', 'Tim', 'Matt'];
it("returns an array of arrays with the first subarray as values returning true from the callback", function(){
expect(partition(arr, isEven)).toEqual([[2,4,6,8],[1,3,5,7]]);
expect(partition(names, isLongerThanThreeCharacters)).toEqual([['Elie', 'Colt', 'Matt'],['Tim']]);
});
});














































20 changes: 10 additions & 10 deletions closures-and-keyword-this/call-apply-bind-exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ function sumEvenArguments(){

// Write a function called sumEvenArguments which takes all of the arguments passed to a function and returns the sum of the even ones.

sumEvenArguments(1,2,3,4) // 6
sumEvenArguments(1,2,6) // 8
sumEvenArguments(1,2) // 2
// sumEvenArguments(1,2,3,4) // 6
// sumEvenArguments(1,2,6) // 8
// sumEvenArguments(1,2) // 2

// Write a function called invokeMax which accepts a function and a maximum amount. invokeMax should return a function that when called increments a counter. If the counter is greater than the maximum amount, the inner function should return "Maxed Out"
// // Write a function called invokeMax which accepts a function and a maximum amount. invokeMax should return a function that when called increments a counter. If the counter is greater than the maximum amount, the inner function should return "Maxed Out"

var addOnlyThreeTimes = invokeMax(add,3);
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(2,2) // 4
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(1,2) // "Maxed Out!"
// var addOnlyThreeTimes = invokeMax(add,3);
// addOnlyThreeTimes(1,2) // 3
// addOnlyThreeTimes(2,2) // 4
// addOnlyThreeTimes(1,2) // 3
// addOnlyThreeTimes(1,2) // "Maxed Out!"


function add(a,b){
Expand Down Expand Up @@ -70,4 +70,4 @@ function flip(fn, thisArg){
var allArgs = outerArgs.concat(innerArgs).slice(0, fn.length)
return fn.apply(thisArg, allArgs.reverse())
}
}
}
5 changes: 3 additions & 2 deletions closures-and-keyword-this/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<script type="text/javascript" src="../jasmine/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="../jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="../jasmine/lib/jasmine-core/boot.js"></script>
<script src="call-apply-bind-exercises"></script>
<script src="closures-exercises"></script>
<script src="call-apply-bind-exercises.js"></script>
<script src="closures-exercises.js"></script>
<script src="spec/closures.spec.js"></script>
</body>
</html>
Loading

0 comments on commit 72c657c

Please sign in to comment.