diff --git a/module-exercises/explicit-coercion.js b/module-exercises/explicit-coercion.js index d6bcfd4..83ac486 100644 --- a/module-exercises/explicit-coercion.js +++ b/module-exercises/explicit-coercion.js @@ -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; @@ -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 }, @@ -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; diff --git a/module-exercises/functions.js b/module-exercises/functions.js index f22570a..9524002 100644 --- a/module-exercises/functions.js +++ b/module-exercises/functions.js @@ -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); @@ -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); @@ -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"); @@ -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"); } @@ -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"); } @@ -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; }; @@ -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); @@ -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; }; @@ -307,6 +310,8 @@ function tracing7() { returnVal = f(arg3, arg1, arg2); console.assert(returnVal === "xzy", "returnVal should be xzy"); + + } evaluate(tracing7); @@ -314,16 +319,16 @@ 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"); @@ -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"); @@ -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"); } @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/module-exercises/index.html b/module-exercises/index.html index 2ef651b..0914d7c 100644 --- a/module-exercises/index.html +++ b/module-exercises/index.html @@ -30,12 +30,12 @@ - + challenge exercises diff --git a/module-exercises/primitive-types.js b/module-exercises/primitive-types.js index 2ab5eb0..cefe586 100644 --- a/module-exercises/primitive-types.js +++ b/module-exercises/primitive-types.js @@ -59,27 +59,27 @@ evaluate(example_allValuesHaveAType); // the type of a value is very important to understanding how JS works const typeofTests = [ // boolean values - { name: 'boo, true', args: [true], expected: '' }, - { name: 'boo, false', args: [false], expected: '' }, + { name: 'boo, true', args: [true], expected: 'boolean' }, + { name: 'boo, false', args: [false], expected: 'boolean' }, // null's type is 'null'. just remember, don't try yet to understand - { name: 'obj, true', args: [null], expected: '' }, + { name: 'obj, true', args: [null], expected: 'object' }, // undefined. like with null, there is only one value with this type - { name: 'und, undefined', args: [undefined], expected: '' }, + { name: 'und, undefined', args: [undefined], expected: 'undefined' }, // strings are anything with quotes around it - { name: 'str, ', args: [''], expected: '' }, - { name: 'str, anything with quotes!', args: ['anything with quotes!'], expected: '' }, + { name: 'str, ', args: [''], expected: 'string' }, + { name: 'str, anything with quotes!', args: ['anything with quotes!'], expected: 'string' }, // numbers are a bit more strange and varied { name: 'num, 0.0', args: [0.0], expected: 'number' }, { name: 'num, NaN', args: [NaN], expected: 'number' }, { name: 'num, Infinity', args: [Infinity], expected: 'number' }, { name: 'num, 4', args: [4], expected: 'number' }, // write 6 more passing test cases with expected value 'number' - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, - { name: '', args: null, expected: null }, + { name: 'num, 4', args: [4], expected: 'number' }, + { name: 'num, 2.1', args: [2,1], expected: 'number' }, + { name: 'num, 0.0', args: [0.0], expected: 'number' }, + { name: 'num, NaN', args: [NaN], expected: 'number' }, + { name: 'num, Infinity', args: [Infinity], expected: 'number' }, + { name: 'mun, 0.2', args: [0.2], expected: 'number' }, ] function allValuesHaveAType(value) { return typeof value; @@ -98,8 +98,10 @@ const typeofReturnsAStringTests = [ { name: 'num, 4', args: [4], expected: 'number' }, ]; function typeofReturnsAString(value) { - const typeofValue = typeof value; - return typeof typeofValue; + return typeof value; + // const typeofValue = typeof value; + // return typeof typeofValue; + } typeofReturnsAString.quizzing = true; evaluate(typeofReturnsAString, typeofReturnsAStringTests); @@ -125,16 +127,16 @@ evaluate(example_aBitAboutNaN); // fix the expected values to pass the tests const strictEqualityTests = [ - { name: 'NaN', args: [NaN, NaN], expected: null }, - { name: 'first', args: [true, 'true'], expected: null }, - { name: 'second', args: [1, '1'], expected: null }, - { name: 'third', args: ['1', '1'], expected: null }, - { name: 'fourth', args: [1000, 1e3], expected: null }, - { name: 'fifth', args: [+0, -0], expected: null }, - { name: 'sixth', args: [1, 1.0], expected: null }, - { name: 'seventh', args: ['', ""], expected: null }, - { name: 'eighth', args: ["", ``], expected: null }, - { name: 'ninth', args: [' ', ' '], expected: null }, + { name: 'NaN', args: [NaN, NaN], expected: false }, + { name: 'first', args: [true, 'true'], expected: false }, + { name: 'second', args: [1, '1'], expected: false }, + { name: 'third', args: ['1', '1'], expected: true }, + { name: 'fourth', args: [1000, 1e3], expected: true }, + { name: 'fifth', args: [+0, -0], expected: true }, + { name: 'sixth', args: [1, 1.0], expected: true }, + { name: 'seventh', args: ['', ""], expected: true }, + { name: 'eighth', args: ["", ``], expected: true }, + { name: 'ninth', args: [' ', ' '], expected: false }, ]; function strictEquality(a, b) { // if type OR value are not the same, returns false @@ -147,16 +149,16 @@ evaluate(strictEquality, strictEqualityTests); const strictInequalityTests = [ - { name: 'NaN', args: [NaN, NaN], expected: null }, - { name: 'first', args: [true, 'true'], expected: null }, - { name: 'second', args: [1, '1'], expected: null }, - { name: 'third', args: ['1', '1'], expected: null }, - { name: 'fourth', args: [1000, 1e3], expected: null }, - { name: 'fifth', args: [+0, -0], expected: null }, - { name: 'sixth', args: [1, 1.0], expected: null }, - { name: 'seventh', args: ['', ""], expected: null }, - { name: 'eighth', args: ["", ``], expected: null }, - { name: 'ninth', args: [' ', ' '], expected: null }, + { name: 'NaN', args: [NaN, NaN], expected: true }, + { name: 'first', args: [true, 'true'], expected: true }, + { name: 'second', args: [1, '1'], expected: true }, + { name: 'third', args: ['1', '1'], expected: false }, + { name: 'fourth', args: [1000, 1e3], expected: false }, + { name: 'fifth', args: [+0, -0], expected: false }, + { name: 'sixth', args: [1, 1.0], expected: false }, + { name: 'seventh', args: ['', ""], expected: false }, + { name: 'eighth', args: ["", ``], expected: false }, + { name: 'ninth', args: [' ', ' '], expected: true }, ]; function strictInequality(a, b) { // if type OR value are not the same, returns true diff --git a/module-exercises/truthiness-operators.js b/module-exercises/truthiness-operators.js index 086fa11..f88fcdf 100644 --- a/module-exercises/truthiness-operators.js +++ b/module-exercises/truthiness-operators.js @@ -20,14 +20,14 @@ const orTests = [ { name: '"true", NaN', args: ["true", NaN], expected: "true" }, { name: 'NaN, NaN', args: [NaN, NaN], expected: NaN }, // complete these test cases - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, + { name: 'null,1', args:[null, 1], expected: 1 }, + { name: 'NaN,1', args:[NaN,1], expected: 1 }, + { name: 'NaN, true', args:[NaN, true], expected: true }, + { name: '0,undefined', args:[0, undefined] , expected: undefined }, + { name: 'NaN, null', args: [NaN, null], expected: null }, + { name: '0,null', args: [0, null], expected:null }, + { name: 'NaN, false', args:[NaN, false], expected: false }, + { name: '1,0', args: [1,0], expected: 1 }, ]; function or(a, b) { return a || b; @@ -46,14 +46,14 @@ const andTests = [ { name: '"true", NaN', args: ["true", NaN], expected: NaN }, { name: 'NaN, NaN', args: [NaN, NaN], expected: NaN }, // complete these test cases - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, - { name: null, args: null, expected: null }, + { name: 'null,null', args: [null,null], expected: null }, + { name: '0,null', args:[0, null], expected: 0 }, + { name: 'null,NaN', args:[null, nan] , expected: null }, + { name: 'null,undefined', args:[null, undefined], expected: null }, + { name: 'undefined,1', args:[undefined,1], expected: undefined }, + { name: '0,NaN', args:[0, NaN], expected: 0 }, + { name: 'NaN, null', args:[NaN, null], expected: NaN }, + { name: 'NaN, NaN', args: [NaN, NaN], expected: NaN }, ]; function and(a, b) { return a && b; diff --git a/module-exercises/variables.js b/module-exercises/variables.js index 0ed91de..186e69c 100644 --- a/module-exercises/variables.js +++ b/module-exercises/variables.js @@ -46,24 +46,26 @@ evaluate(example_declarationAndAssignment); function example_twoVariableSwap() { - // swapping the values stored in two variables is a key skill - // once you get it, it's quite simple - // if you don't get it, programming will be very confusing - // so take some time now to understand how variables work - // how they store values in memory - // that the "=" sign does not work like in math - // what happens when one variable is assigned to another - // that variable assignments go from right to left - // that program memory changes over time - // what is written in source code will not be true forever +// swapping the values stored in two variables is a key skill +// once you get it, it's quite simple +// if you don't get it, programming will be very confusing +// so take some time now to understand how variables work +// how they store values in memory +// that the "=" sign does not work like in math +// what happens when one variable is assigned to another +// that variable assignments go from right to left +// that program memory changes over time +// what is written in source code will not be true forever/ + let a = 'b', b = 'a'; let temp = ''; - temp = a; - a = b; - b = temp; + temp=a; + a=b; + b=temp; + console.assert(a === 'a', 'a should store "a"'); console.assert(b === 'b', 'b should store "b"'); @@ -82,7 +84,11 @@ function threeVariableSwap1() { let temp = ''; // can be done in 4 lines - + temp=a + a=b; + b=c; + c=temp; + console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); @@ -96,7 +102,11 @@ function threeVariableSwap2() { let temp = ''; // can be done in 4 lines - + temp=a; + a=c; + c=b; + b=temp; + console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); @@ -111,7 +121,11 @@ function fourVariableSwap1() { // can be done in 5 lines - + temp=a; + a=b; + b=c; + c=d; + d=temp; console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); console.assert(c === "c", "c should store 'c'"); @@ -125,7 +139,12 @@ function fourVariableSwap2() { let temp = ''; // can be done in 6 lines - + temp=a; + a=d; + d=b; + b=c; + c=d; + d=temp; console.assert(a === "w", "a should store 'w'"); console.assert(b === "x", "b should store 'x'"); @@ -141,6 +160,13 @@ function fiveVariableSwap() { // can be done in 6 lines + temp=a; + a=e; + e=b; + b=d; + d=e; + e=temp; + console.assert(a === "v", "a should store 'v'"); console.assert(b === "w", "b should store 'w'"); @@ -190,6 +216,8 @@ function multipleAssignments1() { // can be done in 1 line +temp=a; a=b; b=c; c=temp; + console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); @@ -203,7 +231,7 @@ function multipleAssignments2() { let temp = ''; // can be done in 1 line - + temp=c; c=b; b=a; a=temp; console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); @@ -218,6 +246,7 @@ function multipleAssignments3() { // can be done in 1 line + temp=a; a=b; b=c; c=d; d=temp; console.assert(a === "a", "a should store 'a'"); console.assert(b === "b", "b should store 'b'"); @@ -233,6 +262,7 @@ function multipleAssignments4() { // can be done in 1 line +temp=a; a=d; d=temp; temp=c; c=b; b=temp; console.assert(a === "w", "a should store 'w'"); console.assert(b === "x", "b should store 'x'"); @@ -269,6 +299,9 @@ function chainedAssignments1() { let temp = ''; // can be done in 3 lines or less + temp=b; + b=a1=a2; + a1=a2=temp; console.assert(a1 === "a", 'a1 should store "a"'); console.assert(a1 === a2, 'a1 should store the same value as a2'); @@ -286,7 +319,10 @@ function chainedAssignments2() { // can be done in 4 lines or less - + temp=a, + a=b1=b2, + b1=b2=c1, + c1=c2=c3=temp; console.assert(a === "a", 'a should store "a"'); console.assert(b1 === "b", 'b1 should store "b"'); diff --git a/week-2-project/index.html b/week-2-project/index.html index 8badc8c..0759a52 100644 --- a/week-2-project/index.html +++ b/week-2-project/index.html @@ -7,15 +7,19 @@