Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished javascript/Koans Tests #36

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 113 additions & 16 deletions koans/AboutApplyingWhatWeHaveLearnt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(productsICanEat.length).toBe(FILL_ME_IN);
expect(productsICanEat.length).toBe(1);
});

it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {

var productsICanEat = [];

/* solve using filter() & all() / any() */

expect(productsICanEat.length).toBe(FILL_ME_IN);
productsICanEat = products.filter((food) => {
return food.name == 'Pizza Primavera'
})

expect(productsICanEat.length).toBe(1);
});

/*********************************************************************************/
Expand All @@ -55,14 +58,28 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(sum).toBe(FILL_ME_IN);
expect(sum).toBe(233168);
});

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {

var sum = FILL_ME_IN; /* try chaining range() and reduce() */
var sum = []; /* try chaining range() and reduce() */
// Using Reduce() and range() Methods :
function reduce(start ,stop){


for(var i =start; i< stop; i++){
if (i % 3 === 0 || i % 5 === 0){
sum.push(i);
}}}
reduce(0,1000)

var mySum = sum.reduce((currentTotal, arg) =>{
return arg + currentTotal ;

expect(233168).toBe(FILL_ME_IN);
},0)

expect(mySum).toBe(233168);
});

/*********************************************************************************/
Expand All @@ -75,39 +92,119 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
expect(ingredientCount['mushrooms']).toBe(2);
});

it("should count the ingredient occurrence (functional)", function () {
var ingredientCount = { "{ingredient name}": 0 };

/* chain() together map(), flatten() and reduce() */

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
var ingredientCount = { "{mushrooms}": 0 };
var allIngredients = products.map((ing) => {
return ing.ingredients
});
var flatten = allIngredients.flat();

for( var i=0; i< flatten.length; i++){
if( flatten[i] == 'mushrooms'){
flatten[i] = 1;
}else{
flatten[i] = 0;
}
}

ingredientCount = flatten.reduce((totalValue, curr) => {
return curr + totalValue;
})

expect(ingredientCount['mushrooms']).toBe(2);
});

/*********************************************************************************/
/* UNCOMMENT FOR EXTRA CREDIT */
/*
it("should find the largest prime factor of a composite number", function () {

it("should find the largest prime factor of a composite number", function () {
function largestPrimeF(number){
if((number== 2) || (number== 3)){
return number;
}
var num,prime ;
for(var i= 0; i<= number; i++){
num = 1;
if( i % 2!=0){
var m=i/2;
for(j=2;j<=m;j++){
if(i %j ==0){
num =0; break;
}
}
}
if(num ==1 && number%i ==0)
{prime= i;}
}
var myPrime = largestPrimeF();
expect(myPrime).tobe(A_NUMBER)
}
});

it("should find the largest palindrome made from the product of two 3 digit numbers", function () {

function largestPalindrome(){
for ( var i =999; i>100; i++){
for(var j=999; j>100; j--){
var m= j*i;
if(isPalin(m)){
return i*j;
}
}
}
}
function isPalin(i){
return i.toString() == i.toString().split('').reverse().join('');
}
var result = largestPalindrome();
expect(result).tobe(906609)
});

it("should find the smallest number divisible by each of the numbers 1 to 20", function () {

function smallestDiv(limit){
var i, n =1;
function largestPow(n,limit){
var p,e = 2 ;
var largest = n;
while((p=math.pow(n,e)) <= limit){
largest = p;
e = e+1;
}
return largest;
}
function isPrime(n){
var i, limit = math.ceil(math.sqrt(n));
for(i = 3;i <= limit; i=i+2){
if(n % 1 === 0){
return false
}
}
return true;
}
for(i=3; i <= limit; i=i+2){
if(isPrime(i)){
largestPow(i,limit);
}
}

return n * largestPow(2,limit);
}
var myResult = smallestDiv(20)
expect(myResult).tobe(2520)

});

/*
it("should find the difference between the sum of the squares and the square of the sums", function () {

});

it("should find the 10001st prime", function () {

});
*/
});

}); */
61 changes: 31 additions & 30 deletions koans/AboutArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ describe("About Arrays", function() {
//We shall contemplate truth by testing reality, via spec expectations.
it("should create arrays", function() {
var emptyArray = [];
expect(typeof(emptyArray)).toBe(FILL_ME_IN); //A mistake? - http:javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(FILL_ME_IN);
var emptyObject = {};
expect(typeof(emptyArray)).toBe(typeof(emptyObject)); //A mistake? - http:javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(0);

var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(multiTypeArray[0]).toBe(FILL_ME_IN);
expect(multiTypeArray[2]).toBe(FILL_ME_IN);
expect(multiTypeArray[3]()).toBe(FILL_ME_IN);
expect(multiTypeArray[4].value1).toBe(FILL_ME_IN);
expect(multiTypeArray[4]["value2"]).toBe(FILL_ME_IN);
expect(multiTypeArray[5][0]).toBe(FILL_ME_IN);
expect(multiTypeArray[0]).toBe(0);
expect(multiTypeArray[2]).toBe("two");
expect(multiTypeArray[3]()).toBe(3);
expect(multiTypeArray[4].value1).toBe(4);
expect(multiTypeArray[4]["value2"]).toBe(5);
expect(multiTypeArray[5][0]).toBe(6);
});

it("should understand array literals", function () {
Expand All @@ -23,36 +24,36 @@ describe("About Arrays", function() {
expect(array).toEqual([1]);

array[1] = 2;
expect(array).toEqual([1, FILL_ME_IN]);
expect(array).toEqual([1, 2]);

array.push(3);
expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([1, 2, 3]);
});

it("should understand array length", function () {
var fourNumberArray = [1, 2, 3, 4];

expect(fourNumberArray.length).toBe(FILL_ME_IN);
expect(fourNumberArray.length).toBe(4);
fourNumberArray.push(5, 6);
expect(fourNumberArray.length).toBe(FILL_ME_IN);
expect(fourNumberArray.length).toBe(6);

var tenEmptyElementArray = new Array(10);
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
expect(tenEmptyElementArray.length).toBe(10);

tenEmptyElementArray.length = 5;
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
expect(tenEmptyElementArray.length).toBe(5);
});

it("should slice arrays", function () {
var array = ["peanut", "butter", "and", "jelly"];

expect(array.slice(0, 1)).toEqual(FILL_ME_IN);
expect(array.slice(0, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 20)).toEqual(FILL_ME_IN);
expect(array.slice(3, 0)).toEqual(FILL_ME_IN);
expect(array.slice(3, 100)).toEqual(FILL_ME_IN);
expect(array.slice(5, 1)).toEqual(FILL_ME_IN);
expect(array.slice(0, 1)).toEqual(["peanut"]);
expect(array.slice(0, 2)).toEqual(["peanut", "butter"]);
expect(array.slice(2, 2)).toEqual([]);
expect(array.slice(2, 20)).toEqual(["and", "jelly"]);
expect(array.slice(3, 0)).toEqual([]);
expect(array.slice(3, 100)).toEqual(["jelly"]);
expect(array.slice(5, 1)).toEqual([]);
});

it("should know array references", function () {
Expand All @@ -62,36 +63,36 @@ describe("About Arrays", function() {
refArray[1] = "changed in function";
}
passedByReference(array);
expect(array[1]).toBe(FILL_ME_IN);
expect(array[1]).toBe(("changed in function"));

var assignedArray = array;
assignedArray[5] = "changed in assignedArray";
expect(array[5]).toBe(FILL_ME_IN);
expect(array[5]).toBe("changed in assignedArray");

var copyOfArray = array.slice();
copyOfArray[3] = "changed in copyOfArray";
expect(array[3]).toBe(FILL_ME_IN);
expect(array[3]).toBe("three");
});

it("should push and pop", function () {
var array = [1, 2];
array.push(3);

expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([1, 2, 3]);

var poppedValue = array.pop();
expect(poppedValue).toBe(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
expect(poppedValue).toBe(3);
expect(array).toEqual([1, 2]);
});

it("should know about shifting arrays", function () {
var array = [1, 2];

array.unshift(3);
expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([3, 1, 2]);

var shiftedValue = array.shift();
expect(shiftedValue).toEqual(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
expect(shiftedValue).toEqual(3);
expect(array).toEqual([1, 2]);
});
});
10 changes: 5 additions & 5 deletions koans/AboutExpects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ describe("About Expects", function() {

//We shall contemplate truth by testing reality, via spec expectations.
it("should expect true", function() {
expect(false).toBeTruthy(); //This should be true
expect(true).toBeTruthy(); //This should be true
});

//To understand reality, we must compare our expectations against reality.
it("should expect equality", function () {
var expectedValue = FILL_ME_IN;
var expectedValue = (2 + 4) * 2 - 10;
var actualValue = 1 + 1;

expect(actualValue === expectedValue).toBeTruthy();
});

//Some ways of asserting equality are better than others.
it("should assert equality a better way", function () {
var expectedValue = FILL_ME_IN;
var expectedValue = 2;
var actualValue = 1 + 1;

// toEqual() compares using common sense equality.
Expand All @@ -24,7 +24,7 @@ describe("About Expects", function() {

//Sometimes you need to be really exact about what you "type".
it("should assert equality with ===", function () {
var expectedValue = FILL_ME_IN;
var expectedValue = '2';
var actualValue = (1 + 1).toString();

// toBe() will always use === to compare.
Expand All @@ -33,6 +33,6 @@ describe("About Expects", function() {

//Sometimes we will ask you to fill in the values.
it("should have filled in values", function () {
expect(1 + 1).toEqual(FILL_ME_IN);
expect(1 + 1).toEqual(2);
});
});
Loading