Skip to content
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
33 changes: 17 additions & 16 deletions module-exercises/explicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
// fix the test cases' expected values to pass the function
const StringTests = [
// string values remain unchanged
{ name: 'str, any string', args: ['any string'], expected: null },
{ name: 'str, any string', args: ['any string'], expected: "any string" },
// casting with String just puts quotes around a thing
{ name: 'num, 3', args: [3], expected: null },
{ name: 'boo, true', args: [true], expected: null },
{ name: 'obj, null', args: [null], expected: null },
{ name: 'und, undefined', args: [undefined], expected: null },
{ name: 'num, 3', args: [3], expected: "3"},
{ name: 'boo, true', args: [true], expected: "true" },
{ name: 'obj, null', args: [null], expected: "null" },
{ name: 'und, undefined', args: [undefined], expected: "undefined" },

// write at least 5 more test cases for the String function
];
String.quizzing = true;
Expand All @@ -45,11 +46,11 @@ const NumberTests = [
{ name: 'num, Infinity', args: [Infinity], expected: Infinity },
{ name: 'num, NaN', args: [NaN], expected: NaN },
// true and false, the only boolean values
{ name: 'boo, true', args: [true], expected: 0 },
{ name: 'boo, false', args: [false], expected: 1 },
{ name: 'boo, true', args: [true], expected: 1 },
{ name: 'boo, false', args: [false], expected: 0 },
// null & undefined
{ name: 'obj, null', args: [null], expected: NaN },
{ name: 'und, undefined', args: [undefined], expected: 0 },
{ name: 'obj, null', args: [null], expected:0 },
{ name: 'und, undefined', args: [undefined], expected: NaN },
// strings are bit more interesting, write 7 more test cases with string args
{ name: 'str, undefined', args: ['undefined'], expected: NaN },
{ name: 'str, Infinity', args: ['Infinity'], expected: Infinity },
Expand All @@ -68,19 +69,19 @@ const BooleanTests = [
{ name: 'boo, false', args: [false], expected: false },
// anything but 0 & NaN is cast to true
{ name: 'num, 3', args: [3], expected: true },
{ name: 'num, 0', args: [0], expected: true },
{ name: 'num, 0', args: [0], expected: false },
{ name: 'num, 1e3', args: [1000], expected: true },
{ name: 'num, Infinity', args: [Infinity], expected: false },
{ name: 'num, Infinity', args: [Infinity], expected: true },
{ name: 'num, NaN', args: [NaN], expected: false },
// null & undefined
{ name: 'obj, null', args: [null], expected: true },
{ name: 'und, undefined', args: [undefined], expected: true },
{ name: 'obj, null', args: [null], expected: false },
{ name: 'und, undefined', args: [undefined], expected: false },
// anything but an empty string is cast to true
{ name: 'str, undefined', args: ['undefined'], expected: false },
{ name: 'str, false', args: ['false'], expected: false },
{ name: 'str, undefined', args: ['undefined'], expected: true },
{ name: 'str, false', args: ['false'], expected: true },
{ name: 'str, Infinity', args: ['Infinity'], expected: true },
{ name: 'str, three', args: ['three'], expected: true },
{ name: 'str, ', args: [''], expected: true },
{ name: 'str, ', args: [''], expected: false },
{ name: 'str, 3', args: ['3'], expected: true },
];
Boolean.quizzing = true;
Expand Down
114 changes: 60 additions & 54 deletions module-exercises/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ function tracing1() {
};

// set values in the args to pass the assert
let arg1 = "", arg2 = "", arg3 = "";
let arg1 = "y", arg2 = "x", arg3 = "z";
let returnval = f(arg1, arg2, arg3);
console.assert(returnval === "zyx", "1 a");

arg1 = "", arg2 = "", arg3 = "";
arg1 = "z", arg2 = "x", arg3 = "y";
returnval = f(arg1, arg2, arg3);
console.assert(returnval === "yzx", "1 b");


}
evaluate(tracing1);
Expand All @@ -194,14 +195,15 @@ function tracing2() {
};

// set values in the args to pass the assert
let arg1 = "", arg2 = "", arg3 = "";
let arg1 = "x", arg2 = "y", arg3 = "z";
let returnVal = f(arg1, arg3, arg2);
console.assert(returnVal === "yxz", "returnVal should be yxz");

arg1 = "", arg2 = "", arg3 = "";
arg1 = "y", arg2 = "x", arg3 = "z";
returnVal = f(arg2, arg1, arg3);
console.log(returnVal === "zxy", "returnVal should be zxy");



}
evaluate(tracing2);

Expand All @@ -217,12 +219,12 @@ function tracing3() {
};

// set values in the args to pass the assert
let arg1 = "", arg2 = "", arg3 = "";
let arg1 = "z", arg2 = "x", arg3 = "y";
let returnVal = f(arg1, arg2, arg3);
console.assert(returnVal === "yxz", "returnVal should be yxz");


arg1 = "", arg2 = "", arg3 = "";
arg1 = "z", arg2 = "y", arg3 = "x";
returnVal = f(arg3, arg2, arg1);
console.assert(returnVal === "zyx", "returnVal should be zyx");

Expand All @@ -239,11 +241,11 @@ function tracing4() {
// pass x, y and z to the function in the right order
// don't change their values!
let x = "x", y = "y", z = "z";
let returnVal = f();
let returnVal = f(x,z,y);
console.assert(returnVal === "yxz", "returnVal should be yxz");

x = "x", y = "z", z = "y";
returnVal = f();
returnVal = f(z,x,y);
console.assert(returnVal === "zyx", "returnVal should be zyx");

}
Expand All @@ -260,11 +262,11 @@ function tracing5() {
// pass x, y and z to the function in the right order
// don't change their values!
let x = "x", y = "y", z = "z";
let returnVal = f();
let returnVal = f(z,x,y);
console.assert(returnVal === "xzy", "returnVal should be xzy");

x = "y", y = "x", z = "z";
returnVal = f();
returnVal = f(x,z,y);
console.assert(returnVal === "zyx", "returnVal should be zyx");

}
Expand All @@ -275,7 +277,7 @@ function tracing6() {

// concatinate the params to pass the tests
function f(param1, param2, param3) {
const result = null;
const result = param3+param1+param2;
return result;
};

Expand All @@ -286,7 +288,8 @@ function tracing6() {
arg1 = "z", arg2 = "x", arg3 = "y";
returnVal = f(arg3, arg2, arg1);
console.assert(returnVal === "zyx", "returnVal should be zyx");



}
evaluate(tracing6);

Expand All @@ -295,7 +298,7 @@ function tracing7() {

// concatinate the params to pass the tests
function f(param1, param2, param3) {
const result = null;
const result = param2+ param3+param1;
return result;
};

Expand All @@ -307,23 +310,25 @@ function tracing7() {
returnVal = f(arg3, arg1, arg2);
console.assert(returnVal === "xzy", "returnVal should be xzy");



}
evaluate(tracing7);


function tracing8() {

// arrange the parameters to pass the asserts
function f() {
function f(param1, param2, param3) {
var result = param2 + param1 + param3;
return result;
};

let arg1 = "z", arg2 = "y", arg3 = "x";
let arg1 = "x", arg2 = "y", arg3 = "z";
let returnVal = f(arg1, arg2, arg3);
console.assert(returnVal === "yxz", "returnVal should be yxz");

arg1 = "x", arg2 = "z", arg3 = "y";
arg1 = "x", arg2 = "y", arg3 = "z";
returnVal = f(arg3, arg1, arg2);
console.assert(returnVal === "xzy", "returnVal should be xzy");

Expand All @@ -334,16 +339,16 @@ evaluate(tracing8);
function tracing9() {

// arrange the parameters to pass the asserts
function f() {
function f(param1, param2, param3) {
var result = param1 + param2 + param3;
return result;
};

let arg1 = "y", arg2 = "z", arg3 = "x";
let arg1 = "x", arg2 = "y", arg3 = "z";
let returnVal = f(arg1, arg2, arg3);
console.assert(returnVal === "xyz", "returnVal should be xyz");

arg1 = "z", arg2 = "x", arg3 = "y";
arg1 = "y", arg2 = "z", arg3 = "x";
returnVal = f(arg3, arg1, arg2);
console.assert(returnVal === "xyz", "returnVal should be xyz");

Expand All @@ -354,21 +359,21 @@ evaluate(tracing9);
function tracing10() {

// do what needs to be done!
function f() { // <--
function f(param1, param2, param3, param4) { // <--
var result = param3 + param1 + param2 + param4;
return result;
};

let arg1 = "", arg2 = "", arg3 = "", arg4 = ""; // <--
let arg1 = "y", arg2 = "z", arg3 = "x", arg4 = "w"; // <--
let returnVal = f(arg1, arg2, arg3, arg4);
console.assert(returnVal === "xyzw", "returnVal should be xyzw");

arg1 = "z", arg2 = "w", arg3 = "y", arg4 = "x";
returnVal = f(arg3, arg1, arg4, arg2);
console.assert(returnVal === "", "returnVal should be ?"); // <--
console.assert(returnVal === "xyzw", "returnVal should be ?"); // <--

arg1 = "z", arg2 = "w", arg3 = "y", arg4 = "x";
returnVal = f(); // <--
returnVal = f(arg3, arg2, arg1, arg4); // <--
console.assert(returnVal === "zywx", "returnVal should be zywx");

}
Expand Down Expand Up @@ -431,12 +436,12 @@ evaluate(example2_testCases, exampleTestCases);


const writeTestCases1 = [
{ name: 'first', args: [/* what adds to be 5? */], expected: 5 },
{ name: 'second', args: [/* what else adds to be 5? */], expected: 5 },
{ name: 'third', args: [-2, 2], expected: null }, // what return value do you expect?
{ name: 'fourth', args: [100, 20], expected: null }, // what return value do you expect?
{ name: 'fifth', args: [], expected: null }, // create your own test case!
{ name: 'sixth', args: [], expected: null }, // create your own test case!
{ name: 'first', args: [/* what adds to be 5? */0,5], expected: 5 },
{ name: 'second', args: [/* what else adds to be 5? */2,3], expected: 5 },
{ name: 'third', args: [-2, 2], expected: 0 }, // what return value do you expect?
{ name: 'fourth', args: [100, 20], expected: 120 }, // what return value do you expect?
{ name: 'fifth', args: [8,9], expected: 17 }, // create your own test case!
{ name: 'sixth', args: [4,9], expected: 13 }, // create your own test case!
];
function functionToTest1(a, b) {
const result = a + b;
Expand All @@ -445,12 +450,12 @@ function functionToTest1(a, b) {
evaluate(functionToTest1, writeTestCases1);

const writeTestCases2 = [
{ name: 'first', args: [/* what subtracts to be 5? */], expected: 5 },
{ name: 'second', args: [/* what else subtracts to be 5? */], expected: 5 },
{ name: 'third', args: [10, 2], expected: null }, // what return value do you expect?
{ name: 'fourth', args: [10, 20], expected: null }, // what return value do you expect?
{ name: 'fifth', args: [], expected: null }, // create your own test case!
{ name: 'sixth', args: [], expected: null }, // create your own test case!
{ name: 'first', args: [/* what subtracts to be 5? */7,2], expected: 5 },
{ name: 'second', args: [/* what else subtracts to be 5? */10,5], expected: 5 },
{ name: 'third', args: [10, 2], expected: 8 }, // what return value do you expect?
{ name: 'fourth', args: [10, 20], expected: -10 }, // what return value do you expect?
{ name: 'fifth', args: [20,9], expected: 11 }, // create your own test case!
{ name: 'sixth', args: [8, 9], expected: -1 }, // create your own test case!
];
function functionToTest2(a, b) {
const result = a - b;
Expand All @@ -461,12 +466,12 @@ evaluate(functionToTest2, writeTestCases2);


const writeTestCases3 = [
{ name: 'first', args: [/* what multiplies to be 5? */], expected: 5 },
{ name: 'second', args: [/* what else multiplies to be 5? */], expected: 5 },
{ name: 'third', args: [10, 2], expected: null }, // what return value do you expect?
{ name: 'fourth', args: [10, 20], expected: null }, // what return value do you expect?
{ name: 'fifth', args: [], expected: null }, // create your own test case!
{ name: 'sixth', args: [], expected: null }, // create your own test case!
{ name: 'first', args: [/* what multiplies to be 5? */1,5], expected: 5 },
{ name: 'second', args: [/* what else multiplies to be 5? */-1, -5], expected: 5 },
{ name: 'third', args: [10, 2], expected: 20 }, // what return value do you expect?
{ name: 'fourth', args: [10, 20], expected: 200 }, // what return value do you expect?
{ name: 'fifth', args: [7,9], expected: 63 }, // create your own test case!
{ name: 'sixth', args: [2,9], expected: 18 }, // create your own test case!
];
function functionToTest3(a, b) {
const result = a * b;
Expand All @@ -477,12 +482,13 @@ evaluate(functionToTest3, writeTestCases3);


const writeTestCases4 = [
{ name: 'first', args: [/* what letters in what order will return "zyx"? */], expected: 'zyx' },
{ name: 'second', args: [/* what letters in what order will return "yzx"? */], expected: 'yzx' },
{ name: 'third', args: ['y', 'z', 'x'], expected: null }, // what return value do you expect?
{ name: 'fourth', args: ['x', 'y', 'z'], expected: null }, // what return value do you expect?
{ name: 'fifth', args: [], expected: null }, // create your own test case!
{ name: 'sixth', args: [], expected: null }, // create your own test case!
{ name: 'first', args: ['y','x','z'/* what letters in what order will return "zyx"? */], expected: 'zyx' },
{ name: 'second', args: ['z','x','y'/* what letters in what order will return "yzx"? */], expected: 'yzx' },
{ name: 'third', args: ['y', 'z', 'x'], expected: 'xyz' }, // what return value do you expect?
{ name: 'fourth', args: ['x', 'y', 'z'], expected: 'zxy' }, // what return value do you expect?
{ name: 'fifth', args: ['z','y','x'], expected: 'xzy' }, // create your own test case!
{ name: 'sixth', args: ['x','z','y'], expected: 'yxz' }, // create your own test case!

];
function functionToTest4(a, b, c) {
const result = c + a + b;
Expand All @@ -493,12 +499,12 @@ evaluate(functionToTest4, writeTestCases4);


const writeTestCases5 = [
{ name: 'first', args: [/* what letters in what order will return "zyx"? */], expected: 'zyx' },
{ name: 'second', args: [/* what letters in what order will return "yzx"? */], expected: 'yzx' },
{ name: 'third', args: ['y', 'z', 'x'], expected: null }, // what return value do you expect?
{ name: 'fourth', args: ['x', 'y', 'z'], expected: null }, // what return value do you expect?
{ name: 'fifth', args: [], expected: null }, // create your own test case!
{ name: 'sixth', args: [], expected: null }, // create your own test case!
{ name: 'first', args: ['x','z','y'/* what letters in what order will return "zyx"? */], expected: 'zyx' },
{ name: 'second', args: ['x','y','z'/* what letters in what order will return "yzx"? */], expected: 'yzx' },
{ name: 'third', args: ['y', 'z', 'x'], expected: 'zxy' }, // what return value do you expect?
{ name: 'fourth', args: ['x', 'y', 'z'], expected: 'yzx' }, // what return value do you expect?
{ name: 'fifth', args: ['z','y','x'], expected:'yxz'}, // create your own test case!
{ name: 'sixth', args: ['z','x','y'], expected: 'xyz' }, // create your own test case!
];
function functionToTest5(a, b, c) {
const result = b + c + a;
Expand Down
4 changes: 2 additions & 2 deletions module-exercises/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
<script src="arrays.js"></script>
<script src="objects.js"></script>
<script src="arrays-vs-objects.js"></script>
<script src="block-scope.js"></script>
<!-- <script src="block-scope.js"></script>
<script src="conditional-statements.js"></script>
<script src="statements-vs-expressions.js"></script>
<script src="loops.js"></script>
<script src="for-in-for-of.js"></script>
<script src="errors-and-lifecycle.js"></script>
<script src="errors-and-lifecycle.js"></script> -->


<a href="./challenges/index.html">challenge exercises</a>
Expand Down
Loading