From ee34d841574a752a503dc67beaadf5b1620048d5 Mon Sep 17 00:00:00 2001 From: meryem sener Date: Sat, 12 Oct 2019 00:24:22 +0200 Subject: [PATCH 1/4] module-exercises --- module-exercises/index.html | 4 +- module-exercises/variables.js | 72 ++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/module-exercises/index.html b/module-exercises/index.html index 2ef651b..6a76a4b 100644 --- a/module-exercises/index.html +++ b/module-exercises/index.html @@ -21,7 +21,7 @@ --> - + challenge exercises diff --git a/module-exercises/variables.js b/module-exercises/variables.js index 0ed91de..acd5bba 100644 --- a/module-exercises/variables.js +++ b/module-exercises/variables.js @@ -47,23 +47,25 @@ 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 + // 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 - + let a = 'c'; + let b1 = b2 = 'a'; + let c1 = c2 = c3 = 'b'; + let temp = ''; console.assert(a === "a", 'a should store "a"'); console.assert(b1 === "b", 'b1 should store "b"'); From e37ddae03f6854b90f9db40569a117a556e72667 Mon Sep 17 00:00:00 2001 From: meryem sener Date: Wed, 16 Oct 2019 22:05:22 +0200 Subject: [PATCH 2/4] FInished some egzercises --- module-exercises/explicit-coercion.js | 34 +++++----- module-exercises/functions.js | 90 ++++++++++++++------------- module-exercises/index.html | 4 +- module-exercises/primitive-types.js | 64 +++++++++---------- module-exercises/variables.js | 32 +++++----- week-2-project/scripts/caesarize.js | 1 + 6 files changed, 119 insertions(+), 106 deletions(-) diff --git a/module-exercises/explicit-coercion.js b/module-exercises/explicit-coercion.js index d6bcfd4..78d31ed 100644 --- a/module-exercises/explicit-coercion.js +++ b/module-exercises/explicit-coercion.js @@ -23,12 +23,18 @@ // 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:'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: 'num, 3', args: [3], expected: Number }, + { name: 'boo, true', args: [true], expected: true }, { name: 'obj, null', args: [null], expected: null }, - { name: 'und, undefined', args: [undefined], expected: null }, + { name: 'und, undefined', args: [undefined], expected: undefined }, + { name: 'Meryem, Sener', arg: [Meryem], expected: 'String' }, + { name: 'Meryem, Sener', arg: [Meryem], expected: 'String' }, + { name: 'Meryem, Sener', arg: [Meryem], expected: 'String' }, + { name: 'Meryem, Sener', arg: [Meryem], expected: 'String' }, + { name: 'Meryem, Sener', arg: [Meryem], expected: 'String' }, + // write at least 5 more test cases for the String function ]; String.quizzing = true; @@ -39,22 +45,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, 3', args: [3], expected: Number }, + { name: 'num, 0', args: [0], expected: Number }, + { name: 'num, 1e3', args: [1000], expected: Number }, { 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: Boolean }, + { name: 'boo, false', args: [false], expected: Boolean }, // null & undefined { name: 'obj, null', args: [null], expected: NaN }, - { name: 'und, undefined', args: [undefined], expected: 0 }, + { name: 'und, undefined', args: [undefined], expected: undefined }, // 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, Infinity', args: ['Infinity'], expected: 'string' }, + { name: 'str, three', args: ['three'], expected: 'string' }, + { name: 'str, 3', args: ['3'], expected: 'String' }, ]; Number.quizzing = true; evaluate(Number, NumberTests); @@ -70,7 +76,7 @@ const BooleanTests = [ { name: 'num, 3', args: [3], expected: true }, { name: 'num, 0', args: [0], expected: true }, { name: 'num, 1e3', args: [1000], expected: true }, - { name: 'num, Infinity', args: [Infinity], expected: false }, + { name: 'num, Infinity', args: [Infinity], expected: 'Infinity' }, { name: 'num, NaN', args: [NaN], expected: false }, // null & undefined { name: 'obj, null', args: [null], expected: true }, diff --git a/module-exercises/functions.js b/module-exercises/functions.js index f22570a..18701c7 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; @@ -483,6 +488,7 @@ const writeTestCases4 = [ { 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! + ]; function functionToTest4(a, b, c) { const result = c + a + b; diff --git a/module-exercises/index.html b/module-exercises/index.html index 6a76a4b..0914d7c 100644 --- a/module-exercises/index.html +++ b/module-exercises/index.html @@ -21,7 +21,7 @@ --> -
-
- - + + + + diff --git a/week-2-project/scripts/caesarize.js b/week-2-project/scripts/caesarize.js index 2a561c7..0119b58 100644 --- a/week-2-project/scripts/caesarize.js +++ b/week-2-project/scripts/caesarize.js @@ -1,11 +1,8 @@ /* 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". + //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". */ const caesarizeTests = [ @@ -15,11 +12,11 @@ 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! - + } evaluate(caesarize, caesarizeTests); @@ -37,7 +34,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'); @@ -49,4 +46,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..03fff78 100644 --- a/week-2-project/scripts/constantize.js +++ b/week-2-project/scripts/constantize.js @@ -4,27 +4,30 @@ so if you needed to assign that value to a variable you would do it at the top of your code with a name like: const SPEED_OF_LIGHT_IN_VACUUM = 'very fast'; - in addition, JS doesn't allow certain charecters to be used in variable names if a string includes any unallowed charecters, just remove them: MILK_&_CEREAL -> MILK__CEREAL - for consistency, and so it's easier to remember variable names, make sure there is only ever one underscore in a row MILK__CEREAL -> MILK_CEREAL */ 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! -} + + + } + + evaluate(constantize, constantizeTests); @@ -44,4 +47,4 @@ function constantizeHandler() { console.log('constantized:', typeof constantized, ',', constantized); }; const constantizeButton = document.getElementById('constantize-button'); -constantizeButton.addEventListener('click', constantizeHandler); +constantizeButton.addEventListener('click', constantizeHandler); \ No newline at end of file diff --git a/week-2-project/scripts/leftpad.js b/week-2-project/scripts/leftpad.js new file mode 100644 index 0000000..a1eebb8 --- /dev/null +++ b/week-2-project/scripts/leftpad.js @@ -0,0 +1,66 @@ +/* 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! + +} +evaluate(leftpad, leftpadTests); + + +function leftpadHandler() { + + // read and process user input (this works, no need to change it!) + const stringToPad = document.getElementById('leftpad-str-input').value; + const targetLengthStr = document.getElementById('leftpad-len-input').value; + let targetLength; + if (isNaN(targetLengthStr) || targetLengthStr === '') { + throw new TypeError('length needs to be a number'); + } else { + targetLength = Number(targetLengthStr); + } + const padding = document.getElementById('leftpad-pad-input').value; + + // pass user input through core logic (this works! no need to change it) + const leftpadded = leftpad(stringToPad, targetLength, padding); + + // report result to user (this works, no need to change it!) + const outputField = document.getElementById('leftpad-output'); + outputField.innerHTML = leftpadded; + + console.log('\n--- leftpadHandler ---'); + console.log('stringToPad:', typeof stringToPad, ',', stringToPad); + console.log('targetLength:', typeof targetLength, ',', targetLength); + console.log('padding:', typeof padding, ',', padding); + console.log('leftpadded:', typeof leftpadded, ',', leftpadded); +}; +const leftpadButton = document.getElementById('leftpad-button'); +leftpadButton.addEventListener('click', leftpadHandler); + + +// https://www.npmjs.com/package/left-pad +// https://programmingpraxis.com/2016/03/25/leftpad/ \ No newline at end of file diff --git a/week-2-project/scripts/repeatChars.js b/week-2-project/scripts/repeatChars.js index d2209b6..1811a56 100644 --- a/week-2-project/scripts/repeatChars.js +++ b/week-2-project/scripts/repeatChars.js @@ -1,23 +1,22 @@ -/* repeat charecters +// repeat charecters +// write a function that repeats the charecters in a string according to these rules: +// - letters are doubled +// - numbers are tripled +// - anything else is quadrupled -write a function that repeats the charecters in a string according to these rules: -- letters are doubled -- numbers are tripled -- anything else is quadrupled - -*/ const repeatCharsTests = [ { 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: '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! + } evaluate(repeatChars, repeatCharsTests); @@ -40,4 +39,4 @@ function repeatCharsHandler() { console.log('repeatCharsed:', typeof repeatCharsed, ',', repeatCharsed); }; const repeatCharsButton = document.getElementById('repeatChars-button'); -repeatCharsButton.addEventListener('click', repeatCharsHandler); +repeatCharsButton.addEventListener('click', repeatCharsHandler); \ No newline at end of file diff --git a/week-2-project/style.css b/week-2-project/style.css index e69de29..da2b822 100644 --- a/week-2-project/style.css +++ b/week-2-project/style.css @@ -0,0 +1,26 @@ +body { + background-color: slategrey; + font-size: 21px; + text-align: center; + margin: 0; + padding: 0; +} +div#hname { + border:8px ridge grey; + background-color: silver; + color:grey; + border-radius:25px; +} +h1 { + padding:10px 400px; + font-family: Arial, Helvetica, sans-serif; +} + + +div#scramble { + background-color: aqua; + +} +div#scramble-output { + +} \ No newline at end of file