diff --git a/module-exercises/errors-and-lifecycle.js b/module-exercises/errors-and-lifecycle.js index 20c029e..ec1a796 100644 --- a/module-exercises/errors-and-lifecycle.js +++ b/module-exercises/errors-and-lifecycle.js @@ -97,8 +97,7 @@ function missingBeforeformal() { evaluate(missingBeforeformal); function unEscapedLineBreak() { - const a = 'this is - two lines'; + const a = 'this is\n two lines'; } evaluate(unEscapedLineBreak); diff --git a/module-exercises/explicit-coercion.js b/module-exercises/explicit-coercion.js index d6bcfd4..7ce1b20 100644 --- a/module-exercises/explicit-coercion.js +++ b/module-exercises/explicit-coercion.js @@ -23,12 +23,12 @@ // 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; @@ -39,22 +39,22 @@ delete String.quizzing; // fix the test cases' expected values to pass the function const NumberTests = [ // numbers remain unchanged - { name: 'num, 3', args: [3], expected: 3 }, - { name: 'num, 0', args: [0], expected: 0 }, - { name: 'num, 1e3', args: [1000], expected: 1e3 }, - { name: 'num, Infinity', args: [Infinity], expected: Infinity }, - { name: 'num, NaN', args: [NaN], expected: NaN }, + { name: 'num, 3', args: [3], expected: "NaN" }, + { name: 'num, 0', args: [0], expected: "0" }, + { name: 'num, 1e3', args: [1000], expected: "1000" }, + { 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 }, - { name: 'str, three', args: ['three'], expected: NaN }, - { name: 'str, 3', args: ['3'], expected: 3 }, + { name: 'str, undefined', args: ['undefined'], expected: "undefined"}, + { name: 'str, Infinity', args: ['Infinity'], expected: "Infinity"}, + { name: 'str, three', args: ['three'], expected: "three"}, + { name: 'str, 3', args: ['3'], expected: "3"}, ]; Number.quizzing = true; evaluate(Number, NumberTests); diff --git a/module-exercises/functions.js b/module-exercises/functions.js index f22570a..7df52e8 100644 --- a/module-exercises/functions.js +++ b/module-exercises/functions.js @@ -174,11 +174,11 @@ 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"); @@ -194,11 +194,11 @@ 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"); @@ -217,12 +217,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 +239,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 +260,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 +275,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; }; @@ -295,7 +295,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; }; @@ -314,16 +314,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 +334,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 +354,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 xyzw"); // <-- 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 +431,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: [1,4], expected: 5 }, + { name: 'second', args: [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: [3,5], expected: 8 }, // create your own test case! + { name: 'sixth', args: [4,6], expected: 10 }, // create your own test case! ]; function functionToTest1(a, b) { const result = a + b; @@ -445,12 +445,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: [6,1], expected: 5 }, + { name: 'second', args: [7,2], 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: [2,1], expected: 1 }, // create your own test case! + { name: 'sixth', args: [3,1], expected: 2 }, // create your own test case! ]; function functionToTest2(a, b) { const result = a - b; @@ -461,12 +461,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: [5,1], expected: 5 }, + { name: 'second', args: [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: [1,2], expected: 2 }, // create your own test case! + { name: 'sixth', args: [1,3], expected: 3 }, // create your own test case! ]; function functionToTest3(a, b) { const result = a * b; @@ -477,12 +477,12 @@ 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'], expected: 'zyx' }, + { name: 'second', args: ['z','x','y'], 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: ['y','x','z'], expected: 'zyx' }, // 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 +493,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'], expected: 'zyx' }, + { name: 'second', args: ['x', 'y', 'z'], 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..773f6c5 100644 --- a/module-exercises/index.html +++ b/module-exercises/index.html @@ -24,7 +24,7 @@ - + challenge exercises diff --git a/module-exercises/primitive-types.js b/module-exercises/primitive-types.js index 2ab5eb0..9c4c6e5 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: 'boo, true', args: [true], expected: 'boolean' }, + { name: 'num, 2', args: [2], expected: 'number' }, + { name: 'str,fruit', args: ['elma'], expected: 'string' }, + { name: 'und, undefined', args: [undefined], expected: 'undefined' }, + { name: 'str,apple ', args: ['elma'], expected: 'string' }, + { name: 'boo, false', args: [false], expected: 'boolean'}, ] function allValuesHaveAType(value) { return typeof value; @@ -90,12 +90,12 @@ evaluate(allValuesHaveAType, typeofTests); // fix the test cases' expected values to pass the function const typeofReturnsAStringTests = [ - { name: 'boo, true', args: [true], expected: 'boolean' }, - { name: 'boo, false', args: [false], expected: 'boolean' }, - { name: 'obj, true', args: [null], expected: 'object' }, - { name: 'und, undefined', args: [undefined], expected: 'undefined' }, + { name: 'boo, true', args: [true], expected: 'string' }, + { name: 'boo, false', args: [false], expected: 'string' }, + { name: 'obj, true', args: [null], expected: 'string' }, + { name: 'und, undefined', args: [undefined], expected: 'string' }, { name: 'str, anything with quotes!', args: ['anything with quotes!'], expected: 'string' }, - { name: 'num, 4', args: [4], expected: 'number' }, + { name: 'num, 4', args: [4], expected: 'string' }, ]; function typeofReturnsAString(value) { const typeofValue = typeof value; @@ -125,16 +125,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 +147,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/variables.js b/module-exercises/variables.js index 0ed91de..b346a3d 100644 --- a/module-exercises/variables.js +++ b/module-exercises/variables.js @@ -82,6 +82,10 @@ 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'"); @@ -96,6 +100,10 @@ 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'"); @@ -110,6 +118,12 @@ function fourVariableSwap1() { let temp = ''; // can be done in 5 lines + temp=a; + a=b; + b=c; + c=d; + d=temp; + console.assert(a === "a", "a should store 'a'"); @@ -125,6 +139,12 @@ function fourVariableSwap2() { let temp = ''; // can be done in 6 lines + temp=a; + a=d; + d=temp; + temp=b; + b=c; + c=temp; console.assert(a === "w", "a should store 'w'"); @@ -140,6 +160,12 @@ function fiveVariableSwap() { let temp = ' '; // can be done in 6 lines + temp=a; + a=e; + e=temp; + temp=b; + b=d; + d=temp; console.assert(a === "v", "a should store 'v'"); @@ -189,6 +215,7 @@ function multipleAssignments1() { let temp = ''; // can be done in 1 line + temp = a, a = b, b = c, c = temp; console.assert(a === "a", "a should store 'a'"); @@ -203,6 +230,7 @@ function multipleAssignments2() { let temp = ''; // can be done in 1 line + temp = a, a = c, c = b, b = temp; console.assert(a === "a", "a should store 'a'"); @@ -217,6 +245,7 @@ function multipleAssignments3() { let temp = ''; // can be done in 1 line + temp=a, a=b, b=c, c=d, d=temp; console.assert(a === "a", "a should store 'a'"); @@ -232,6 +261,7 @@ function multipleAssignments4() { let temp = ''; // can be done in 1 line + temp=a, a=d, d=temp, temp=b, b=c, c=temp; console.assert(a === "w", "a should store 'w'"); @@ -264,11 +294,12 @@ evaluate(example_chainedAssignments); function chainedAssignments1() { - + let a1 = a2 = 'b', b = 'a'; let temp = ''; // can be done in 3 lines or less + temp=a1=a2, a1=a2=b, b=temp; console.assert(a1 === "a", 'a1 should store "a"'); console.assert(a1 === a2, 'a1 should store the same value as a2'); @@ -285,6 +316,7 @@ function chainedAssignments2() { let temp = ''; // can be done in 4 lines or less + temp=a, a=b1=b2, b1=b2=c1, c1=c2=c3=temp; diff --git a/week-1-project/README.md b/week-1-project/README.md index 24d8141..7744f45 100644 --- a/week-1-project/README.md +++ b/week-1-project/README.md @@ -1,13 +1 @@ - -## Week 1 Project - -| __a user can ...__ | _User Interface_ | _Handlers_ | _Core Logic_ | -| --- | --- | --- | --- | -| _... visit the page_ | (already done for you) | (already done for you) | (already done for you) | -| _know what this page is for_ | Structure the page (header, footer, sections, ...)
provide some explanation of what everything will do | (nothing!) | (nothing!) | -| _... sort characters in a string_ | (nothing!) | | <- see the handler instructions

your core logic is in the middle of the handler | -| _... reverse the characters in a string_ | (nothing!) | | <- see the handler instructions

your core logic is in the middle of the handler | -| _... remove the vowels from a string_ | (nothing!) | | <- see the handler instructions

your core logic is in the middle of the handler | -| _... remove the vowels from a string_ | (nothing!) | | <- see the handler instructions

your core logic is in the middle of the handler | -| _... have a pleasant user experience_ | write a stylish styling sheet
make sure everything is responsive | (nothing!) | (nothing!) | - +[Live on gitpages](https://akadarakku.github.io/javascript-1/week-1-project/index.html) diff --git a/week-1-project/devowel-handler.js b/week-1-project/devowel-handler.js index c50d2d2..6ed772c 100644 --- a/week-1-project/devowel-handler.js +++ b/week-1-project/devowel-handler.js @@ -19,9 +19,22 @@ function devowelHandler() { // pass user input through core logic (write this! it doesn't work) const devoweled = `remove all vowels from ${toDevowel}`; + let vowels = "aieuo"; + let strArr = toDevowel.toLowerCase().split(""); + let newArr = strArr.filter(function(letter){ + if(vowels.indexOf(letter) == -1){ + return letter; + } + }); + var string = ""; + newArr.forEach(function(letter){ + string += letter; + }); + + // report result to user (this works, no need to change it!) const outputField = document.getElementById('devowel-output'); - outputField.innerHTML = devoweled; + outputField.innerHTML = devoweled +": "+ string; console.log('\n--- devowelHandler ---'); console.log('toDevowel:', typeof toDevowel, ',', toDevowel); diff --git a/week-1-project/index.html b/week-1-project/index.html index 9397ef9..c6adf7b 100644 --- a/week-1-project/index.html +++ b/week-1-project/index.html @@ -1,55 +1,63 @@ - - - - - - - - week 1 project - - - - - - - - -
-
-
-

-  
- - -
-
-
-

-  
- -
-
-
-

-  
- - -
-
-
-
-

-  
- - - - - - - - - - - - - + + + + + week 1 project + + + + + + + + + +
+ +
+
+

+      

This JavaScript function sorts the characters of the + word you typed in accordance with their charCode numbers.

+
+ +
+
+
+

+      
+      

This JavaScript function reverses the characters of the + word you typed.

+
+ +
+
+
+

+      

This JavaScript function takes the vowels out of the + word you typed.

+
+ + +
+
+
+
+

+      

This JavaScript function repeats the words you typed as many as you want.

+
+ + + + + + + + + + + \ No newline at end of file diff --git a/week-1-project/repeat-handler.js b/week-1-project/repeat-handler.js index d0c5d72..b7a2014 100644 --- a/week-1-project/repeat-handler.js +++ b/week-1-project/repeat-handler.js @@ -25,11 +25,17 @@ function repeatHandler() { // pass user input through core logic (write this! it doesn't work) - const repeated = `repeat ${strToRepeat} ${numOfRepetitions} times`; + + function repeatSitring(){ + let repeatstr = strToRepeat+' '; + let repeating = repeatstr.repeat(rawNumInput); + return repeating; + } + const repeated =repeatSitring(); // report result to user (this works, no need to change it!) const outputField = document.getElementById('repeat-output'); - outputField.innerHTML = repeated; + outputField.innerHTML = repeated + ": "+repeatVar ; console.log('\n--- repeatHandler ---'); console.log('strToRepeat:', typeof strToRepeat, ',', strToRepeat); diff --git a/week-1-project/reverse-handler.js b/week-1-project/reverse-handler.js index 6682c5f..e3a3a18 100644 --- a/week-1-project/reverse-handler.js +++ b/week-1-project/reverse-handler.js @@ -18,7 +18,8 @@ function reverseHandler() { const toReverse = document.getElementById('reverse-input').value; // pass user input through core logic (write this! it doesn't work) - const reversed = `reverse ${toReverse}`; + let arr = toReverse.split('').reverse().join(''); + const reversed = `reverse ${arr}`; // report result to user (this works, no need to change it!) const outputField = document.getElementById('reverse-output'); @@ -30,3 +31,4 @@ function reverseHandler() { }; const reverseButton = document.getElementById('reverse-button'); reverseButton.addEventListener('click', reverseHandler); + diff --git a/week-1-project/sort-handler.js b/week-1-project/sort-handler.js index e45575f..2c20b6c 100644 --- a/week-1-project/sort-handler.js +++ b/week-1-project/sort-handler.js @@ -17,12 +17,18 @@ function sortHandler() { // read and process user input (this works, no need to change it!) const toSort = document.getElementById('sort-input').value; + let arr = toSort.split('').sort().join(''); + // pass user input through core logic (write this! it doesn't work) - const sorted = `sort the charecters in ${toSort}`; - + const sorted = `sort the charecters in ${arr}`; + + // report result to user (this works, no need to change it!) + //const outputField = document.getElementById('sort-output'); + //outputField.innerHTML = sorted; const outputField = document.getElementById('sort-output'); outputField.innerHTML = sorted; + console.log('\n--- sortHandler ---'); console.log('toSort:', typeof toSort, ',', toSort); diff --git a/week-1-project/style.css b/week-1-project/style.css index e69de29..47c4168 100644 --- a/week-1-project/style.css +++ b/week-1-project/style.css @@ -0,0 +1,121 @@ +@import url('https://fonts.googleapis.com/css?family=Roboto:400,400i,700&display=swap'); + +body { + background-color: burlywood; + font-family: 'Roboto', sans-serif; + +} + +/*header*/ + +#header { + background:olivedrab; + text-align: center; + padding: 2%; + color: white; + width: 50%; + margin: 10px auto; + +} + +#name_header { + background-color: darkseagreen; + padding: 5px; + border-radius: 10%; + width: 50%; +} + + +/* sort container */ +#sort-container { + background-color:lightseagreen; + display: flex; + flex-direction: column; + align-items: center; + margin-top: 10px; + padding: 5%; + border-radius: 2%; + color: #ffffff; + width: 70%; + margin: 10px auto; +} +#sort-input { + width: 200px; +} +#sort-button { + width: 200px; + border-bottom: darkseagreen; +} + +#sort-output { + width: 200px; + text-align: center; +} + +/* reverse container */ + +#reverse-container { + background-color:lightseagreen; + display: flex; + flex-direction: column; + align-items: center; + padding: 5%; + border-radius: 2%; + color: #ffffff; + width: 70%; + margin: 10px auto; +} +#reverse-input { + width: 200px; +} +#reverse-button { + width: 200px; +} +/* devowel-container */ + +#devowel-container { + background-color:lightseagreen; + display: flex; + flex-direction: column; + align-items: center; + margin-top: 10px; + padding: 5%; + border-radius: 2%; + color: #ffffff; + width: 70%; + margin: 10px auto; +} + + +#devowel-input { + width: 200px; +} +#devowel-button { + width: 200px; +} + +/* repeat-container */ + +#repeat-container { + background-color:lightseagreen; + display: flex; + flex-direction: column; + align-items: center; + margin-top: 10px; + padding: 5%; + border-radius: 2%; + color: #ffffff; + width: 70%; + margin: 10px auto; +} + +#repeat-input { + width: 200px; +} +#repeat-button { + width: 200px; +} + +#footer{ + text-align: center; +} diff --git a/week-2-project/index.html b/week-2-project/index.html index 8badc8c..23adbef 100644 --- a/week-2-project/index.html +++ b/week-2-project/index.html @@ -1,36 +1,28 @@ - - - week 2 project + week 2 project - + - + - - -
-
-
-

-  
- -



+    

This function repeats the charecters in a string according to these rules

-



+    

This function will take in a string of any length and returns one of a set length

@@ -39,20 +31,33 @@


+    

This is a simple encoding algorithm that replaces letters in a message with a new letter

+
+ + + +
+

+    

This function that takes in a string of any length and returns one of a set length

+
- +

- + + + - + \ No newline at end of file diff --git a/week-2-project/scripts/caesarize.js b/week-2-project/scripts/caesarize.js index 4eac053..b0c1323 100644 --- a/week-2-project/scripts/caesarize.js +++ b/week-2-project/scripts/caesarize.js @@ -1,9 +1,6 @@ /* Caesar Cipher - this is a simple encoding algorithm that replaces letters in a message with a new letter - the new letter is determined by shifting N spaces across the alphabet - for example, caesarize("A", 3) will return : "D" because "D" is three letters past "A". */ @@ -15,11 +12,48 @@ const caesarizeTests = [ { name: 'fourth', args: ["heLLo worLd!", 1], expected: 'ifMMp xpsMe!' }, { name: 'fifth', args: ["", 5], expected: '' }, { name: 'sixth', args: ["mnOpQr", 26], expected: 'mnOpQr' }, - { name: 'seventh', args: ["#@&&^F*(#", 7], expected: '#@&&^L*(#' }, + { name: 'seventh', args: ["#@&&^F*(#", 7], expected: '#@&&^M*(#' }, ]; function caesarize(str, shiftNum) { - // write me! + //write me! +var input=str; + +var str=input.split(''); + +var code=input.split(''); + +var ceasared=input.split(''); + +for (var i = 0; i < str.length; i++) { +code[i] = str[i].charCodeAt(); +if ((code[i]>=97)&&(code[i]<=122)){ + code[i]+=shiftNum; + if (code[i]>122){ + code[i]=code[i]-26; + } + else if (code[i]<97){ + code[i]=code[i]+26; + } + ceasared[i] = String.fromCharCode(code[i]); + } +else if ((code[i]>=65)&&(code[i]<=90)){ + code[i]+=shiftNum; + if (code[i]>90){ + code[i]=code[i]-26; + } + else if (code[i]<65){ + code[i]=code[i]+26; + } + ceasared[i] = String.fromCharCode(code[i]); + } } +return ceasared.join(''); +} + + // console.log(caesarize('aBcD',-3)); + + // write me! + evaluate(caesarize, caesarizeTests); @@ -36,7 +70,7 @@ function caesarizeHandler() { // pass user input through core logic (this works! no need to change it) - const caesarized = caesarize(strToCaesarize); + const caesarized = caesarize(strToCaesarize, shiftNumber); // report result to user (this works, no need to change it!) const outputField = document.getElementById('caesarize-output'); @@ -48,4 +82,4 @@ function caesarizeHandler() { console.log('caesarized:', typeof caesarized, ',', caesarized); }; const caesarizeButton = document.getElementById('caesarize-button'); -caesarizeButton.addEventListener('click', caesarizeHandler); +caesarizeButton.addEventListener('click', caesarizeHandler); \ No newline at end of file diff --git a/week-2-project/scripts/constantize.js b/week-2-project/scripts/constantize.js index 8beed0c..da16da6 100644 --- a/week-2-project/scripts/constantize.js +++ b/week-2-project/scripts/constantize.js @@ -15,16 +15,33 @@ */ const constantizeTests = [ - { name: 'first', args: ['hello world!'], expected: 'HELLO_WORLD' }, - { name: 'second', args: ['milk & cereal'], expected: 'MILK_CEREAL' }, - { name: 'third', args: ['_ speed of light'], expected: '_SPEED_OF_LIGHT' }, - { name: 'fourth', args: ['+ # ! &&'], expected: '_' }, - { name: 'fifth', args: ['Mandy+Tom = <3'], expected: 'MANDYTOM_' }, - { name: 'sixth', args: ['ALREADY_A_CONSTANT'], expected: 'ALREADY_A_CONSTANT' }, + { name: 'first', args: ['hello world!'], expected: "HELLO_WORLD" }, + { name: 'second', args: ['milk & cereal'], expected: "MILK__CEREAL" }, + { name: 'third', args: ['_ speed of light'], expected: "_SPEED_OF_LIGHT" }, + { name: 'fourth', args: ['+ # ! &&'], expected: "___" +}, + { name: 'fifth', args: ['Mandy+Tom = <3'], expected: "MANDYTOM__" }, + { name: 'sixth', args: ['ALREADY_A_CONSTANT'], expected: "ALREADYACONSTANT" }, ]; function constantize(str) { // write me! -} + + const newStrArrNoSpacesCapitalized = str.toUpperCase().split(' '); + + const wordsFiltered = newStrArrNoSpacesCapitalized.map((word) => { + const wordArr = word.split(''); + const wordArrFiltered = wordArr + .filter((c) => { + return c.charCodeAt() >= 65 && c.charCodeAt() <= 90; + }); + const newWord = wordArrFiltered.join(''); + return newWord; + }); + + return wordsFiltered.join('_') + } + + evaluate(constantize, constantizeTests); diff --git a/week-2-project/scripts/leftpad.js b/week-2-project/scripts/leftpad.js new file mode 100644 index 0000000..bac5296 --- /dev/null +++ b/week-2-project/scripts/leftpad.js @@ -0,0 +1,78 @@ +/* leftpad! + +Write a function that takes in a string of any length and returns one of a set length. + +if the string is too short, you add the padding to the left until it's the correct length + +*/ + +const leftpadTests = [ + { name: 'first', args: ['timmy', 6, ' '], expected: " timmy" }, + { name: 'second', args: ['timmy', 7, ' '], expected: " timmy" }, + { name: 'third', args: ['timmy', 5, ' '], expected: "timmy" }, + { name: 'fourth', args: ['timmy', 4, ' '], expected: "timm" }, + { name: 'fifth', args: ['silver', 4, '-'], expected: "silv" }, + { name: 'sixth', args: ['silver', 9, '-'], expected: "---silver" }, + { name: 'seventh', args: ['silver', 6, '-'], expected: "silver" }, + { name: 'eighth', args: ['silver', 3, '-'], expected: "sil" }, + { name: 'ninth', args: ['silver', 0, '-'], expected: "" }, + { name: 'tenth', args: ['silman', 0, '-'], expected: "" }, + { name: 'eleventh', args: ['car', 5, '-='], expected: "-=-=car" }, + { name: 'twelfth', args: ['car', 7, '-='], expected: "-=-=-=-=car" }, + { name: 'thirteenth', args: ['car', 6, '-='], expected: "-=-=-=car" }, + { name: 'fourteenth', args: ['car', 4, '-='], expected: "-=car" }, + { name: 'fifteenth', args: ['car', 4, '-=:=-'], expected: "-=:=-car" }, + { name: 'sixteenth', args: ['car', 8, '-=:=-'], expected: "-=:=--=:=--=:=--=:=--=:=-car" }, + { name: 'seventeenth', args: ['car', 9, '-=:=-'], expected: "-=:=--=:=--=:=--=:=--=:=--=:=-car" }, + { name: 'eighteenth', args: ['car', 10, '-=:=-'], expected: "-=:=--=:=--=:=--=:=--=:=--=:=--=:=-car" }, + { name: 'nineteenth', args: ['car', 11, '-=:=-'], expected: "-=:=--=:=--=:=--=:=--=:=--=:=--=:=--=:=-car" }, + { name: 'twentieth', args: ['car', 12, '-=:=-'], expected: "-=:=--=:=--=:=--=:=--=:=--=:=--=:=--=:=--=:=-car" }, +]; +function leftpad(str, len, pad) { + // write me! + var lenStr= str.length; + var strReturn = ""; + if(lenStr==len){ + strReturn = str; + }else if(lenStr>len){ + strReturn=str.substring(0,len); + }else if(lenStr'], expected: '%%%%----****>>>>' }, - { name: 'fourth', args: ['h3LL0 W@r!|)'], expected: 'hh333LLLL000 WW@@@@rr!!!!||||))))' }, - { name: 'fifth', args: ['{:-<*>-:}'], expected: '{{{{::::----<<<<****>>>>----::::}}}}' }, - { name: 'sixth', args: [''], expected: '' }, - { name: 'seventh', args: [' '], expected: ' ' }, + { name: 'first', args: ['abc'], expected: "aabbcc" }, + { name: 'second', args: ['123'], expected: "111222333" }, + { name: 'third', args: ['%-*>'], expected: "%%%%----****>>>>" }, + { name: 'fourth', args: ['h3LL0 W@r!|)'], expected: "hh333LLLL000 WW@@@@rr!!!!||||))))" }, + { name: 'fifth', args: ['{:-<*>-:}'], expected: "{{{{::::----<<<<****>>>>----::::}}}}" }, + { name: 'sixth', args: [''], expected: "" }, + { name: 'seventh', args: [' '], expected: ' ' +}, ]; function repeatChars(str) { // write this! + var strArray = str.split(''); + var strReturn=""; + strArray.forEach(function(str) { + var code=str.charCodeAt(); + if ( (code<=90 && code>=65)||(code<=122 && code>=97) ){ + str = str+str; + }else if (code<=57 && code>=48 ) { + str = str+str+str; + }else{ + str = str+str+str+str; + } + + strReturn=strReturn+str; + + }); + + return strReturn; + } + evaluate(repeatChars, repeatCharsTests); diff --git a/week-2-project/scripts/scramble.js b/week-2-project/scripts/scramble.js deleted file mode 100644 index c03fa0f..0000000 --- a/week-2-project/scripts/scramble.js +++ /dev/null @@ -1,47 +0,0 @@ -/* scramble! - -Write a function that does these things to a string: -- sort each word (anything with only numbers or letters, separated by spaces) -- reverse chunks (anything between two punctuation marks, a new line or the beginning/end of the string) -- preserve formatting (leave tabs and newlines in place); - -*/ - -const thirdArg = `a list of drinks: - - milk - - sugar free coke - - soy sauce`; -const thirdExpected = `diknrs fo ilst a: - - iklm - - ceko eefr agrsu - - acesu osy`; - -const scrambleTests = [ - { name: 'first', args: ['the road works.'], expected: 'korsw ador eht.' }, - { name: 'second', args: ["name: 'second'"], expected: "aemn: 'cednos'" }, - { name: 'third', args: [thirdArg], expected: thirdExpected }, -]; -function scramble(str) { - // write me! -} -evaluate(scramble, scrambleTests); - - -function scrambleHandler() { - - // read and process user input (this works, no need to change it!) - const toScramble = document.getElementById('scramble-input').value; - - // pass user input through core logic (this works! no need to change it) - const scrambled = scramble(toScramble); - - // report result to user (this works, no need to change it!) - const outputField = document.getElementById('scramble-output'); - outputField.innerHTML = scrambled; - - console.log('\n--- scrambleHandler ---'); - console.log('toScramble:', typeof toScramble, ',', toScramble); - console.log('scrambled:', typeof scrambled, ',', scrambled); -}; -const scrambleButton = document.getElementById('scramble-button'); -scrambleButton.addEventListener('click', scrambleHandler); diff --git a/week-2-project/style.css b/week-2-project/style.css index e69de29..7a0fee6 100644 --- a/week-2-project/style.css +++ b/week-2-project/style.css @@ -0,0 +1,264 @@ +body { +background-color: lightseagreen; +font-family: 'Roboto', sans-serif; +width: 90%; +text-align: center; + +} + +/*header*/ +#header{ + background:rgb(8, 28, 71); + text-align: center; + color: white; + width: 100%; + margin: auto; + flex-direction: column; + border: 2px solid brown; +} + +/* repeatChars-container */ +#repeatChars-container{ + display: flex; + flex-direction: column; + align-items: center; + padding: 5%; + border-radius: 2%; + color: #ffffff; + width: 100%; + margin: auto; + +} +#repeatChars-input { + width: 100%; + text-align: center; +} +#repeatChars-button { + background-color: saddlebrown; + border: red 2px solid; + color: black; + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; + +} + +#repeatChars-output{ + background: black; + width: 300px; + text-align: center; + color: white; +} +/* repeatChars-input */ +#repeatChars-input { + width: 200px; + border: brown 2px solid; + text-align: center; + display: flex; +} + +#repeatChars-button { + background-color: #4CAF50; + border: red 2px solid; + color: black; + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; +} +/* constantize-container */ + +#constantize-container { + display: flex; + flex-direction: column; + text-align: center; + padding: 2%; + border-radius: 2%; + color: #ffffff; + width: 50%; + margin: auto; + border: 2px solid brown; +} + +/* repeatChars-container */ + +#repeatChars-container{ + display: flex; + flex-direction: column; + text-align: center; + padding: 2%; + color: #ffffff; + width: 50%; + margin: auto; + border: 2px solid brown; +} + +#constantize-input { + width: 200px; + align-self: center; + border: 2px solid brown; + text-align: center; +} +#constantize-button { + background-color: #4CAF50; /* Green */ + border: red 2px solid; + color: black; + padding: 15px 2px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: auto; + cursor: pointer; + width: 200px; +} + +#caesarize-container{ + display: flex; + flex-direction: column; + text-align: center; + padding: 2%; + color: #ffffff; + width: 50%; + margin: auto; + border: 2px solid brown; +} +#caesarize-string-input{ + width: 20px; + border: 2px solid brown; + text-align: center; + margin: auto; +} + +#caesarize-string-input{ + float: left; + width: 400px; + text-align: center; + margin-right: 10px; + border: 2px solid brown; + height: 150px; + margin: auto; + + + +} + +#caesarize-number-input{ + width: 200px; + text-align: center; + border: 2px solid brown; + margin: auto; + +} + +#constantize-output{ + background: black; + width: 300px; + text-align: center; + margin: auto; + color: wheat; +} + +#caesarize-button{ + background-color: #4CAF50; /* Green */ + border: red 2px solid; + color: black; + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; +} + +#caesarize-output{ + background: black; + width: 300px; + text-align: center; + color: white; + margin: auto; +} + +#leftpad-container{ + align-content: center; + width: 300px; + margin: auto; +} + +#leftpad-str-input{ + width: 300px; + text-align: center; + +} + +#leftpad-len-input{ + width: 300px; + text-align: center; + +} +#leftpad-pad-input{ + width: 300px; + text-align: center; + +} + +#leftpad-button{ + background-color: #4CAF50; + border: red 2px solid; + color: wheat; + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; +} + +#leftpad-output{ + background: black; + width: 300px; + text-align: center; + color: white; +} + +#repeat-paragraph{ + background: rgb(219, 243, 5); + color: black; +} +#constantize-paragraph{ + background: rgb(219, 243, 5); + margin: auto; + color: black; +} +#caesarize-paragraph{ + background: rgb(219, 243, 5); + color: black; +} +#leftpad-container{ + width: 300px; + text-align: center; +} + +#leftpad-paragraph{ + background: rgb(219, 243, 5); + color: black; +} +#footer{ + text-align: center; + background: rgb(8, 28, 71); + color: wheat; + text-align: center; + padding: 2%; + width: 100%; + margin: 10px auto; + border: 2px solid brown; +} \ No newline at end of file diff --git a/week-3-project/handlers/decrypt.js b/week-3-project/handlers/decrypt.js index 5878ad6..cf5493f 100644 --- a/week-3-project/handlers/decrypt.js +++ b/week-3-project/handlers/decrypt.js @@ -1,21 +1,23 @@ function decryptHandler() { // read and process user input (this works, no need to change it!) - const strToDecrypt = document.getElementById('encrypted-string-output').value; - - const rawNumInput = document.getElementById('decryption-key-input').value; + const encryptedArea = document.getElementById('encrypted-text-area'); + const strToDecrypt = encryptedArea.value; + const keyInput = document.getElementById('encryption-key-input'); + const rawNumInput = keyInput.value; const shiftNumber = Number(rawNumInput); if (isNaN(shiftNumber)) { throw new TypeError('decryption key must be a number'); } - // pass user input through core logic (this works! no need to change it) - const decrypted = caesarize(strToDecrypt); + // pass user input through core logic + const decrypted = decaesarize(strToDecrypt,shiftNumber); // report result to user (this works, no need to change it!) - const outputField = document.getElementById('decrypted-string-output'); - outputField.innerHTML = decrypted; + keyInput.value = ''; + const decryptedArea = document.getElementById('decrypted-text-area'); + decryptedArea.value = decrypted; console.log('\n--- decryptHandler ---'); console.log('strToDecrypt:', typeof strToDecrypt, ',', strToDecrypt); @@ -23,4 +25,4 @@ function decryptHandler() { console.log('decrypted:', typeof decrypted, ',', decrypted); }; const decryptButton = document.getElementById('decrypt-button'); -decryptButton.addEventListener('click', decryptHandler); +decryptButton.addEventListener('click', decryptHandler); \ No newline at end of file diff --git a/week-3-project/handlers/encrypt.js b/week-3-project/handlers/encrypt.js index b2e3325..5becd86 100644 --- a/week-3-project/handlers/encrypt.js +++ b/week-3-project/handlers/encrypt.js @@ -1,21 +1,23 @@ function encryptHandler() { // read and process user input (this works, no need to change it!) - const strToEncrypt = document.getElementById('unencrypted-string-input').value; + const encryptArea = document.getElementById('encrypted-text-area') + const strToEncrypt = encryptArea.value; - const rawNumInput = document.getElementById('encryption-key-input').value; + const keyInput = document.getElementById('encryption-key-input'); + const rawNumInput = keyInput.value; const shiftNumber = Number(rawNumInput); if (isNaN(shiftNumber)) { throw new TypeError('encryption key must be a number'); } - // pass user input through core logic (this works! no need to change it) - const encrypted = caesarize(strToEncrypt); + // pass user input through core logic + const encrypted = caesarize(strToEncrypt,shiftNumber); // report result to user (this works, no need to change it!) - const outputField = document.getElementById('encrypted-string-output'); - outputField.innerHTML = encrypted; + keyInput.value = ''; + encryptArea.value = encrypted; console.log('\n--- encryptHandler ---'); console.log('strToEncrypt:', typeof strToEncrypt, ',', strToEncrypt); @@ -23,4 +25,4 @@ function encryptHandler() { console.log('encrypted:', typeof encrypted, ',', encrypted); }; const encryptButton = document.getElementById('encrypt-button'); -encryptButton.addEventListener('click', encryptHandler); +encryptButton.addEventListener('click', encryptHandler); \ No newline at end of file diff --git a/week-3-project/handlers/scramble.js b/week-3-project/handlers/scramble.js index f93a0cd..8ddfaea 100644 --- a/week-3-project/handlers/scramble.js +++ b/week-3-project/handlers/scramble.js @@ -4,7 +4,7 @@ function scrambleHandler() { const toScramble = document.getElementById('scramble-input').value; // pass user input through core logic (this works! no need to change it) - const scrambled = scramble(toScramble); + const scrambled = document.getElementById('scramble-input').value;; // report result to user (this works, no need to change it!) const outputField = document.getElementById('scramble-output'); diff --git a/week-3-project/index.html b/week-3-project/index.html index e039e38..7ea89db 100644 --- a/week-3-project/index.html +++ b/week-3-project/index.html @@ -4,6 +4,10 @@ + + week 3 project @@ -12,7 +16,7 @@ - + @@ -21,24 +25,20 @@
-
+

This Encryption function translates data into another form

+

-
-

-     
-
-

+     
+

This Decryption function takes encrypted text and converting it back into text that you or the computer can read

- +
- -
-
-
-

+     

This scramble function converts an input string into a seemingly random output string of the same length

+
+
@@ -46,8 +46,15 @@ + + - + \ No newline at end of file diff --git a/week-3-project/logic/backward-sentence.js b/week-3-project/logic/backward-sentence.js deleted file mode 100644 index 1150fc7..0000000 --- a/week-3-project/logic/backward-sentence.js +++ /dev/null @@ -1,21 +0,0 @@ -/* reverse a string - - reverse the order of words in a string. - a word is anything with spaces on either side of it - - the spaces stay put - -*/ - - -const backwardsCases = [ - { name: 'first', args: [' plug (play!)'], expected: ' (play!) plug' }, - { name: 'second', args: [' -- - '], expected: ' - -- ' }, - { name: 'third', args: ['12 34 '], expected: '34 12 ' }, - { name: 'fourth', args: ['const x = null; '], expected: ';llun = x tsnoc ' }, - { name: 'fifth', args: [' e e '], expected: ' e e ' }, -]; -function backwards(str) { - // write me! -} -evaluate(backwards, backwardsCases); diff --git a/week-3-project/logic/caesarize.js b/week-3-project/logic/caesarize.js index a840159..c86c6ba 100644 --- a/week-3-project/logic/caesarize.js +++ b/week-3-project/logic/caesarize.js @@ -1,13 +1,10 @@ /* Caesar Cipher - this is a simple encoding algorithm that replaces letters in a message with a new letter - the new letter is determined by shifting N spaces across the alphabet - for example, caesarize("A", 3) will return : "D" because "D" is three letters past "A". + (yes, this is the same function you wrote last week) */ - const caesarizeTests = [ { name: 'first', args: ["aBcD", 3], expected: 'dEfG' }, { name: 'second', args: ["aBcD", -3], expected: 'xYzA' }, @@ -15,9 +12,66 @@ const caesarizeTests = [ { name: 'fourth', args: ["heLLo worLd!", 1], expected: 'ifMMp xpsMe!' }, { name: 'fifth', args: ["", 5], expected: '' }, { name: 'sixth', args: ["mnOpQr", 26], expected: 'mnOpQr' }, - { name: 'seventh', args: ["#@&&^F*(#", 7], expected: '#@&&^L*(#' }, + { name: 'seventh', args: ["#@&&^F*(#", 7], expected: '#@&&^M*(#' }, ]; -function caesarize(str, shiftNum) { +function caesarize(str, shiftNum, method) { // write me! + var input=str; + var str=input.split(''); + var code=input.split(''); + var ceasared=input.split(''); + for (var i = 0; i < str.length; i++) { + code[i] = str[i].charCodeAt(); + if ((code[i]>=97)&&(code[i]<=122)){ + code[i]+=shiftNum; + if (code[i]>122){ + code[i]=code[i]-26; + } + else if (code[i]<97){ + code[i]=code[i]+26; + } + ceasared[i] = String.fromCharCode(code[i]); + }else if ((code[i]>=65)&&(code[i]<=90)){ + code[i]+=shiftNum; + if (code[i]>90){ + code[i]=code[i]-26; + } + else if (code[i]<65){ + code[i]=code[i]+26; + } + ceasared[i] = String.fromCharCode(code[i]); + } + } + return ceasared.join(''); } -evaluate(caesarize, caesarizeTests); +function decaesarize(str, shiftNum) { + // write me! + var input=str; + var str=input.split(''); + var code=input.split(''); + var ceasared=input.split(''); + for (var i = 0; i < str.length; i++) { + code[i] = str[i].charCodeAt(); + if ((code[i]>=97)&&(code[i]<=122)){ + code[i]-=shiftNum; + if (code[i]>122){ + code[i]=code[i]+26; + } + else if (code[i]<97){ + code[i]=code[i]-26; + } + ceasared[i] = String.fromCharCode(code[i]); + }else if ((code[i]>=65)&&(code[i]<=90)){ + code[i]-=shiftNum; + if (code[i]>90){ + code[i]=code[i]+26; + } + else if (code[i]<65){ + code[i]=code[i]+26; + } + ceasared[i] = String.fromCharCode(code[i]); + } + } + return ceasared.join(''); +} +evaluate(caesarize, caesarizeTests); \ No newline at end of file diff --git a/week-3-project/logic/chunk.js b/week-3-project/logic/chunk.js index 0af0e05..ef978fc 100644 --- a/week-3-project/logic/chunk.js +++ b/week-3-project/logic/chunk.js @@ -39,10 +39,47 @@ const thirdExpected = [ const chunkTests = [ { name: 'first', args: ['const x = null;'], expected: ['const x ', '=', ' null', ';'] }, - { name: 'second', args: [secondArg], expected: secondExpected }, - { name: 'second', args: [thirdArg], expected: thirdExpected }, + { name: 'second', args: ['list of things'], expected: ["list of things" ] }, + { name: 'third', args: ['softly grows the grasses'], expected: ["softly grows the grasses"] }, ]; function chunk(str) { // write me! + var arrayReturn=[]; + var aStr = str.split(''); + var tempTypeLast = ''; + var tempIndiceLast = 0; + aStr.forEach(function (str){ + var codeStr = str.charCodeAt(); + if(codeStr==32||(codeStr>=48&&codeStr<=57)||(codeStr>=65&&codeStr<=90)||(codeStr>=97&&codeStr<=122)){ + if(tempTypeLast==''){ + arrayReturn[tempIndiceLast]=str; + tempTypeLast='isalphanum'; + }else{ + if(tempTypeLast=='isalphanum'){ + arrayReturn[tempIndiceLast]=arrayReturn[tempIndiceLast]+str; + }else{ + var newIndice=tempIndiceLast+1; + arrayReturn[newIndice]=str; + tempTypeLast='isalphanum'; + tempIndiceLast=newIndice; + } + } + }else{ + if(tempTypeLast==''){ + arrayReturn[tempIndiceLast]=str; + tempTypeLast='ispunct'; + }else{ + if(tempTypeLast=='ispunct'){ + arrayReturn[tempIndiceLast]=arrayReturn[tempIndiceLast]+str; + }else{ + var newIndice=tempIndiceLast+1; + arrayReturn[newIndice]=str; + tempTypeLast='ispunct'; + tempIndiceLast=newIndice; + } + } + } + }); + return arrayReturn; } evaluate(chunk, chunkTests); diff --git a/week-3-project/logic/reverse-chunk.js b/week-3-project/logic/reverse-chunk.js new file mode 100644 index 0000000..d0e6c72 --- /dev/null +++ b/week-3-project/logic/reverse-chunk.js @@ -0,0 +1,31 @@ +/* reverse a string + + reverse the order of words in a string. + a word is anything with spaces on either side of it + + the spaces, tabs and newlines stay put + +*/ + + +const reverseChunkTests = [ + { name: 'first', args: [' plug (play!)'], expected: " gulp )!yalp(" }, + { name: 'second', args: [' -- - '], expected: " -- - "}, + { name: 'third', args: ['12 34 '], expected: "21 43 " }, + { name: 'fourth', args: ['const x = null; '], expected: "tsnoc x = ;llun " }, + { name: 'fifth', args: [' e e '], expected: " e e " }, +]; +function reverseChunk(str) { + // write me! + var wordsArr = str.split(' '); + var reversedWordsArr = []; + wordsArr.forEach(word => { + var reversedWord = ''; + for(var i = word.length - 1; i >= 0; i--) { + reversedWord += word[i]; + } + reversedWordsArr.push(reversedWord); + }); + return reversedWordsArr.join(' '); +} +evaluate(reverseChunk, reverseChunkTests); diff --git a/week-3-project/logic/scramble.js b/week-3-project/logic/scramble.js index 5bb15ce..ed3dc51 100644 --- a/week-3-project/logic/scramble.js +++ b/week-3-project/logic/scramble.js @@ -1,10 +1,8 @@ /* scramble! - Write a function that does these things to a string: - sort each word (anything with only numbers or letters, separated by spaces) - reverse chunks (anything between two punctuation marks, a new line or the beginning/end of the string) - preserve formatting (leave tabs and newlines in place); - */ const thirdScrambleArg = `a list of drinks: @@ -20,13 +18,15 @@ const scrambleTests = [ { name: 'first', args: ['the road works.'], expected: 'korsw ador eht.' }, { name: 'second', args: ["name: 'second'"], expected: "aemn: 'cednos'" }, { name: 'third', args: [thirdScrambleArg], expected: thirdScrambleExpected }, + { name: 'fourth', args: ["name: 'second cow'"], expected: "aemn: 'cow cednos'" }, + { name: 'fifth', args: ["name e eman: 'second cow, cba'"], expected: "aemn e aemn: 'cow cednos, cba'" }, ]; function scramble(str) { /* - write a new implementation of scramble, passing the same tests as last week - this time you will use the functions "backwards", "sort", and "chunk" + write this function using "chunk", "sortWords" and "reverseChunk" each of these functions is one step along the way to a scrambled string - this is a nice exercise in using smaller functions to solve larger problems + this is a an exercise to practice using breaking large problems into smaller ones + and then solving the smaller problems and combining the small solutions into a full solution */ } -evaluate(scramble, scrambleTests); +evaluate(scramble, scrambleTests); \ No newline at end of file diff --git a/week-3-project/logic/sort-words.js b/week-3-project/logic/sort-words.js index bbb61f3..2311965 100644 --- a/week-3-project/logic/sort-words.js +++ b/week-3-project/logic/sort-words.js @@ -1,15 +1,28 @@ /* sort characters - sort the words in a string, words are substrings separated by spaces + sort the characters in a word, words are substrings separated by spaces */ const sortTests = [ - { name: 'first', args: ['4321 cde'], expected: '1234 cde' }, - { name: 'second', args: ['abcd 153'], expected: 'abcd 135' }, - { name: 'third', args: ['howdy doody time'], expected: 'dhowy ddooy eimt' }, + { name: 'first', args: ['4321 cde'], expected: " 1234cde" }, + { name: 'second', args: ['abcd 153'], expected: " 135abcd" }, + { name: 'third', args: ['howdy doody time'], expected: " dddehimoootwyy" }, ]; function sort(str) { // write me! + var arr = str.split(''); + var tmp; + for(var i = 0; i < arr.length; i++){ + for(var j = i + 1; j < arr.length; j++){ + if(arr[i] > arr[j]){ + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + } + return arr.join(''); + } evaluate(sort, sortTests); diff --git a/week-3-project/style.css b/week-3-project/style.css index e69de29..9ae36f5 100644 --- a/week-3-project/style.css +++ b/week-3-project/style.css @@ -0,0 +1,125 @@ +body{ + background: rgb(33, 78, 78); + font-family: sans-serif; + text-align: center; + margin: auto; + width: 90%; +} + +#header{ + background: darkgrey; + margin: auto; + width: 50%; + padding: 0.1%; + border: 2px rgb(0, 195, 255) solid; + font-size: 10px; +} + +#caesaring-container{ + margin: auto; + width: 50%; + text-align: center; +} + +#encrypted-text-area{ + margin: auto; + border: 2px rgb(0, 195, 255) solid; + text-align: center; +} + +#encrypt-button{ + display: inline-block; + border-radius: 4px; + background-color: #442e27; + border: none; + color: #FFFFFF; + text-align: center; + font-size: 28px; + padding: 10px; + width: 200px; + transition: all 0.5s; + cursor: pointer; + margin: 5px; + +} + +#encryption-key-input{ +margin: auto; +border: 2px rgb(0, 195, 255) solid; +text-align: center; +width: 35%; +} + +#decrypt-button{ + display: inline-block; + border-radius: 4px; + background-color: #442e27; + border: none; + color: #FFFFFF; + text-align: center; + font-size: 28px; + padding: 10px; + width: 200px; + transition: all 0.5s; + cursor: pointer; + margin: 5px; +} + +#decrypted-text-area{ + margin: auto; + border: 2px rgb(0, 195, 255) solid; + text-align: center; +} +#scramble-input{ + margin: auto; + border: 2px rgb(0, 195, 255) solid; + text-align: center; +} +#scramble-button{ + display: inline-block; + border-radius: 4px; + background-color: #442e27; + border: none; + color: #FFFFFF; + text-align: center; + font-size: 28px; + padding: 10px; + width: 200px; + transition: all 0.5s; + cursor: pointer; + margin: 5px; +} + +#scramble-output{ + margin: auto; + border: 2px rgb(0, 195, 255) solid; + text-align: center; +} +#footer{ + background: darkgrey; + margin: auto; + width:50%; + padding: 0.1%; + border: 2px rgb(0, 195, 255) solid; + font-size: 10px; +} + +#encrypt-tag{ + background: rgb(99, 95, 95); +} + +#decrypt-tag{ + background: rgb(99, 95, 95); + +} + +#scramble-tag{ + background: rgb(99, 95, 95); + margin: auto; + width: 50%; +} +#footer-logo{ + margin: auto; + width: 1px; + padding: 0.1%; +} \ No newline at end of file