diff --git a/module-exercises/index.html b/module-exercises/index.html deleted file mode 100644 index 2ef651b..0000000 --- a/module-exercises/index.html +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - understand javascript - - - - - - - - how to work with these exercises - - - - - - - - - - - - - - - - - - - - - - challenge exercises - - - - diff --git a/module-exercises/variables.js b/module-exercises/variables.js deleted file mode 100644 index 0ed91de..0000000 --- a/module-exercises/variables.js +++ /dev/null @@ -1,323 +0,0 @@ -// https://www.youtube.com/watch?v=pHt_tKYUgbo&list=PLzV58Zm8FuBJFfQN5il3ujx6FDAY8Ds3u&index=2 -// https://github.com/janke-learning/variables-and-hoisting -// https://github.com/janke-learning/variable-exercises - -{ - const pageTitle = 'variables'; - const header = document.createElement("h2"); - header.innerHTML = pageTitle; - document.body.appendChild(header); - console.groupCollapsed(pageTitle); -} - - -// this example covers only let & const -function example_declarationAndAssignment() { - - // declaring a new "let" variable opens a new labeled slot in memory - // if no value is assigned, the default value is 'undefined' - let declaredLetWithoutAssignment; - - // setting or resetting the value stored in the slot is "assignment" - declaredLetWithoutAssignment = 'assigned after declaration'; - - // accessing a const/let variable before it is declared will error - declaredLetWithAssignment; // comment this line to remove the error! - - // you will generally do both declaration and assignment at once - // notice how this slot is only created when this line is reached? - let declaredLetWithAssignment = 'assigned at declaration'; - - // variables declared with 'let' can have their values reassigned later on - declaredLetWithoutAssignment = 'new value'; - declaredLetWithAssignment = 'another new value'; - - - // const variables cannot be declared without an assignment - const constantVariable = 'forever!'; - // const errorTime; // uncomment this line to throw error! - - // const variables cannot be reassigned later in the program - constantVariable = 'error time'; - -} -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 - - let a = 'b', b = 'a'; - let temp = ''; - - temp = a; - a = b; - b = temp; - - console.assert(a === 'a', 'a should store "a"'); - console.assert(b === 'b', 'b should store "b"'); - -} -evaluate(example_twoVariableSwap); - - -/* variable exercises - - the remainder of these exercises are all variations of the swap above -*/ - -function threeVariableSwap1() { - - let a = "c", b = "a", c = "b"; - let temp = ''; - - // can be done in 4 lines - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); -} -evaluate(threeVariableSwap1); - -function threeVariableSwap2() { - - let a = "b", b = "c", c = "a"; - let temp = ''; - - // can be done in 4 lines - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); -} -evaluate(threeVariableSwap2); - -function fourVariableSwap1() { - - let a = "d", b = "a", c = "b", d = "c"; - let temp = ''; - - // can be done in 5 lines - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); - console.assert(d === "d", "d should store 'd'"); -} -evaluate(fourVariableSwap1); - -function fourVariableSwap2() { - - let a = "z", b = "y", c = "x", d = "w"; - let temp = ''; - - // can be done in 6 lines - - - console.assert(a === "w", "a should store 'w'"); - console.assert(b === "x", "b should store 'x'"); - console.assert(c === "y", "c should store 'y'"); - console.assert(d === "z", "d should store 'z'"); -} -evaluate(fourVariableSwap2); - -function fiveVariableSwap() { - - let a = "z", b = "y", c = "x", d = "w", e = "v"; - let temp = ' '; - - // can be done in 6 lines - - - console.assert(a === "v", "a should store 'v'"); - console.assert(b === "w", "b should store 'w'"); - console.assert(c === "x", "c should store 'x'"); - console.assert(d === "y", "d should store 'y'"); - console.assert(e === "z", "e should store 'z'"); -} -evaluate(fiveVariableSwap); - - -function example1_multipleAssignments() { - - // it is possible to assign multiple variables on one line - // these assignments are executed from left to right - - let a = 'b', b = 'a', temp = ''; - - temp = a, a = b, b = temp; - - console.assert(a === 'a', 'a should store "a"'); - console.assert(b === 'b', 'b should store "b"'); -} -evaluate(example1_multipleAssignments); - - - -function example2_multipleAssignments() { - - // using multiple assignments is largely a style choice - // if you find it easier to read and understand, go for it! - - let a = 'c', b = 'a', c = 'b', temp = ''; - - temp = a, a = b, b = c, c = 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"'); -} -evaluate(example2_multipleAssignments); - - -function multipleAssignments1() { - - let a = "c", b = "a", c = "b"; - let temp = ''; - - // can be done in 1 line - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); -} -evaluate(multipleAssignments1); - -function multipleAssignments2() { - - let a = "b", b = "c", c = "a"; - let temp = ''; - - // can be done in 1 line - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); -} -evaluate(multipleAssignments2); - -function multipleAssignments3() { - - let a = "d", b = "a", c = "b", d = "c"; - let temp = ''; - - // can be done in 1 line - - - console.assert(a === "a", "a should store 'a'"); - console.assert(b === "b", "b should store 'b'"); - console.assert(c === "c", "c should store 'c'"); - console.assert(d === "d", "d should store 'd'"); -} -evaluate(multipleAssignments3); - -function multipleAssignments4() { - - let a = "z", b = "y", c = "x", d = "w"; - let temp = ''; - - // can be done in 1 line - - - console.assert(a === "w", "a should store 'w'"); - console.assert(b === "x", "b should store 'x'"); - console.assert(c === "y", "c should store 'y'"); - console.assert(d === "z", "d should store 'z'"); -} -evaluate(multipleAssignments4); - - - -function example_chainedAssignments() { - - // you can assign the same value to multiple variables at once - // chained assignments are read right to left - // using chained or single assignments depends on what you understand better - - let a = 'b', b1 = b2 = 'a'; - let temp = ''; - - temp = a; - a = b1; - b1 = b2 = temp; - - console.assert(a === "a", 'a should store "a"'); - console.assert(b1 === "b", 'b1 should store "b"'); - console.assert(b1 === b2, 'b1 should store the same value as b2'); -} -evaluate(example_chainedAssignments); - - -function chainedAssignments1() { - - let a1 = a2 = 'b', b = 'a'; - let temp = ''; - - // can be done in 3 lines or less - - console.assert(a1 === "a", 'a1 should store "a"'); - console.assert(a1 === a2, 'a1 should store the same value as a2'); - console.assert(b === "b", 'b should store "b"'); -} -evaluate(chainedAssignments1); - - - -function chainedAssignments2() { - let a = 'c'; - let b1 = b2 = 'a'; - let c1 = c2 = c3 = 'b'; - let temp = ''; - - // can be done in 4 lines or less - - - - console.assert(a === "a", 'a should store "a"'); - console.assert(b1 === "b", 'b1 should store "b"'); - console.assert(b1 === b2, 'b1 should store the same as b2'); - console.assert(c1 === "c", 'c1 should store "c"'); - console.assert(c1 === c2, 'c1 should store the same as c2'); - console.assert(c2 === c3, 'c2 should store the same as c3'); -} -evaluate(chainedAssignments2); - - -function footnote_var() { - - // in JS you can also declare variables using 'var' - // 'var' is like 'let' in that variables can be reassigned - // 'var' variables are different in two main ways - // this type of variable is "hoisted" - // they have no block scope, only lexical - // don't worry about "var" or these differences for now - // just know that this exists since you will find it online - // always use 'let' and 'const' in your programs to avoid "var" bugs - - var varVariable = 'the slot is created before the declaration is reached (hoisting)'; - - varVariable = 'reassignment is possible'; - -} -evaluate(footnote_var); - - -{ - console.groupEnd(); - document.body.appendChild(document.createElement('hr')); -} diff --git a/week-1-project/devowel-handler.js b/week-1-project/devowel-handler.js index c50d2d2..228835e 100644 --- a/week-1-project/devowel-handler.js +++ b/week-1-project/devowel-handler.js @@ -17,7 +17,14 @@ function devowelHandler() { const toDevowel = document.getElementById('devowel-input').value; // pass user input through core logic (write this! it doesn't work) - const devoweled = `remove all vowels from ${toDevowel}`; + + function disVowel(toDevowel){ + let vowels = ['a','e','o','i','u']; + return toDevowel.split('').filter(function(el) { + return vowels.indexOf(el.toLowerCase()) == -1; + }).join(); + } + const devoweled = disVowel(toDevowel); // report result to user (this works, no need to change it!) const outputField = document.getElementById('devowel-output'); diff --git a/week-1-project/index.html b/week-1-project/index.html index 9397ef9..a13925b 100644 --- a/week-1-project/index.html +++ b/week-1-project/index.html @@ -4,16 +4,17 @@ - + week 1 project - + - - +

Mustapha/Week1/JS/Homework

+

How to use this website

+

You should write a string in the placeholder you want and click on the button below it



diff --git a/week-1-project/repeat-handler.js b/week-1-project/repeat-handler.js index d0c5d72..56d3b4c 100644 --- a/week-1-project/repeat-handler.js +++ b/week-1-project/repeat-handler.js @@ -25,8 +25,15 @@ function repeatHandler() { // pass user input through core logic (write this! it doesn't work) - const repeated = `repeat ${strToRepeat} ${numOfRepetitions} times`; - + function strToRepeat1(string, times) { + if(times < 0) + return ""; + if(times === 1) + return string; + else + return string + strToRepeat1(string, times - 1); + } + const repeated = strToRepeat.repeat(20) ; // report result to user (this works, no need to change it!) const outputField = document.getElementById('repeat-output'); outputField.innerHTML = repeated; diff --git a/week-1-project/reverse-handler.js b/week-1-project/reverse-handler.js index 6682c5f..a082c45 100644 --- a/week-1-project/reverse-handler.js +++ b/week-1-project/reverse-handler.js @@ -18,7 +18,13 @@ 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}`; + + function stringToReverse(toReverse){ + return toReverse.split('').reverse().join(''); + + }; + const reversed = stringToReverse(toReverse) ; + // report result to user (this works, no need to change it!) const outputField = document.getElementById('reverse-output'); diff --git a/week-1-project/sort-handler.js b/week-1-project/sort-handler.js index e45575f..14db537 100644 --- a/week-1-project/sort-handler.js +++ b/week-1-project/sort-handler.js @@ -13,12 +13,17 @@ the handler is already set up to: function sortHandler() { - + const toSort1 = ['Lemon','Banana','Apples','Grapes']; // read and process user input (this works, no need to change it!) const toSort = document.getElementById('sort-input').value; // pass user input through core logic (write this! it doesn't work) - const sorted = `sort the charecters in ${toSort}`; + function sortString(toSort){ + let toSort1 = toSort.split(''); + let sorted = toSort1.sort(); + return sorted.join(''); + } + const sorted = sortString(toSort); // report result to user (this works, no need to change it!) const outputField = document.getElementById('sort-output'); diff --git a/week-1-project/style.css b/week-1-project/style.css index e69de29..f8a7387 100644 --- a/week-1-project/style.css +++ b/week-1-project/style.css @@ -0,0 +1,8 @@ +body { + font-family: Arial, Helvetica, sans-serif; + font-size: 16px; + background: aqua; + text-align: center; + +} +