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

worked thru the koans exercise #31

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
changed the incorrect information about functions
  • Loading branch information
blakeFleck committed Aug 20, 2016
commit 4a50d10a9428e3512771c177f1e48bb974b8a0db
69 changes: 36 additions & 33 deletions koans/AboutFunctions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
describe("About Functions", function() {

it("should declare functions", function() {

function add(a, b) {
return a + b;
}
expect(add(1, 2)).toBe(FILL_ME_IN);

expect(add(1, 2)).toBe(3);
});

it("should know internal variables override outer variables", function () {
var message = "Outer";

function getMessage() {
return message;
}

function overrideMessage() {
var message = "Inner";
return message;
}
expect(getMessage()).toBe(FILL_ME_IN);
expect(overrideMessage()).toBe(FILL_ME_IN);
expect(message).toBe(FILL_ME_IN);

expect(getMessage()).toBe("Outer");
expect(overrideMessage()).toBe("Inner");
expect(message).toBe("Outer");
});

it("should have lexical scoping", function () {
@@ -35,74 +35,77 @@ describe("About Functions", function() {
}
return childfunction();
}
expect(parentfunction()).toBe(FILL_ME_IN);
expect(parentfunction()).toBe("local");
});

it("should use lexical scoping to synthesise functions", function () {

function makeIncreaseByFunction(increaseByAmount) {
return function (numberToIncrease) {
return numberToIncrease + increaseByAmount;
};
}

var increaseBy3 = makeIncreaseByFunction(3);
var increaseBy5 = makeIncreaseByFunction(5);
expect(increaseBy3(10) + increaseBy5(10)).toBe(FILL_ME_IN);

expect(increaseBy3(10) + increaseBy5(10)).toBe(28);
});

it("should allow extra function arguments", function () {

function returnFirstArg(firstArg) {
return firstArg;
}
expect(returnFirstArg("first", "second", "third")).toBe(FILL_ME_IN);

expect(returnFirstArg("first", "second", "third")).toBe("first");

function returnSecondArg(firstArg, secondArg) {
return secondArg;
}
expect(returnSecondArg("only give first arg")).toBe(FILL_ME_IN);

expect(returnSecondArg("only give first arg")).toBe(undefined);

function returnAllArgs() {
var argsArray = [];
for (var i = 0; i < arguments.length; i += 1) {
argsArray.push(arguments[i]);
}
return argsArray.join(",");
}
expect(returnAllArgs("first", "second", "third")).toBe(FILL_ME_IN);

expect(returnAllArgs("first", "second", "third")).toBe('first,second,third');
});

it("should pass functions as values", function () {

var appendRules = function (name) {
return name + " rules!";
};

var appendDoubleRules = function (name) {
return name + " totally rules!";
};

var praiseSinger = { givePraise: appendRules };
expect(praiseSinger.givePraise("John")).toBe(FILL_ME_IN);
expect(praiseSinger.givePraise("John")).toBe("John rules!");

praiseSinger.givePraise = appendDoubleRules;
expect(praiseSinger.givePraise("Mary")).toBe(FILL_ME_IN);
expect(praiseSinger.givePraise("Mary")).toBe("Mary totally rules!");

});

it("should use function body as a string", function () {
var add = new Function("a", "b", "return a + b;");
expect(add(1, 2)).toBe(FILL_ME_IN);
expect(add(1, 2)).toBe(3);

var multiply = function (a, b) {
//An internal comment
return a * b;
};
expect(multiply.toString()).toBe(FILL_ME_IN);
});
expect(multiply.toString()).toBe('function (a, b) {
//An internal comment
return a * b;
}'
});
});