From 66604e95f9528f5101c2d9b7627345c6d8db4f7c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 5 Mar 2019 22:56:25 +0530 Subject: [PATCH 001/161] Solution --- .../Sign-of-product/index.html | 6 +++++- .../Sign-of-product/script.js | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Program-Structures/Extra-exercises-from-online/Sign-of-product/index.html b/Program-Structures/Extra-exercises-from-online/Sign-of-product/index.html index 784618b..d50e4ce 100644 --- a/Program-Structures/Extra-exercises-from-online/Sign-of-product/index.html +++ b/Program-Structures/Extra-exercises-from-online/Sign-of-product/index.html @@ -9,7 +9,7 @@
-

Enter two integers and click submit to find the largest

+

Sign of Product of three Numbers

@@ -19,6 +19,10 @@

Enter two integers and click submit to find the largest

+
+ + +
diff --git a/Program-Structures/Extra-exercises-from-online/Sign-of-product/script.js b/Program-Structures/Extra-exercises-from-online/Sign-of-product/script.js index b8e6e2f..1e9140c 100644 --- a/Program-Structures/Extra-exercises-from-online/Sign-of-product/script.js +++ b/Program-Structures/Extra-exercises-from-online/Sign-of-product/script.js @@ -1,15 +1,19 @@ let inputOne = document.getElementById("firstNum"); let inputTwo = document.getElementById("secondNum"); +let inputThree = document.getElementById("thirdNum"); let submitBtn = document.getElementById("btn"); let resetBtn = document.getElementById("reset-btn"); let answer = document.getElementById("result"); submitBtn.addEventListener("click", function() { - if (Number(inputOne.value) >= Number(inputTwo.value)) { - answer.innerHTML = Number(inputOne.value); - } else if (Number(inputTwo.value) >= Number(inputOne.value)) { - answer.innerHTML = Number(inputTwo.value); + let product = + Number(inputOne.value) * Number(inputTwo.value) * Number(inputThree.value); + if (Math.sign(product) == -1) { + answer.innerHTML = "-"; + } else if (Math.sign(product) == 1) { + answer.innerHTML = "+"; } + // answer.innerHTML = product; }); resetBtn.addEventListener("click", function() { @@ -19,7 +23,8 @@ resetBtn.addEventListener("click", function() { }); //things I learned in this -//input.value is always a string -//so if you have to compare numbers -//convert it to number using Number(); -//Please correct me If I am wrong. +//built in function in JS called Math.sign(number) +//that checks for the sign of a number +//eg:Math.sign(-4) result will be -1; +//eg:Math.sign(1) result will be 1; +//Reference website: https://www.geeksforgeeks.org/javascript-math-sign-function/ From 4761f4a8232fe6012d8334be219ff52be1d235f9 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 7 Mar 2019 00:15:05 +0530 Subject: [PATCH 002/161] Sort the three numbers in ascending --- .../Sort-three-numbers/index.html | 33 ++++++++++++ .../Sort-three-numbers/script.js | 53 +++++++++++++++++++ .../Sort-three-numbers/style.css | 25 +++++++++ 3 files changed, 111 insertions(+) create mode 100644 Program-Structures/Extra-exercises-from-online/Sort-three-numbers/index.html create mode 100644 Program-Structures/Extra-exercises-from-online/Sort-three-numbers/script.js create mode 100644 Program-Structures/Extra-exercises-from-online/Sort-three-numbers/style.css diff --git a/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/index.html b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/index.html new file mode 100644 index 0000000..bae164e --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/index.html @@ -0,0 +1,33 @@ + + + + + + + + Codestin Search App + + +
+

Sort Three Numbers

+
+
+ + +
+
+ + +
+
+ + +
+ + +
+ Answer is: +
+ + + diff --git a/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/script.js b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/script.js new file mode 100644 index 0000000..7cd9ee7 --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/script.js @@ -0,0 +1,53 @@ +let inputOne = document.getElementById("firstNum"); +let inputTwo = document.getElementById("secondNum"); +let inputThree = document.getElementById("thirdNum"); +let submitBtn = document.getElementById("btn"); +let resetBtn = document.getElementById("reset-btn"); +let answer = document.getElementById("result"); + +submitBtn.addEventListener("click", function() { + if ( + Number(inputOne.value) < Number(inputTwo.value) && + Number(inputOne.value) < Number(inputThree.value) + ) { + if (Number(inputTwo.value) < Number(inputThree.value)) { + answer.innerHTML = + inputOne.value + " " + inputTwo.value + " " + inputThree.value; + } else + answer.innerHTML = + inputOne.value + " " + inputThree.value + " " + inputTwo.value; + } else if ( + Number(inputTwo.value) < Number(inputOne.value) && + Number(inputTwo.value) < Number(inputThree.value) + ) { + if (Number(inputOne.value) < Number(inputThree.value)) { + answer.innerHTML = + inputTwo.value + " " + inputOne.value + " " + inputThree.value; + } else { + answer.innerHTML = + inputTwo.value + " " + inputThree.value + " " + inputOne.value; + } + } else if ( + Number(inputThree.value) < Number(inputOne.value) && + Number(inputThree.value) < Number(inputTwo.value) + ) { + if (Number(inputOne.value) < Number(inputTwo.value)) { + answer.innerHTML = + inputThree.value + " " + inputOne.value + " " + inputTwo.value; + } else { + answer.innerHTML = + inputThree.value + " " + inputTwo.value + " " + inputOne.value; + } + } +}); + +resetBtn.addEventListener("click", function() { + inputOne.value = ""; + inputTwo.value = ""; + inputThree.value = ""; + answer.innerHTML = ""; +}); + +//things I learned in this +//this is using long and tedious method of comparing each and every number with other one by one +//getting the result diff --git a/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/style.css b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/style.css new file mode 100644 index 0000000..21330eb --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Sort-three-numbers/style.css @@ -0,0 +1,25 @@ +body { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +.parent { + width: 90%; + border: 1px solid #ccc; + padding: 10px; + margin: 0 auto; + text-align: center; +} +.inputs { + display: flex; + justify-content: center; + align-items: center; + margin-bottom: 1rem; +} +.inputs-bags { + margin-right: 10px; +} +.answer { + font-size: 1.5rem; +} From c946822b105a589f38eadc5dd8c15b7e0b608746 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 8 Mar 2019 00:17:43 +0530 Subject: [PATCH 003/161] Find the odd and even number between 0 and 15. --- .../Find-odd-even/index.html | 28 ++++++++++++++ .../Find-odd-even/script.js | 26 +++++++++++++ .../Find-odd-even/style.css | 38 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 Program-Structures/Extra-exercises-from-online/Find-odd-even/index.html create mode 100644 Program-Structures/Extra-exercises-from-online/Find-odd-even/script.js create mode 100644 Program-Structures/Extra-exercises-from-online/Find-odd-even/style.css diff --git a/Program-Structures/Extra-exercises-from-online/Find-odd-even/index.html b/Program-Structures/Extra-exercises-from-online/Find-odd-even/index.html new file mode 100644 index 0000000..c4f8f00 --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Find-odd-even/index.html @@ -0,0 +1,28 @@ + + + + + + + + Codestin Search App + + +
+

Find odd or even from numbers 0 to 15

+
    + + Answer is: + +
    + Even Number is: + +
    +
    + Odd Number is: + +
    +
    + + + diff --git a/Program-Structures/Extra-exercises-from-online/Find-odd-even/script.js b/Program-Structures/Extra-exercises-from-online/Find-odd-even/script.js new file mode 100644 index 0000000..1596694 --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Find-odd-even/script.js @@ -0,0 +1,26 @@ +let numbers = ""; +let displayNum = document.getElementById("displayNum"); + +for (let i = 0; i < 15; i++) { + numbers = i; + + let li = document.createElement("LI"); + let textNode = document.createTextNode(numbers); + li.appendChild(textNode); + displayNum.appendChild(li); + + if (numbers % 2 == 0) { + li.className += "even-number"; + let paraEven = document.getElementById("even"); + paraEven.innerHTML += numbers + " "; + } else { + li.className += "odd-number"; + let paraOdd = document.getElementById("odd"); + paraOdd.innerHTML += numbers + " "; + } +} + +//things I learned in this +//adding content into ul via js +//adding class to a element coditionally +//showing one number after another in html diff --git a/Program-Structures/Extra-exercises-from-online/Find-odd-even/style.css b/Program-Structures/Extra-exercises-from-online/Find-odd-even/style.css new file mode 100644 index 0000000..5dc49f0 --- /dev/null +++ b/Program-Structures/Extra-exercises-from-online/Find-odd-even/style.css @@ -0,0 +1,38 @@ +body { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +.parent { + width: 90%; + border: 1px solid #ccc; + padding: 10px; + margin: 0 auto; + text-align: left; +} +.inputs { + display: flex; + justify-content: center; + align-items: center; + margin-bottom: 1rem; +} +#displayNum { + display: flex; + text-align: center; + list-style: none; +} +#displayNum li { + flex: 1 1 0; +} +.even-number, +.odd-number { + color: #fff; +} + +.even-number { + background: green; +} +.odd-number { + background: mediumaquamarine; +} From bd52ccc1d7321d3e4e92b3071d4d2a1e6168b6e7 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 19 Mar 2019 23:49:33 +0530 Subject: [PATCH 004/161] Notes about Functions --- Functions/README.md | 11 +++++++ Functions/index.html | 68 ++++++++++++++++++++++++++++++++++++++++++++ Functions/script.js | 0 Functions/style.css | 24 ++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 Functions/index.html create mode 100644 Functions/script.js create mode 100644 Functions/style.css diff --git a/Functions/README.md b/Functions/README.md index f849655..d175c09 100644 --- a/Functions/README.md +++ b/Functions/README.md @@ -1,3 +1,14 @@ # Eloquent-Javascript-Exercises These Exercises are from the chapter Function from the Book + +Notes Related to Functions in JS: + +Defining a function + +eg: let square = function(x){ +return x\*x; +} +console.log(square(5)); + +Result: 25; diff --git a/Functions/index.html b/Functions/index.html new file mode 100644 index 0000000..67876c3 --- /dev/null +++ b/Functions/index.html @@ -0,0 +1,68 @@ + + + + + + + + + Codestin Search App + + +

    + Functions In JS +

    +

    + Function in JS is set of statements that execute a certain tasks. +

    +

    Advantage of functions

    +
      +
    • Reduce repetion
    • +
    • Helps in structuring large programs
    • +
    +

    Defining Functions

    +
    +
    +

    Functions declarations

    + Eg: +
    +         function square(number){
    +           return number*number;
    +          }
    +
    +          Calling the function:
    +          square(30);
    +          
    + Here number is called a parameter +
    + +
    +

    Functions Expressions

    + Here it is like passing a value to a variable(binding) Eg: +
    +        let square = function (number){
    +         return number*number;
    +       }
    +                                      
    +       square(30);
    +        
    + Here number is called a parameter +
    +
    +

    Arrow Functions

    + This is the simpler(ES6) way of defining a function +
    +                        let square = (number) => {return number*number};
    +
    +                        square(40);
    +                  
    + Here number is called a parameter +
    +
    + + diff --git a/Functions/script.js b/Functions/script.js new file mode 100644 index 0000000..e69de29 diff --git a/Functions/style.css b/Functions/style.css new file mode 100644 index 0000000..a5f1aa1 --- /dev/null +++ b/Functions/style.css @@ -0,0 +1,24 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; +} +.blocks:last-child { + margin-right: 0; +} From 7fcffbbe9069540213da456278b20e5faf0d03bb Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 23 Mar 2019 16:00:34 +0530 Subject: [PATCH 005/161] added a script that shows what closure is. --- Functions/index.html | 3 ++- Functions/script.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Functions/index.html b/Functions/index.html index 67876c3..9f07dc7 100644 --- a/Functions/index.html +++ b/Functions/index.html @@ -16,7 +16,7 @@

    Functions In JS

    - Function in JS is set of statements that execute a certain tasks. + Function are set of statements that execute certain tasks.

    Advantage of functions

      @@ -64,5 +64,6 @@

      Arrow Functions

      Here number is called a parameter
    + diff --git a/Functions/script.js b/Functions/script.js index e69de29..cf22456 100644 --- a/Functions/script.js +++ b/Functions/script.js @@ -0,0 +1,34 @@ +function outer() { + let a = 10; + let c = 11; + + function inner() { + let d = 20; + console.log("OuterVar " + a); + console.log("InnerVar " + d); + a++; + d++; + console.log("IncrementedOuter " + a); + console.log("IncrementedInner " + d); + } + + return inner; +} + +let X = outer(); +let Y = outer(); + +X(); +X(); +X(); + +Y(); + +//this is an example for closure +//the inner() is closure that has access to the outer() functions scope and and can refrence its variables +//and change the outer variables value +//so for the inner function outer function is like global environment where it can alter the outer fucntion variables; + +//A Analogy I came up with to understand(though it sounds stupid) +//inner functions is like a king who says to outer function, I will protect your children(variable) becasue your +//children (variable) is my subject, even after you die, but i will change his/her value as I please. From ea55cdc0a39f5646b3fc4e8f8af025c17b6474e3 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 23 Mar 2019 21:49:25 +0530 Subject: [PATCH 006/161] Added notes about closure --- Functions/index.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ Functions/style.css | 1 + 2 files changed, 45 insertions(+) diff --git a/Functions/index.html b/Functions/index.html index 9f07dc7..c0a5324 100644 --- a/Functions/index.html +++ b/Functions/index.html @@ -64,6 +64,50 @@

    Arrow Functions

    Here number is called a parameter + +

    Closure In Functions

    +

    + Closure is a function that is enclosed within a function, that have access + to the outer functions variables and scope. This closes the outer + functions variable within it and uses it when inner function is executed. + Its like the inner function captures the variables from outer function and + keeps it for later use. +

    + +
    +
    + Simple Eg for Closure that make sense to me: +
    +            function outer(x){
    +              function inner(y){
    +                return x*y;
    +              }
    +              return inner;
    +            }
    +
    +        
    + Calling the Function: since we can pass function as a value we can do + the following: +
    +          let multiply = outer(50);
    +          multiply(25);
    +        
    +
    +

    + Solution will be:1250;
    + Explanation: When we pass the outer function to variable multiply + we are calling the outer function and passing a parameter value along + with assigning the variable value to inner(). When we call the variable + multiply we call the inner function that has the outer functions value + of 5 and uses to get the result. So even when the outer function got + executed, the inner function still had the value of x and was able use + it get the result. +

    +

    + Inner function can also alter the outer functions variable value. +

    +
    + diff --git a/Functions/style.css b/Functions/style.css index a5f1aa1..9725ef2 100644 --- a/Functions/style.css +++ b/Functions/style.css @@ -18,6 +18,7 @@ body { padding: 10px; background: #eee; margin-right: 10px; + flex: 1 1 0; } .blocks:last-child { margin-right: 0; From 547b622053062d7d057716e6dd06b6cd3ff0b24e Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 23 Mar 2019 21:54:04 +0530 Subject: [PATCH 007/161] Added topic Recursion . --- Functions/index.html | 5 ++++- Functions/style.css | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Functions/index.html b/Functions/index.html index c0a5324..328d3d7 100644 --- a/Functions/index.html +++ b/Functions/index.html @@ -92,9 +92,9 @@

    Closure In Functions

    let multiply = outer(50); multiply(25); + Solution will be:1250;

    - Solution will be:1250;
    Explanation: When we pass the outer function to variable multiply we are calling the outer function and passing a parameter value along with assigning the variable value to inner(). When we call the variable @@ -108,6 +108,9 @@

    Closure In Functions

    +

    Recursion

    +

    Recursion happens when a function calls itself.

    + diff --git a/Functions/style.css b/Functions/style.css index 9725ef2..337e85a 100644 --- a/Functions/style.css +++ b/Functions/style.css @@ -13,6 +13,7 @@ body { .blocks-container { display: flex; + flex-wrap: wrap; } .blocks { padding: 10px; From d127b3a3e688da04dc2e2085e38d270ebdbe4150 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 27 Mar 2019 21:10:51 +0530 Subject: [PATCH 008/161] Functions first assignment --- Functions/{ => Notes}/README.md | 0 Functions/{ => Notes}/index.html | 0 Functions/{ => Notes}/script.js | 0 Functions/{ => Notes}/style.css | 0 .../functions-excercises-minimum/README.md | 7 +++++ .../functions-excercises-minimum/index.html | 17 ++++++++++++ .../functions-excercises-minimum/script.js | 8 ++++++ .../functions-excercises-minimum/style.css | 26 +++++++++++++++++++ 8 files changed, 58 insertions(+) rename Functions/{ => Notes}/README.md (100%) rename Functions/{ => Notes}/index.html (100%) rename Functions/{ => Notes}/script.js (100%) rename Functions/{ => Notes}/style.css (100%) create mode 100644 Functions/functions-excercises-minimum/README.md create mode 100644 Functions/functions-excercises-minimum/index.html create mode 100644 Functions/functions-excercises-minimum/script.js create mode 100644 Functions/functions-excercises-minimum/style.css diff --git a/Functions/README.md b/Functions/Notes/README.md similarity index 100% rename from Functions/README.md rename to Functions/Notes/README.md diff --git a/Functions/index.html b/Functions/Notes/index.html similarity index 100% rename from Functions/index.html rename to Functions/Notes/index.html diff --git a/Functions/script.js b/Functions/Notes/script.js similarity index 100% rename from Functions/script.js rename to Functions/Notes/script.js diff --git a/Functions/style.css b/Functions/Notes/style.css similarity index 100% rename from Functions/style.css rename to Functions/Notes/style.css diff --git a/Functions/functions-excercises-minimum/README.md b/Functions/functions-excercises-minimum/README.md new file mode 100644 index 0000000..ff306f7 --- /dev/null +++ b/Functions/functions-excercises-minimum/README.md @@ -0,0 +1,7 @@ +# Eloquent-Javascript-Exercises + +These Exercises are from the chapter Function from the Book + +Exercise One: + +Minimum find minimum numbers diff --git a/Functions/functions-excercises-minimum/index.html b/Functions/functions-excercises-minimum/index.html new file mode 100644 index 0000000..fd7cb33 --- /dev/null +++ b/Functions/functions-excercises-minimum/index.html @@ -0,0 +1,17 @@ + + + + + + + + + Codestin Search App + + + + + diff --git a/Functions/functions-excercises-minimum/script.js b/Functions/functions-excercises-minimum/script.js new file mode 100644 index 0000000..7c931de --- /dev/null +++ b/Functions/functions-excercises-minimum/script.js @@ -0,0 +1,8 @@ +function(a,b){ + if(a Date: Thu, 28 Mar 2019 21:00:28 +0530 Subject: [PATCH 009/161] Functions min exercise done --- Functions/Recursion-exercise/README.md | 7 +++++++ Functions/Recursion-exercise/index.html | 17 ++++++++++++++++ Functions/Recursion-exercise/script.js | 10 ++++++++++ Functions/Recursion-exercise/style.css | 26 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 Functions/Recursion-exercise/README.md create mode 100644 Functions/Recursion-exercise/index.html create mode 100644 Functions/Recursion-exercise/script.js create mode 100644 Functions/Recursion-exercise/style.css diff --git a/Functions/Recursion-exercise/README.md b/Functions/Recursion-exercise/README.md new file mode 100644 index 0000000..7dd9441 --- /dev/null +++ b/Functions/Recursion-exercise/README.md @@ -0,0 +1,7 @@ +# Eloquent-Javascript-Exercises + +These Exercises are from the chapter Functions from the Book + +Exercise One: + +Find minimum numbers diff --git a/Functions/Recursion-exercise/index.html b/Functions/Recursion-exercise/index.html new file mode 100644 index 0000000..fd7cb33 --- /dev/null +++ b/Functions/Recursion-exercise/index.html @@ -0,0 +1,17 @@ + + + + + + + + + Codestin Search App + + + + + diff --git a/Functions/Recursion-exercise/script.js b/Functions/Recursion-exercise/script.js new file mode 100644 index 0000000..957ea37 --- /dev/null +++ b/Functions/Recursion-exercise/script.js @@ -0,0 +1,10 @@ +function(a,b){ + if(a Date: Thu, 28 Mar 2019 21:03:23 +0530 Subject: [PATCH 010/161] script file in function folder changed --- Functions/functions-excercises-minimum/script.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Functions/functions-excercises-minimum/script.js b/Functions/functions-excercises-minimum/script.js index 7c931de..f03a467 100644 --- a/Functions/functions-excercises-minimum/script.js +++ b/Functions/functions-excercises-minimum/script.js @@ -1,8 +1,9 @@ -function(a,b){ - if(a Date: Sat, 30 Mar 2019 20:31:54 +0530 Subject: [PATCH 011/161] Added note reagrding IIFE --- Functions/Notes/index.html | 1 + Functions/Notes/script-iife.js | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 Functions/Notes/script-iife.js diff --git a/Functions/Notes/index.html b/Functions/Notes/index.html index 328d3d7..beccf8c 100644 --- a/Functions/Notes/index.html +++ b/Functions/Notes/index.html @@ -112,5 +112,6 @@

    Recursion

    Recursion happens when a function calls itself.

    + diff --git a/Functions/Notes/script-iife.js b/Functions/Notes/script-iife.js new file mode 100644 index 0000000..f12f967 --- /dev/null +++ b/Functions/Notes/script-iife.js @@ -0,0 +1,9 @@ +//The below code is called a IIFE +//it gets immedialely after its written +//dont have to invoke it separately +var result = (function() { + let person = "Sam"; + return person; +})(); + +console.log(result); From 8efbf61fdf37a859d741ae7e3a1694d003087b1d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 4 Apr 2019 08:00:56 +0530 Subject: [PATCH 012/161] Notes on Array --- Arrays/Notes/README.md | 5 +++++ Arrays/Notes/index.html | 37 ++++++++++++++++++++++++++++++++++ Arrays/Notes/script-iife.js | 14 +++++++++++++ Arrays/Notes/script.js | 28 +++++++++++++++++++++++++ Arrays/Notes/style.css | 26 ++++++++++++++++++++++++ Functions/Notes/script-iife.js | 5 +++++ 6 files changed, 115 insertions(+) create mode 100644 Arrays/Notes/README.md create mode 100644 Arrays/Notes/index.html create mode 100644 Arrays/Notes/script-iife.js create mode 100644 Arrays/Notes/script.js create mode 100644 Arrays/Notes/style.css diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md new file mode 100644 index 0000000..5f4764a --- /dev/null +++ b/Arrays/Notes/README.md @@ -0,0 +1,5 @@ +# Eloquent-Javascript-Exercises + +Notes Related to Arrays in JS from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/Arrays/Notes/index.html b/Arrays/Notes/index.html new file mode 100644 index 0000000..309c6e9 --- /dev/null +++ b/Arrays/Notes/index.html @@ -0,0 +1,37 @@ + + + + + + + + + Codestin Search App + + +

    + Arrays In JS +

    +

    + Arrays are collections of values stored as a list. +

    + +

    Arrays Example

    +
    +
    +

    Array Example

    + Eg: +
    +         let sampleArr = ["John",5,true,undefined];
    +          
    + We can pass any values with any type into an array +
    +
    + + + + diff --git a/Arrays/Notes/script-iife.js b/Arrays/Notes/script-iife.js new file mode 100644 index 0000000..231b811 --- /dev/null +++ b/Arrays/Notes/script-iife.js @@ -0,0 +1,14 @@ +//The below code is called a IIFE +//it gets immedialely after its written +//dont have to invoke it separately +var result = (function() { + let person = "Sam"; + return person; +})(); + +console.log(result); + +// another example +(function(firstName, lastName) { + return firstName + lastName; +})("Sam", "Cherian"); diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js new file mode 100644 index 0000000..51a1ce3 --- /dev/null +++ b/Arrays/Notes/script.js @@ -0,0 +1,28 @@ +let sampleArr = ["Sam", 2, "Biriyani"]; + +// Adding a new value to array +//push() adds value at the last of an array +sampleArr.push("Animal Farm"); +//Adding a new value at the start of an array +//unshift(); + +console.log(sampleArr.unshift(666)); +console.log(sampleArr); + +//Removing a value from the back of an array +//pop() + +console.log(sampleArr.pop()); +console.log(sampleArr); + +//Removing a value from the beggining of an array +//unshift(); +console.log(sampleArr.shift()); +console.log(sampleArr); + +//splice() + +//it can be used to both add and delete values from array + +sampleArr.splice(0, 1); +console.log(sampleArr); diff --git a/Arrays/Notes/style.css b/Arrays/Notes/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/Arrays/Notes/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/Functions/Notes/script-iife.js b/Functions/Notes/script-iife.js index f12f967..231b811 100644 --- a/Functions/Notes/script-iife.js +++ b/Functions/Notes/script-iife.js @@ -7,3 +7,8 @@ var result = (function() { })(); console.log(result); + +// another example +(function(firstName, lastName) { + return firstName + lastName; +})("Sam", "Cherian"); From 2f18e9b57d1e74c1ed1fabd6be84e2dbc197f4e7 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Apr 2019 07:25:10 +0530 Subject: [PATCH 013/161] push and unshift in arrays pop and unshift() --- Arrays/Notes/script.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index 51a1ce3..f37f7de 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -2,27 +2,39 @@ let sampleArr = ["Sam", 2, "Biriyani"]; // Adding a new value to array //push() adds value at the last of an array -sampleArr.push("Animal Farm"); -//Adding a new value at the start of an array -//unshift(); - -console.log(sampleArr.unshift(666)); +//this doesnt create a new array +//this just returns the new array length +let pushArr = sampleArr.push("Animal Farm"); +console.log(pushArr); +console.log(sampleArr); +// ---------------------------------------------- +// Adding a new value at the start of an array +// unshift(); +let unshiftArr = sampleArr.unshift(666); +console.log(unshiftArr); console.log(sampleArr); //Removing a value from the back of an array //pop() -console.log(sampleArr.pop()); +let popArr = sampleArr.pop(); + +console.log(popArr); console.log(sampleArr); //Removing a value from the beggining of an array -//unshift(); -console.log(sampleArr.shift()); +//shift(); +let shiftArr = sampleArr.shift(); +console.log(shiftArr); console.log(sampleArr); +/* +pop() and unshift() return the value that has been removed from the array +*/ + //splice() //it can be used to both add and delete values from array -sampleArr.splice(0, 1); -console.log(sampleArr); +// sampleArr.splice(0, 1); +// console.log(sampleArr); From 68186f211cc5afbea5da222a68fdb582d25bfbaa Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Apr 2019 07:31:10 +0530 Subject: [PATCH 014/161] Learning about splice --- Arrays/Notes/script.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index f37f7de..26ae0b2 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -32,9 +32,10 @@ console.log(sampleArr); pop() and unshift() return the value that has been removed from the array */ -//splice() - +////splice() //it can be used to both add and delete values from array -// sampleArr.splice(0, 1); -// console.log(sampleArr); +let sampleSLice = sampleArr.splice(0, 1); +//Splice creates a new array +console.log("After splice ", sampleSLice); +console.log(sampleArr); From a58d53284f0f7932e77d4aba0b1929a416940783 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Apr 2019 07:35:26 +0530 Subject: [PATCH 015/161] Added notes about splice --- Arrays/Notes/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index 26ae0b2..90af7b0 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -34,7 +34,9 @@ pop() and unshift() return the value that has been removed from the array ////splice() //it can be used to both add and delete values from array - +//the below code removes an item from the array +//splice takes two params +//first is the index and second is the number of items to removed or added let sampleSLice = sampleArr.splice(0, 1); //Splice creates a new array console.log("After splice ", sampleSLice); From 3602cc60663ad61bf54e9558ebcc27d0a3771c79 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Apr 2019 07:42:00 +0530 Subject: [PATCH 016/161] Learned to add an item to array using splice function. --- Arrays/Notes/script.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index 90af7b0..cca9dd1 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -37,7 +37,11 @@ pop() and unshift() return the value that has been removed from the array //the below code removes an item from the array //splice takes two params //first is the index and second is the number of items to removed or added -let sampleSLice = sampleArr.splice(0, 1); +let sampleSplice = sampleArr.splice(0, 1); //Splice creates a new array -console.log("After splice ", sampleSLice); +console.log("After splice ", sampleSplice); +console.log(sampleArr); +let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); + +console.log("After splice ", sampleSpliceAdd); console.log(sampleArr); From 459da8e6df37f42d402cd9c69568126f92864988 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 6 Apr 2019 23:05:03 +0530 Subject: [PATCH 017/161] Unserstanding built-in functions. --- Arrays/Notes/index.html | 9 +++++++++ Arrays/Notes/script.js | 23 +++++++++++++++++++++++ Arrays/README.md | 6 ++++++ 3 files changed, 38 insertions(+) create mode 100644 Arrays/README.md diff --git a/Arrays/Notes/index.html b/Arrays/Notes/index.html index 309c6e9..5303295 100644 --- a/Arrays/Notes/index.html +++ b/Arrays/Notes/index.html @@ -32,6 +32,15 @@

    Array Example

    +

    + Add to the last of array +

    + + + + +
    + diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index cca9dd1..ac5a2e3 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -45,3 +45,26 @@ let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); console.log("After splice ", sampleSpliceAdd); console.log(sampleArr); + +let inputVal = document.getElementById("inputArr"); +let val = document.getElementById("vals"); +let arr = []; +let button = document.querySelector("button"); +let delButton = document.getElementById("popBtn"); + +button.addEventListener("click", function() { + arr.unshift(inputVal.value); + printArr(); + console.log(arr); +}); + +delButton.addEventListener("click", function() { + arr.pop(); + console.log(arr); +}); + +function printArr() { + for (let i = 0; i < arr.length; i++) { + val.innerText += " " + "[ " + arr[i] + " ]"; + } +} diff --git a/Arrays/README.md b/Arrays/README.md new file mode 100644 index 0000000..86d8053 --- /dev/null +++ b/Arrays/README.md @@ -0,0 +1,6 @@ +# Eloquent-Javascript-Exercises + +Notes about Arrays +Functions with Array +-push(); +-pop(); From b0306dd28c9f42a34e042d069aa755b938c3845e Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 6 Apr 2019 23:21:28 +0530 Subject: [PATCH 018/161] Added notes to Readme and testing markdown. --- Arrays/Notes/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 5f4764a..51909bc 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -3,3 +3,9 @@ Notes Related to Arrays in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods + +Array Functions: + +### Add values to the end + + -push(); From 50ca8fd481eb4a8d3856d04b52e911b54216c388 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 8 Apr 2019 07:13:50 +0530 Subject: [PATCH 019/161] Added new notes related to Array methods --- Arrays/Notes/README.md | 20 ++++++++++++++------ Arrays/Notes/script-iife.js | 14 -------------- Arrays/Notes/script.js | 7 +++++-- 3 files changed, 19 insertions(+), 22 deletions(-) delete mode 100644 Arrays/Notes/script-iife.js diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 51909bc..c20a538 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -1,11 +1,19 @@ -# Eloquent-Javascript-Exercises - -Notes Related to Arrays in JS from +#Notes Related to Arrays in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods -Array Functions: +Array Methods: + +### Add values to the end of the Array + + push("new array value"); + +### Add values to the start of the Array -### Add values to the end + unshift("new array value"); - -push(); +###splice() +It can be used to both add and delete values from array +splice takes two params +-first is the index +-second is the number of items to removed or added diff --git a/Arrays/Notes/script-iife.js b/Arrays/Notes/script-iife.js deleted file mode 100644 index 231b811..0000000 --- a/Arrays/Notes/script-iife.js +++ /dev/null @@ -1,14 +0,0 @@ -//The below code is called a IIFE -//it gets immedialely after its written -//dont have to invoke it separately -var result = (function() { - let person = "Sam"; - return person; -})(); - -console.log(result); - -// another example -(function(firstName, lastName) { - return firstName + lastName; -})("Sam", "Cherian"); diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index ac5a2e3..3e37fc1 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -46,6 +46,8 @@ let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); console.log("After splice ", sampleSpliceAdd); console.log(sampleArr); +//Testing array methods + let inputVal = document.getElementById("inputArr"); let val = document.getElementById("vals"); let arr = []; @@ -53,7 +55,8 @@ let button = document.querySelector("button"); let delButton = document.getElementById("popBtn"); button.addEventListener("click", function() { - arr.unshift(inputVal.value); + arr.push(inputVal.value); + // arr.unshift(inputVal.value); printArr(); console.log(arr); }); @@ -65,6 +68,6 @@ delButton.addEventListener("click", function() { function printArr() { for (let i = 0; i < arr.length; i++) { - val.innerText += " " + "[ " + arr[i] + " ]"; + val.innerText += " " + "[" + arr[i] + "]"; } } From fd5f2ef6ac70b8da2f7bcb1aed105433cf06ce95 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 8 Apr 2019 07:31:04 +0530 Subject: [PATCH 020/161] learned about slice method --- Arrays/Notes/README.md | 8 ++++++++ Arrays/Notes/script.js | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index c20a538..e3605a1 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -17,3 +17,11 @@ It can be used to both add and delete values from array splice takes two params -first is the index -second is the number of items to removed or added + +###slice() + +creates a copy array or we can create new sub-array from an array +it takes two params +-first one is the starting index and second last index +the result will the new sub array from starting index and going upto last index(not including the last index) +But if we dont specify the last index it will start and show all the values after that diff --git a/Arrays/Notes/script.js b/Arrays/Notes/script.js index 3e37fc1..162dae5 100644 --- a/Arrays/Notes/script.js +++ b/Arrays/Notes/script.js @@ -46,6 +46,16 @@ let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); console.log("After splice ", sampleSpliceAdd); console.log(sampleArr); +//slice +//creates a copy array or we can create new sub-array from an array +//it takes two params +//first one is the starting index and second last index +//the result will the new sub array from starting index and going upto last index(not including the last index) +//but if we dont specify the last index it will start and show all the values after that +let books = ["1984", "Animal Farm", "Randam Oozham", "Pathummade aadu"]; +let malayalamBooks = books.slice(2); +console.log(malayalamBooks); + //Testing array methods let inputVal = document.getElementById("inputArr"); From 9b24ec8c7cc5cfd32341bdeb731b84059e1c94f4 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 8 Apr 2019 07:32:47 +0530 Subject: [PATCH 021/161] Change in read me --- Arrays/Notes/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index e3605a1..277b0fc 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -12,13 +12,14 @@ Array Methods: unshift("new array value"); -###splice() +### splice() + It can be used to both add and delete values from array splice takes two params -first is the index -second is the number of items to removed or added -###slice() +### slice() creates a copy array or we can create new sub-array from an array it takes two params From 5bcc95dc3a71c4b02e0e930dc559175ea5680b8a Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 9 Apr 2019 07:45:07 +0530 Subject: [PATCH 022/161] Added bout join and concat --- Arrays/Notes/README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 277b0fc..888acf9 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -1,4 +1,4 @@ -#Notes Related to Arrays in JS from +# Notes Related to Arrays in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods @@ -24,5 +24,11 @@ splice takes two params creates a copy array or we can create new sub-array from an array it takes two params -first one is the starting index and second last index -the result will the new sub array from starting index and going upto last index(not including the last index) -But if we dont specify the last index it will start and show all the values after that +the result will be new sub array from starting index and going upto last index(not including the last index) +But if we dont specify the last index it will start and show all the values after that. + +### concat + +Joins Arrays together + +### join From d96c2cbf357ad697d243ca4ae33694d9b5eaeddd Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 9 Apr 2019 09:57:27 +0530 Subject: [PATCH 023/161] Updated Readme --- Arrays/README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Arrays/README.md b/Arrays/README.md index 86d8053..8f43559 100644 --- a/Arrays/README.md +++ b/Arrays/README.md @@ -1,6 +1,3 @@ # Eloquent-Javascript-Exercises -Notes about Arrays -Functions with Array --push(); --pop(); +Notes about Arrays and Exdrecises From a85052e540b33f8919b331eb0a5e6ec219e89fae Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 9 Apr 2019 11:19:09 +0530 Subject: [PATCH 024/161] Update README.md --- Arrays/Notes/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 888acf9..7210e68 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -32,3 +32,5 @@ But if we dont specify the last index it will start and show all the values afte Joins Arrays together ### join + +Join method joins the elements within an array with whatever is passed as arguement. The output will be string value. This arguement passed is used usually called *delimiter*. From 65bdb21d0027a8d00e0e66cbeb9f83546b050a1c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 10 Apr 2019 20:24:30 +0530 Subject: [PATCH 025/161] Array Methods Exercise --- Arrays/Exercises/README.md | 3 +++ Arrays/Exercises/index.html | 17 ++++++++++++++++ Arrays/Exercises/script.js | 39 +++++++++++++++++++++++++++++++++++++ Arrays/Exercises/style.css | 26 +++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 Arrays/Exercises/README.md create mode 100644 Arrays/Exercises/index.html create mode 100644 Arrays/Exercises/script.js create mode 100644 Arrays/Exercises/style.css diff --git a/Arrays/Exercises/README.md b/Arrays/Exercises/README.md new file mode 100644 index 0000000..cc9d626 --- /dev/null +++ b/Arrays/Exercises/README.md @@ -0,0 +1,3 @@ +# Array Related exercises from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/Arrays/Exercises/index.html b/Arrays/Exercises/index.html new file mode 100644 index 0000000..b2c4ff1 --- /dev/null +++ b/Arrays/Exercises/index.html @@ -0,0 +1,17 @@ + + + + + + + + + Codestin Search App + + + + + diff --git a/Arrays/Exercises/script.js b/Arrays/Exercises/script.js new file mode 100644 index 0000000..087a3dd --- /dev/null +++ b/Arrays/Exercises/script.js @@ -0,0 +1,39 @@ +//Part One +let arr = []; +arr = ["Sam"]; +arr.push("Cherian"); +arr.unshift("Anjmal Farm is my fav Book"); +arr.shift(0); +console.log(arr); + +arr2 = [15]; +arr2.push("JavaScript"); +//finding indexOf of an element +//no element found or nothing is passed +//it returns -1 +console.log(arr2.indexOf()); +let combineArr = arr.concat(arr2); +console.log(arr2); +console.log(combineArr); + +//Part Two +var newArr = ["JavaScript", "Python", "Ruby", "Java"]; +var fromNewArr = newArr.slice(1, 3); +console.log(fromNewArr); + +combinedNewArr = newArr.concat(["Haskell", "Clojure"]); +console.log(combinedNewArr); + +let newString = newArr.join(", "); +console.log(newString); + +// Trying to Understand Passing by value and passing by Reference + +var a = "Sam"; +var b = a; +console.log(a, b); +var b = "Bency"; +console.log(a, b); +// Here b value has changed +// but a value remains the same. +//changing b value havent affected the value of a. diff --git a/Arrays/Exercises/style.css b/Arrays/Exercises/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/Arrays/Exercises/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} From c3836228438680fdc00f8510fb37e8ebcf0a2656 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 11 Apr 2019 07:20:43 +0530 Subject: [PATCH 026/161] Added comments about passing by value and passing by reference. --- Arrays/Exercises/script.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Arrays/Exercises/script.js b/Arrays/Exercises/script.js index 087a3dd..b350792 100644 --- a/Arrays/Exercises/script.js +++ b/Arrays/Exercises/script.js @@ -37,3 +37,16 @@ console.log(a, b); // Here b value has changed // but a value remains the same. //changing b value havent affected the value of a. +//this passing by value +//need to research more on this + +var arrOne = [1, 2, 3, 4]; +console.log(arrOne); +var arrTwo = arrOne; +console.log(arrTwo); +arrTwo.push(5); +console.log("arrTwo after push" + arrTwo); +console.log("arrOne after push" + arrOne); + +//here any change in arrTwo affects arrOne +//becasue arrays are just referenced not passed as value From c0d9333674e261f0c0f75691494408de64428001 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 11 Apr 2019 07:24:36 +0530 Subject: [PATCH 027/161] correction in script file. --- Arrays/Exercises/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Arrays/Exercises/script.js b/Arrays/Exercises/script.js index b350792..7506657 100644 --- a/Arrays/Exercises/script.js +++ b/Arrays/Exercises/script.js @@ -2,7 +2,7 @@ let arr = []; arr = ["Sam"]; arr.push("Cherian"); -arr.unshift("Anjmal Farm is my fav Book"); +arr.unshift("Animal Farm is my fav Book"); arr.shift(0); console.log(arr); From ab841fe0d012d6ab9419f61b01bd385268f9a690 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 11 Apr 2019 07:47:37 +0530 Subject: [PATCH 028/161] Created new script file for exercise two --- Arrays/Exercises/index.html | 1 + Arrays/Exercises/script-exercise-two.js | 50 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Arrays/Exercises/script-exercise-two.js diff --git a/Arrays/Exercises/index.html b/Arrays/Exercises/index.html index b2c4ff1..d6ddd5e 100644 --- a/Arrays/Exercises/index.html +++ b/Arrays/Exercises/index.html @@ -13,5 +13,6 @@ + diff --git a/Arrays/Exercises/script-exercise-two.js b/Arrays/Exercises/script-exercise-two.js new file mode 100644 index 0000000..b5d004a --- /dev/null +++ b/Arrays/Exercises/script-exercise-two.js @@ -0,0 +1,50 @@ +//Exercise Two of Arrays + +var people = ["Greg", "Mary", "Devon", "James"]; +//Using a loop, iterate through this array and console.log all of the people. +for (let i = 0; i < people.length; i++) { + console.log(people[i]); +} + +// Write the command to remove "Greg" from the array. +people.shift(); +console.log(people); + +//Write the command to remove "James" from the array. + +people.pop(); +console.log(people); + +//Write the command to add "Matt" to the front of the array. + +people.unshift("Matt"); +console.log(people); + +//Write the command to add your name to the end of the array. + +people.push("Sam"); +console.log(people); + +//Using a loop, iterate through this array and after console.log-ing "Mary", exit from the loop +for (let i = 0; i < people.length; i++) { + console.log(people[1]); + break; +} +//Write the command to make a copy of the array using slice. The copy should NOT include "Mary" or "Matt". +var peopleCopy = people.slice(2); +console.log(peopleCopy); + +//Write the command that gives the indexOf where "Mary" is located. + +console.log(people.indexOf("Mary")); + +//Write the command that gives the indexOf where "Foo" is located (this should return -1). + +console.log(people.indexOf("Foo")); + +// Redefine the people variable with the value you started with. Using the splice command, remove "Devon" from the array and add "Elizabeth" and "Artie". Your array should look like this when you are done ["Greg", "Mary", "Elizabeth", "Artie", "James"]. + +people = ["Greg", "Mary", "Devon", "James"]; +var addNewPeople = people.splice(2, 1, "Elizabeth", "Artie"); +console.log(addNewPeople); +console.log(people); From b32b40933a5b64ca9ca59872d82c4ac23f5c32b4 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:01:48 +0530 Subject: [PATCH 029/161] last exercisein array fundamentals --- Arrays/Exercises/script-exercise-two.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Arrays/Exercises/script-exercise-two.js b/Arrays/Exercises/script-exercise-two.js index b5d004a..8c2a72c 100644 --- a/Arrays/Exercises/script-exercise-two.js +++ b/Arrays/Exercises/script-exercise-two.js @@ -48,3 +48,8 @@ people = ["Greg", "Mary", "Devon", "James"]; var addNewPeople = people.splice(2, 1, "Elizabeth", "Artie"); console.log(addNewPeople); console.log(people); + +// Create a new variable called withBob and set it equal to the people array concatenated with the string of "Bob". + +var withBob = people.concat("bob"); +console.table(withBob); From 69bc21cb0fef1661cbe913b4188208cf5ac49ba7 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:13:24 +0530 Subject: [PATCH 030/161] Created new folder for Objects lessons --- Arrays/Notes/README.md | 41 +++++------- Objects/Exercises/README.md | 3 + Objects/Exercises/index.html | 18 +++++ Objects/Exercises/script-exercise-two.js | 55 ++++++++++++++++ Objects/Exercises/script.js | 0 Objects/Exercises/style.css | 26 ++++++++ Objects/Notes/README.md | 36 ++++++++++ Objects/Notes/index.html | 46 +++++++++++++ Objects/Notes/script.js | 83 ++++++++++++++++++++++++ Objects/Notes/style.css | 26 ++++++++ Objects/README.md | 3 + 11 files changed, 311 insertions(+), 26 deletions(-) create mode 100644 Objects/Exercises/README.md create mode 100644 Objects/Exercises/index.html create mode 100644 Objects/Exercises/script-exercise-two.js create mode 100644 Objects/Exercises/script.js create mode 100644 Objects/Exercises/style.css create mode 100644 Objects/Notes/README.md create mode 100644 Objects/Notes/index.html create mode 100644 Objects/Notes/script.js create mode 100644 Objects/Notes/style.css create mode 100644 Objects/README.md diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 7210e68..77cebdd 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -1,36 +1,25 @@ -# Notes Related to Arrays in JS from +# Notes Related to Objects in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods -Array Methods: +## Objects Basics -### Add values to the end of the Array +Objects allows us to map key to values. - push("new array value"); +eg:
     
    -### Add values to the start of the Array
    +var employee = {
    +firstName:"Sam",
    +lastName:"Cherian",
    +designation:Developer
    +}
     
    -      unshift("new array value");
    +
    -### splice() +We can access objects values either using the dot notation or Bracket Notation -It can be used to both add and delete values from array -splice takes two params --first is the index --second is the number of items to removed or added +eg:
    +employee.firstName
    +result will be "Sam"
     
    -### slice()
    -
    -creates a copy array or we can create new sub-array from an array
    -it takes two params
    --first one is the starting index and second last index
    -the result will be new sub array from starting index and going upto last index(not including the last index)
    -But if we dont specify the last index it will start and show all the values after that.
    -
    -### concat
    -
    -Joins Arrays together
    -
    -### join
    -
    -Join method joins the elements within an array with whatever is passed as arguement. The output will be string value. This arguement passed is used usually called *delimiter*.
    +
    diff --git a/Objects/Exercises/README.md b/Objects/Exercises/README.md new file mode 100644 index 0000000..8eade75 --- /dev/null +++ b/Objects/Exercises/README.md @@ -0,0 +1,3 @@ +# Object Related exercises from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/Objects/Exercises/index.html b/Objects/Exercises/index.html new file mode 100644 index 0000000..d6ddd5e --- /dev/null +++ b/Objects/Exercises/index.html @@ -0,0 +1,18 @@ + + + + + + + + + Codestin Search App + + + + + + diff --git a/Objects/Exercises/script-exercise-two.js b/Objects/Exercises/script-exercise-two.js new file mode 100644 index 0000000..8c2a72c --- /dev/null +++ b/Objects/Exercises/script-exercise-two.js @@ -0,0 +1,55 @@ +//Exercise Two of Arrays + +var people = ["Greg", "Mary", "Devon", "James"]; +//Using a loop, iterate through this array and console.log all of the people. +for (let i = 0; i < people.length; i++) { + console.log(people[i]); +} + +// Write the command to remove "Greg" from the array. +people.shift(); +console.log(people); + +//Write the command to remove "James" from the array. + +people.pop(); +console.log(people); + +//Write the command to add "Matt" to the front of the array. + +people.unshift("Matt"); +console.log(people); + +//Write the command to add your name to the end of the array. + +people.push("Sam"); +console.log(people); + +//Using a loop, iterate through this array and after console.log-ing "Mary", exit from the loop +for (let i = 0; i < people.length; i++) { + console.log(people[1]); + break; +} +//Write the command to make a copy of the array using slice. The copy should NOT include "Mary" or "Matt". +var peopleCopy = people.slice(2); +console.log(peopleCopy); + +//Write the command that gives the indexOf where "Mary" is located. + +console.log(people.indexOf("Mary")); + +//Write the command that gives the indexOf where "Foo" is located (this should return -1). + +console.log(people.indexOf("Foo")); + +// Redefine the people variable with the value you started with. Using the splice command, remove "Devon" from the array and add "Elizabeth" and "Artie". Your array should look like this when you are done ["Greg", "Mary", "Elizabeth", "Artie", "James"]. + +people = ["Greg", "Mary", "Devon", "James"]; +var addNewPeople = people.splice(2, 1, "Elizabeth", "Artie"); +console.log(addNewPeople); +console.log(people); + +// Create a new variable called withBob and set it equal to the people array concatenated with the string of "Bob". + +var withBob = people.concat("bob"); +console.table(withBob); diff --git a/Objects/Exercises/script.js b/Objects/Exercises/script.js new file mode 100644 index 0000000..e69de29 diff --git a/Objects/Exercises/style.css b/Objects/Exercises/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/Objects/Exercises/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md new file mode 100644 index 0000000..7210e68 --- /dev/null +++ b/Objects/Notes/README.md @@ -0,0 +1,36 @@ +# Notes Related to Arrays in JS from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods + +Array Methods: + +### Add values to the end of the Array + + push("new array value"); + +### Add values to the start of the Array + + unshift("new array value"); + +### splice() + +It can be used to both add and delete values from array +splice takes two params +-first is the index +-second is the number of items to removed or added + +### slice() + +creates a copy array or we can create new sub-array from an array +it takes two params +-first one is the starting index and second last index +the result will be new sub array from starting index and going upto last index(not including the last index) +But if we dont specify the last index it will start and show all the values after that. + +### concat + +Joins Arrays together + +### join + +Join method joins the elements within an array with whatever is passed as arguement. The output will be string value. This arguement passed is used usually called *delimiter*. diff --git a/Objects/Notes/index.html b/Objects/Notes/index.html new file mode 100644 index 0000000..5303295 --- /dev/null +++ b/Objects/Notes/index.html @@ -0,0 +1,46 @@ + + + + + + + + + Codestin Search App + + +

    + Arrays In JS +

    +

    + Arrays are collections of values stored as a list. +

    + +

    Arrays Example

    +
    +
    +

    Array Example

    + Eg: +
    +         let sampleArr = ["John",5,true,undefined];
    +          
    + We can pass any values with any type into an array +
    +
    + +

    + Add to the last of array +

    + + + + +
    + + + + diff --git a/Objects/Notes/script.js b/Objects/Notes/script.js new file mode 100644 index 0000000..162dae5 --- /dev/null +++ b/Objects/Notes/script.js @@ -0,0 +1,83 @@ +let sampleArr = ["Sam", 2, "Biriyani"]; + +// Adding a new value to array +//push() adds value at the last of an array +//this doesnt create a new array +//this just returns the new array length +let pushArr = sampleArr.push("Animal Farm"); +console.log(pushArr); +console.log(sampleArr); +// ---------------------------------------------- +// Adding a new value at the start of an array +// unshift(); +let unshiftArr = sampleArr.unshift(666); +console.log(unshiftArr); +console.log(sampleArr); + +//Removing a value from the back of an array +//pop() + +let popArr = sampleArr.pop(); + +console.log(popArr); +console.log(sampleArr); + +//Removing a value from the beggining of an array +//shift(); +let shiftArr = sampleArr.shift(); +console.log(shiftArr); +console.log(sampleArr); + +/* +pop() and unshift() return the value that has been removed from the array +*/ + +////splice() +//it can be used to both add and delete values from array +//the below code removes an item from the array +//splice takes two params +//first is the index and second is the number of items to removed or added +let sampleSplice = sampleArr.splice(0, 1); +//Splice creates a new array +console.log("After splice ", sampleSplice); +console.log(sampleArr); +let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); + +console.log("After splice ", sampleSpliceAdd); +console.log(sampleArr); + +//slice +//creates a copy array or we can create new sub-array from an array +//it takes two params +//first one is the starting index and second last index +//the result will the new sub array from starting index and going upto last index(not including the last index) +//but if we dont specify the last index it will start and show all the values after that +let books = ["1984", "Animal Farm", "Randam Oozham", "Pathummade aadu"]; +let malayalamBooks = books.slice(2); +console.log(malayalamBooks); + +//Testing array methods + +let inputVal = document.getElementById("inputArr"); +let val = document.getElementById("vals"); +let arr = []; +let button = document.querySelector("button"); +let delButton = document.getElementById("popBtn"); + +button.addEventListener("click", function() { + arr.push(inputVal.value); + // arr.unshift(inputVal.value); + printArr(); + console.log(arr); +}); + +delButton.addEventListener("click", function() { + arr.pop(); + console.log(arr); +}); + +function printArr() { + for (let i = 0; i < arr.length; i++) { + val.innerText += " " + "[" + arr[i] + "]"; + } +} diff --git a/Objects/Notes/style.css b/Objects/Notes/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/Objects/Notes/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/Objects/README.md b/Objects/README.md new file mode 100644 index 0000000..f7131ff --- /dev/null +++ b/Objects/README.md @@ -0,0 +1,3 @@ +# Eloquent-Javascript-Exercises + +Notes about Objects and Exercises. From cf09790a2b19b59f5d13955d840b4dfecf0673ee Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:17:14 +0530 Subject: [PATCH 031/161] New notes regarding objects in readme --- Arrays/Notes/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 77cebdd..dab2b25 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -23,3 +23,5 @@ employee.firstName result will be "Sam" + +Bracket Notaion is used to access values in an array when we can't use dot notation From fd0a4edc9480d9f8e51c4392efaa575a858b8105 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:19:53 +0530 Subject: [PATCH 032/161] Readme changes in arrays and objects notes section. --- Arrays/Notes/README.md | 41 +++++++++++++++++++++++++---------------- Objects/Notes/README.md | 41 ++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index dab2b25..0cb6455 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -1,27 +1,36 @@ -# Notes Related to Objects in JS from +# Notes Related to Arrays in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods -## Objects Basics +Array Methods: -Objects allows us to map key to values. +### Add values to the end of the Array -eg:
    +      push("new array value");
     
    -var employee = {
    -firstName:"Sam",
    -lastName:"Cherian",
    -designation:Developer
    -}
    +### Add values to the start of the Array
     
    -
    + unshift("new array value"); -We can access objects values either using the dot notation or Bracket Notation +### splice() -eg:
    -employee.firstName
    -result will be "Sam"
    +It can be used to both add and delete values from array
    +splice takes two params
    +-first is the index
    +-second is the number of items to removed or added
     
    -
    +### slice() -Bracket Notaion is used to access values in an array when we can't use dot notation +creates a copy array or we can create new sub-array from an array +it takes two params +-first one is the starting index and second last index +the result will be new sub array from starting index and going upto last index(not including the last index) +But if we dont specify the last index it will start and show all the values after that. + +### concat + +Joins Arrays together + +### join + +Join method joins the elements within an array with whatever is passed as arguement. The output will be string value. This arguement passed is used usually called _delimiter_. diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md index 7210e68..dab2b25 100644 --- a/Objects/Notes/README.md +++ b/Objects/Notes/README.md @@ -1,36 +1,27 @@ -# Notes Related to Arrays in JS from +# Notes Related to Objects in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods -Array Methods: +## Objects Basics -### Add values to the end of the Array +Objects allows us to map key to values. - push("new array value"); +eg:
     
    -### Add values to the start of the Array
    +var employee = {
    +firstName:"Sam",
    +lastName:"Cherian",
    +designation:Developer
    +}
     
    -      unshift("new array value");
    +
    -### splice() +We can access objects values either using the dot notation or Bracket Notation -It can be used to both add and delete values from array -splice takes two params --first is the index --second is the number of items to removed or added +eg:
    +employee.firstName
    +result will be "Sam"
     
    -### slice()
    +
    -creates a copy array or we can create new sub-array from an array -it takes two params --first one is the starting index and second last index -the result will be new sub array from starting index and going upto last index(not including the last index) -But if we dont specify the last index it will start and show all the values after that. - -### concat - -Joins Arrays together - -### join - -Join method joins the elements within an array with whatever is passed as arguement. The output will be string value. This arguement passed is used usually called *delimiter*. +Bracket Notaion is used to access values in an array when we can't use dot notation From 18db56e412c3b7f116ba0fafe3e7f3f49d0cfa7d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:35:50 +0530 Subject: [PATCH 033/161] Object basics notes --- Objects/Notes/README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md index dab2b25..9762f9f 100644 --- a/Objects/Notes/README.md +++ b/Objects/Notes/README.md @@ -19,9 +19,36 @@ designation:Developer We can access objects values either using the dot notation or Bracket Notation eg:
    -employee.firstName
    +employee.firstName;
    +employee.lastName;
     result will be "Sam"
    +result will be "Cherian"
     
     
    Bracket Notaion is used to access values in an array when we can't use dot notation +eg:
    +
    +var obj = {};
    +var person = "Tom";
    +
    +obj[person]="This is a person";
    +obj[1+1+1] = "three";
    +
    +obj;
    +
    +
    + +when accessing +obj.3//this will result in syntax error +but obj[3] will give "three"; +also obj.person will also give error. +using obj[person] will give "This is a person" + +Use Bracket Notation when: +-Evaluating a expression +-Pass in a varaible to get the name of the key +Use Dot notaion when: +-we know name of the key +-when its not a varaible and expression +Need to research more on this topic From 36b98fe8e0c706ffa2c03d021ff9ec7d3c351549 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:41:06 +0530 Subject: [PATCH 034/161] Added new notes to object readme --- Objects/Notes/README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md index 9762f9f..a453848 100644 --- a/Objects/Notes/README.md +++ b/Objects/Notes/README.md @@ -51,4 +51,19 @@ Use Bracket Notation when: Use Dot notaion when: -we know name of the key -when its not a varaible and expression -Need to research more on this topic +Need to research more on this topic + +### Keys are always string in JS Objects. + +## Adding to Objects + +Adding properties or fuctions +Here also use either dot(.) or [] operator + +
    +      employee.knowsAngular = true
    +
    + +## Removing from Objects + +delete employee.designation; From 9d81346904c9a0a0993c88cc05d0357ab51f5afb Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 12 Apr 2019 07:42:32 +0530 Subject: [PATCH 035/161] Changed text from heading to emphasis in readme --- Objects/Notes/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md index a453848..6e9a1c0 100644 --- a/Objects/Notes/README.md +++ b/Objects/Notes/README.md @@ -53,7 +53,7 @@ Use Dot notaion when: -when its not a varaible and expression Need to research more on this topic -### Keys are always string in JS Objects. +Keys are always string in JS Objects. ## Adding to Objects From 2172fec235eb7c0d2eda2fa7f9ccaa408fb07d07 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 11:17:10 +0530 Subject: [PATCH 036/161] Completed basic object exercises. --- Objects/Exercises/script-exercise-two.js | 55 ------------------------ Objects/Exercises/script.js | 17 ++++++++ 2 files changed, 17 insertions(+), 55 deletions(-) delete mode 100644 Objects/Exercises/script-exercise-two.js diff --git a/Objects/Exercises/script-exercise-two.js b/Objects/Exercises/script-exercise-two.js deleted file mode 100644 index 8c2a72c..0000000 --- a/Objects/Exercises/script-exercise-two.js +++ /dev/null @@ -1,55 +0,0 @@ -//Exercise Two of Arrays - -var people = ["Greg", "Mary", "Devon", "James"]; -//Using a loop, iterate through this array and console.log all of the people. -for (let i = 0; i < people.length; i++) { - console.log(people[i]); -} - -// Write the command to remove "Greg" from the array. -people.shift(); -console.log(people); - -//Write the command to remove "James" from the array. - -people.pop(); -console.log(people); - -//Write the command to add "Matt" to the front of the array. - -people.unshift("Matt"); -console.log(people); - -//Write the command to add your name to the end of the array. - -people.push("Sam"); -console.log(people); - -//Using a loop, iterate through this array and after console.log-ing "Mary", exit from the loop -for (let i = 0; i < people.length; i++) { - console.log(people[1]); - break; -} -//Write the command to make a copy of the array using slice. The copy should NOT include "Mary" or "Matt". -var peopleCopy = people.slice(2); -console.log(peopleCopy); - -//Write the command that gives the indexOf where "Mary" is located. - -console.log(people.indexOf("Mary")); - -//Write the command that gives the indexOf where "Foo" is located (this should return -1). - -console.log(people.indexOf("Foo")); - -// Redefine the people variable with the value you started with. Using the splice command, remove "Devon" from the array and add "Elizabeth" and "Artie". Your array should look like this when you are done ["Greg", "Mary", "Elizabeth", "Artie", "James"]. - -people = ["Greg", "Mary", "Devon", "James"]; -var addNewPeople = people.splice(2, 1, "Elizabeth", "Artie"); -console.log(addNewPeople); -console.log(people); - -// Create a new variable called withBob and set it equal to the people array concatenated with the string of "Bob". - -var withBob = people.concat("bob"); -console.table(withBob); diff --git a/Objects/Exercises/script.js b/Objects/Exercises/script.js index e69de29..4f53ee6 100644 --- a/Objects/Exercises/script.js +++ b/Objects/Exercises/script.js @@ -0,0 +1,17 @@ +//Create an object that has your firstName, lastName, and occupation as keys. + +var me = { + firstName: "Sam", + lastName: "Cherian", + occupation: "Developer" +}; + +//Accessing Objects through dot and bracket notations +console.log(me.firstName); +console.log(me["lastName"]); +console.log(me.occupation); +//Adding hobby key with value +console.log((me.hobby = "Reading")); +//Deleting occupation +console.log(delete me.occupation); +console.log(me); From 629a653e255aef63e58ef2e8b55461cb5d09c8c2 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 11:21:18 +0530 Subject: [PATCH 037/161] Added two md files to take notes related to objects --- Objects/Notes/Objects-Iteration.md | 0 Objects/Notes/Objects-basics.md | 65 +++++++++++++++++++++++++++++ Objects/Notes/README.md | 66 ------------------------------ 3 files changed, 65 insertions(+), 66 deletions(-) create mode 100644 Objects/Notes/Objects-Iteration.md create mode 100644 Objects/Notes/Objects-basics.md diff --git a/Objects/Notes/Objects-Iteration.md b/Objects/Notes/Objects-Iteration.md new file mode 100644 index 0000000..e69de29 diff --git a/Objects/Notes/Objects-basics.md b/Objects/Notes/Objects-basics.md new file mode 100644 index 0000000..41d3e64 --- /dev/null +++ b/Objects/Notes/Objects-basics.md @@ -0,0 +1,65 @@ +## Objects Basics + +Objects allows us to map key to values. + +eg:
    +
    +var employee = {
    +firstName:"Sam",
    +lastName:"Cherian",
    +designation:Developer
    +}
    +
    +
    + +We can access objects values either using the dot notation or Bracket Notation + +eg:
    +employee.firstName;
    +employee.lastName;
    +result will be "Sam"
    +result will be "Cherian"
    +
    +
    + +Bracket Notaion is used to access values in an array when we can't use dot notation +eg:
    +
    +var obj = {};
    +var person = "Tom";
    +
    +obj[person]="This is a person";
    +obj[1+1+1] = "three";
    +
    +obj;
    +
    +
    + +when accessing +obj.3//this will result in syntax error +but obj[3] will give "three"; +also obj.person will also give error. +using obj[person] will give "This is a person" + +Use Bracket Notation when: +-Evaluating a expression +-Pass in a varaible to get the name of the key +Use Dot notaion when: +-we know name of the key +-when its not a varaible and expression +Need to research more on this topic + +Keys are always string in JS Objects. + +## Adding to Objects + +Adding properties or fuctions +Here also use either dot(.) or [] operator + +
    +      employee.knowsAngular = true
    +
    + +## Removing from Objects + +delete employee.designation; diff --git a/Objects/Notes/README.md b/Objects/Notes/README.md index 6e9a1c0..5915a1e 100644 --- a/Objects/Notes/README.md +++ b/Objects/Notes/README.md @@ -1,69 +1,3 @@ # Notes Related to Objects in JS from https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods - -## Objects Basics - -Objects allows us to map key to values. - -eg:
    -
    -var employee = {
    -firstName:"Sam",
    -lastName:"Cherian",
    -designation:Developer
    -}
    -
    -
    - -We can access objects values either using the dot notation or Bracket Notation - -eg:
    -employee.firstName;
    -employee.lastName;
    -result will be "Sam"
    -result will be "Cherian"
    -
    -
    - -Bracket Notaion is used to access values in an array when we can't use dot notation -eg:
    -
    -var obj = {};
    -var person = "Tom";
    -
    -obj[person]="This is a person";
    -obj[1+1+1] = "three";
    -
    -obj;
    -
    -
    - -when accessing -obj.3//this will result in syntax error -but obj[3] will give "three"; -also obj.person will also give error. -using obj[person] will give "This is a person" - -Use Bracket Notation when: --Evaluating a expression --Pass in a varaible to get the name of the key -Use Dot notaion when: --we know name of the key --when its not a varaible and expression -Need to research more on this topic - -Keys are always string in JS Objects. - -## Adding to Objects - -Adding properties or fuctions -Here also use either dot(.) or [] operator - -
    -      employee.knowsAngular = true
    -
    - -## Removing from Objects - -delete employee.designation; From 1c0977b32d654c895de1841f4af20b1211dd2df3 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 11:24:51 +0530 Subject: [PATCH 038/161] notes in objects basics --- Objects/Notes/Objects-basics.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Objects/Notes/Objects-basics.md b/Objects/Notes/Objects-basics.md index 41d3e64..88c43bb 100644 --- a/Objects/Notes/Objects-basics.md +++ b/Objects/Notes/Objects-basics.md @@ -41,12 +41,19 @@ but obj[3] will give "three"; also obj.person will also give error. using obj[person] will give "This is a person" -Use Bracket Notation when: --Evaluating a expression --Pass in a varaible to get the name of the key -Use Dot notaion when: --we know name of the key --when its not a varaible and expression +Use Bracket Notation when: + +
      +
    • *Evaluating a expression
    • +
    • *Pass in a varaible to get the name of the key
    • +
    + +Use Dot notaion when: +
      +
    • -we know name of the key
    • +
    • -when its not a varaible and expression
    • +
    + Need to research more on this topic Keys are always string in JS Objects. From 019327a262ad4a04eff40e7f5dc83da814b1b415 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 11:58:02 +0530 Subject: [PATCH 039/161] Objects itration notes and added code example code snippets in js file. --- Objects/Notes/Objects-Iteration.md | 38 ++++++++++++ Objects/Notes/Objects-basics.md | 11 +++- Objects/Notes/index.html | 31 +--------- Objects/Notes/script.js | 93 +++++------------------------- 4 files changed, 63 insertions(+), 110 deletions(-) diff --git a/Objects/Notes/Objects-Iteration.md b/Objects/Notes/Objects-Iteration.md index e69de29..6a2ceda 100644 --- a/Objects/Notes/Objects-Iteration.md +++ b/Objects/Notes/Objects-Iteration.md @@ -0,0 +1,38 @@ +## Looping Over objects + +### Using for in loop + +eg: + +
    +      var instructor = {
    +            name:"Sam",
    +            knowsJs:true,
    +            knowAngular:true,
    +      }
    +
    +      for(let info in instructor){
    +            console.log(info[instructor]);
    +      }
    +
    + +In this loop every key from object is assinged to info variable. +And to access the value we use the bracket notation. + +### Using if...in condition + +This is used to check if a key exists in an object. + +eg: + +
    +      var sampleObj = {
    +            name:"sam",
    +            favBook:"Animal Farm"
    +      }
    +
    +      if("favBook" in sampleObj){
    +            console.log("Fav book exists")
    +      }
    +
    +
    diff --git a/Objects/Notes/Objects-basics.md b/Objects/Notes/Objects-basics.md index 88c43bb..c72d1c5 100644 --- a/Objects/Notes/Objects-basics.md +++ b/Objects/Notes/Objects-basics.md @@ -36,7 +36,10 @@ obj; when accessing -obj.3//this will result in syntax error +obj.3 + +//this will result in syntax error + but obj[3] will give "three"; also obj.person will also give error. using obj[person] will give "This is a person" @@ -45,13 +48,13 @@ using obj[person] will give "This is a person"
    • *Evaluating a expression
    • -
    • *Pass in a varaible to get the name of the key
    • +
    • *Pass in a variable to get the name of the key
    Use Dot notaion when:
    • -we know name of the key
    • -
    • -when its not a varaible and expression
    • +
    • -when its not a variable and expression
    Need to research more on this topic @@ -69,4 +72,6 @@ Here also use either dot(.) or [] operator ## Removing from Objects +
     delete employee.designation;
    +
    diff --git a/Objects/Notes/index.html b/Objects/Notes/index.html index 5303295..d5d2133 100644 --- a/Objects/Notes/index.html +++ b/Objects/Notes/index.html @@ -9,38 +9,9 @@ rel="stylesheet" /> - Codestin Search App + Codestin Search App -

    - Arrays In JS -

    -

    - Arrays are collections of values stored as a list. -

    - -

    Arrays Example

    -
    -
    -

    Array Example

    - Eg: -
    -         let sampleArr = ["John",5,true,undefined];
    -          
    - We can pass any values with any type into an array -
    -
    - -

    - Add to the last of array -

    - - - - -
    - diff --git a/Objects/Notes/script.js b/Objects/Notes/script.js index 162dae5..8ec2ecc 100644 --- a/Objects/Notes/script.js +++ b/Objects/Notes/script.js @@ -1,83 +1,22 @@ -let sampleArr = ["Sam", 2, "Biriyani"]; +//for in iteration -// Adding a new value to array -//push() adds value at the last of an array -//this doesnt create a new array -//this just returns the new array length -let pushArr = sampleArr.push("Animal Farm"); -console.log(pushArr); -console.log(sampleArr); -// ---------------------------------------------- -// Adding a new value at the start of an array -// unshift(); -let unshiftArr = sampleArr.unshift(666); -console.log(unshiftArr); -console.log(sampleArr); +var instructor = { + name: "Sam", + knowsJs: true, + knowAngular: true +}; -//Removing a value from the back of an array -//pop() - -let popArr = sampleArr.pop(); - -console.log(popArr); -console.log(sampleArr); - -//Removing a value from the beggining of an array -//shift(); -let shiftArr = sampleArr.shift(); -console.log(shiftArr); -console.log(sampleArr); - -/* -pop() and unshift() return the value that has been removed from the array -*/ - -////splice() -//it can be used to both add and delete values from array -//the below code removes an item from the array -//splice takes two params -//first is the index and second is the number of items to removed or added -let sampleSplice = sampleArr.splice(0, 1); -//Splice creates a new array -console.log("After splice ", sampleSplice); -console.log(sampleArr); -let sampleSpliceAdd = sampleArr.splice(1, 0, "The God of small things"); - -console.log("After splice ", sampleSpliceAdd); -console.log(sampleArr); - -//slice -//creates a copy array or we can create new sub-array from an array -//it takes two params -//first one is the starting index and second last index -//the result will the new sub array from starting index and going upto last index(not including the last index) -//but if we dont specify the last index it will start and show all the values after that -let books = ["1984", "Animal Farm", "Randam Oozham", "Pathummade aadu"]; -let malayalamBooks = books.slice(2); -console.log(malayalamBooks); - -//Testing array methods - -let inputVal = document.getElementById("inputArr"); -let val = document.getElementById("vals"); -let arr = []; -let button = document.querySelector("button"); -let delButton = document.getElementById("popBtn"); +for (var info in instructor) { + console.log(instructor[info]); +} -button.addEventListener("click", function() { - arr.push(inputVal.value); - // arr.unshift(inputVal.value); - printArr(); - console.log(arr); -}); +//Check if key exists using if..in -delButton.addEventListener("click", function() { - arr.pop(); - console.log(arr); -}); +var sampleObj = { + name: "sam", + favBook: "Animal Farm" +}; -function printArr() { - for (let i = 0; i < arr.length; i++) { - val.innerText += " " + "[" + arr[i] + "]"; - } +if ("favBook" in sampleObj) { + console.log("Fav book exists"); } From e237b348b5ca0bc36fa567db79fc8208736842e5 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 16:15:05 +0530 Subject: [PATCH 040/161] Completed exercises related to basics of objects --- Objects/Exercises/script.js | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Objects/Exercises/script.js b/Objects/Exercises/script.js index 4f53ee6..f3e0902 100644 --- a/Objects/Exercises/script.js +++ b/Objects/Exercises/script.js @@ -15,3 +15,71 @@ console.log((me.hobby = "Reading")); //Deleting occupation console.log(delete me.occupation); console.log(me); + +//Given the following object below, write code to print the value then the key to the console separated by '=>': + +var namesAndHobbies = { + elie: "JavaScript", + matt: "jogging", + janey: "table building", + tim: "sailing" +}; + +//Add a key for your name, and a value for your favorite hobby to the namesAndHobbies object. + +namesAndHobbies.sam = "reading"; + +//Write an if statement that console.logs your name and hobby to the console if the key of your name is contained in the object. + +for (let objKey in namesAndHobbies) { + console.log(namesAndHobbies[objKey] + " => " + objKey); + if ("sam" in namesAndHobbies) { + console.log(objKey + ":" + namesAndHobbies.sam); + } +} + +//------------------------------------------------------------------------------------------ +var programming = { + languages: ["JavaScript", "Python", "Ruby"], + isChallenging: true, + isRewarding: true, + difficulty: 8, + jokes: + "http://stackoverflow.com/questions/234075/what-is-your-best-programmer-joke" +}; + +//Write the command to add the language "Go" to the end of the languages array. +programming.languages.push("GO"); +console.table(programming); + +//Change the difficulty to the value of 7. + +programming.difficulty = 7; + +console.table(programming); + +//Using the delete keyword, write the command to remove the jokes key from the programming object. + +delete programming.jokes; + +console.table(programming); + +//Write the command to add a new key called isFun and a value of true to the programming object. + +programming.isFun = true; +console.table(programming); + +//Using a loop, iterate through the languages array and console.log all of the languages. +for (let i = 0; i < programming.languages.length; i++) { + console.log(programming.languages[i]); +} +//Using a loop, console.log all of the keys in the programming object. + +for (objKeys in programming) { + console.log(objKeys); +} + +//Using a loop, console.log all of the values in the programming object. +for (objKeys in programming) { + console.log(programming[objKeys]); +} From 75acaa0e27407777bd90ec07e3f4bcd072049c18 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 13 Apr 2019 17:02:35 +0530 Subject: [PATCH 041/161] Functions basics exercises with arrays --- Functions/Exercises/README.md | 3 ++ Functions/Exercises/index.html | 16 +++++++ Functions/Exercises/script.js | 86 ++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 Functions/Exercises/README.md create mode 100644 Functions/Exercises/index.html create mode 100644 Functions/Exercises/script.js diff --git a/Functions/Exercises/README.md b/Functions/Exercises/README.md new file mode 100644 index 0000000..f44c525 --- /dev/null +++ b/Functions/Exercises/README.md @@ -0,0 +1,3 @@ +# These Exercises are from + +https://www.rithmschool.com/courses/javascript/javascript-functions-functions-basic diff --git a/Functions/Exercises/index.html b/Functions/Exercises/index.html new file mode 100644 index 0000000..1970dd9 --- /dev/null +++ b/Functions/Exercises/index.html @@ -0,0 +1,16 @@ + + + + + + + + Codestin Search App + + + + + diff --git a/Functions/Exercises/script.js b/Functions/Exercises/script.js new file mode 100644 index 0000000..0f426f3 --- /dev/null +++ b/Functions/Exercises/script.js @@ -0,0 +1,86 @@ +//Write a function called myName that logs your full name. Save your full name to a variable inside of the function body, then use console.log to print your name to the console. + +function myName() { + var fullName = "Sam Cherian"; + return fullName; +} + +console.log(myName()); + +//Create an array called favoriteFoods which contains the strings "pizza" and "ice cream". +// Write a function called randomFood. The function should use Math.random to randomly choose a favorite food in your favoriteFoods array to return. For example, your function will return either pizza or ice cream, depending on what you get back from Math.random. + +var favoriteFoods = ["pizza", "ice cream"]; + +function randomFood() { + for (let i = 0; i < favoriteFoods.length; i++) { + var randomFood = favoriteFoods[Math.round(Math.random())]; + } + console.log(randomFood); +} + +randomFood(); + +// Create a variable called numbers which is an array that contains the numbers 1 through 10. + +// Write a function called displayOddNumbers which iterates over the numbers array and console.logs out all of the numbers that are odd. Here is what that might look like: +var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + +function oddNumbers() { + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] % 2 !== 0) { + console.log("Odd Numbers " + numbers[i]); + } + } +} + +oddNumbers(); + +function evenNumbers() { + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] % 2 === 0) { + console.log("Even Numbers " + numbers[i]); + } + } +} + +evenNumbers(); + +function returnFirstOddNumber() { + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] % 2 !== 0) { + console.log("First Odd Number " + numbers[0]); + break; + } + } +} + +returnFirstOddNumber(); + +function returnFirstEvenNumber() { + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] % 2 === 0) { + console.log("First Even Number " + numbers[i]); + break; + } + } +} +returnFirstEvenNumber(); + +function returnFirstHalf() { + for (let i = 0; i < numbers.length; i++) { + var firstHalf = numbers.slice(0, numbers.length / 2); + } + console.log(firstHalf); +} + +returnFirstHalf(); + +function returnSecondHalf() { + for (let i = 0; i < numbers.length; i++) { + var secondHalf = numbers.slice(numbers.length / 2); + } + console.log(secondHalf); +} + +returnSecondHalf(); From 86d92972a8e4a218aa4fd7ea2dccee059c5b9441 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 14 Apr 2019 12:26:35 +0530 Subject: [PATCH 042/161] Functions basics exercises using parameters done --- Functions/Exercises/script.js | 124 ++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/Functions/Exercises/script.js b/Functions/Exercises/script.js index 0f426f3..2ce94b9 100644 --- a/Functions/Exercises/script.js +++ b/Functions/Exercises/script.js @@ -84,3 +84,127 @@ function returnSecondHalf() { } returnSecondHalf(); + +// Make a function for add, subtract, multiply, and divide. Each of these functions should accept two parameters and return the sum, difference, product and quotient. + +function add(numbOne, numbTwo) { + console.log(numbOne + numbTwo); +} +function subtract(numbOne, numbTwo) { + console.log(numbOne - numbTwo); +} + +function multiply(numbOne, numbTwo) { + console.log(numbOne * numbTwo); +} + +function divide(numbOne, numbTwo) { + console.log(numbOne / numbTwo); +} +add(5, 6); +subtract(5, 6); +multiply(5, 6); +divide(5, 6); + +// Write a function called sayHello that takes in a string as a parameter. If the parameter passed to the function is your first name, it should return "Hello Boss", but if the parameter passed to the function is any other name, it should return the string "Hello" and the name parameter. + +function sayHello(name) { + console.log(name === "Sam" ? "Hello Boss" : "Hello " + name); +} + +sayHello("Sam"); +sayHello("Bency"); + +// Write a function called average which accepts an array as a parameter. The function should return the average of all of the numbers in the array (you can assume that the array passed to the function will contain only numbers) + +function average(arr) { + let total = 0; + for (var i = 0; i < arr.length; i++) { + total += arr[i]; + } + console.log(total / 2); +} + +average([1, 2, 3]); +average([1, 2, 4, 5, 6, 8, 9, 10]); + +//note:when adding always add a variable with initial value and update that variable +// Write a function called createStudent, which accepts two parameters both of which are strings. The function should return an object with the keys firstName and lastName and the values should be each of the + +function createStudent(stringOne, stringTwo) { + var functionObj = { + firstName: stringOne, + secondName: stringTwo + }; + return functionObj; +} + +var studentOne = createStudent("Sam", "Cherian"); +var studentTwo = createStudent("Bency", "Cherian"); +var studentThree = createStudent("Joe", "Ann"); + +var students = [studentOne, studentTwo, studentThree]; +console.table(students); + +//Write a function called findStudentByFirstName, which accepts one parameter, a string. This function should iterate through the students array you just made and if the parameter passed to the function is the same as one of the first name's of the students, the function should return true. Otherwise it should return false. This function should be case insensitive so that you can search successfully regardless of capitalization + +function findStudentByFirstName(name) { + var lowecaseapara = name.toLowerCase(); + for (var i = 0; i < students.length; i++) { + if (lowecaseapara === students[i].firstName.toLowerCase()) { + console.log("There is a student with firstname " + students[i].firstName); + } + } + return false; +} + +findStudentByFirstName("sam"); +findStudentByFirstName("joe"); +findStudentByFirstName("bency"); +findStudentByFirstName("Monika"); + +// Write a function called extractEveryThird which accepts an array as a parameter. The function should iterate over the array and return a new array with every 3rd element in the array passed to the function. + +function extractEveryThird(arraypara) { + let newArr = []; + for (let i = 2; i < arraypara.length; i += 3) { + newArr.push(arraypara[i]); + } + console.log(newArr); +} + +extractEveryThird([1, 3, 4, 5, 6, 7, 8, 9, 10]); + +// Write a function called countEvensAndOdds which accepts an array as a parameter. This function should return an object with the count of even numbers and the count of odd numbers. The object returned should have the keys oddCount and evenCount. +function countEvensAndOdds(oddevenarr) { + var odds = 0; + var evens = 0; + for (let i = 0; i < oddevenarr.length; i++) { + if (oddevenarr[i] % 2 === 0) { + evens++; + } else { + odds++; + } + } + var resultobj = { + oddCount: odds, + eventCount: evens + }; + console.log(resultobj); +} + +countEvensAndOdds([1, 2, 3, 4]); +countEvensAndOdds([1, 2, 3, 4, 5, 6, 7]); + +function onlyCapitalLetters(capString) { + var onlyCapitalLetters = ""; + for (let i = 0; i < capString.length; i++) { + onlyCapitalLetters = capString[i]; + if (onlyCapitalLetters === capString[i].toUpperCase()) { + console.log(onlyCapitalLetters); + } + } + return false; +} +onlyCapitalLetters("Amazing"); +onlyCapitalLetters("NothIng"); From 6314e185defe297e38bdf12e5bda583443c42807 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 14 Apr 2019 18:44:14 +0530 Subject: [PATCH 043/161] Moved all the different exercises into separate js files. --- Functions/Exercises/function-parameters.js | 123 +++++++++++++++++ .../Exercises/functions-anoynmous-iife.js | 28 ++++ Functions/Exercises/index.html | 2 + Functions/Exercises/script.js | 124 ------------------ 4 files changed, 153 insertions(+), 124 deletions(-) create mode 100644 Functions/Exercises/function-parameters.js create mode 100644 Functions/Exercises/functions-anoynmous-iife.js diff --git a/Functions/Exercises/function-parameters.js b/Functions/Exercises/function-parameters.js new file mode 100644 index 0000000..5f1f3e6 --- /dev/null +++ b/Functions/Exercises/function-parameters.js @@ -0,0 +1,123 @@ +// Make a function for add, subtract, multiply, and divide. Each of these functions should accept two parameters and return the sum, difference, product and quotient. + +function add(numbOne, numbTwo) { + console.log(numbOne + numbTwo); +} +function subtract(numbOne, numbTwo) { + console.log(numbOne - numbTwo); +} + +function multiply(numbOne, numbTwo) { + console.log(numbOne * numbTwo); +} + +function divide(numbOne, numbTwo) { + console.log(numbOne / numbTwo); +} +add(5, 6); +subtract(5, 6); +multiply(5, 6); +divide(5, 6); + +// Write a function called sayHello that takes in a string as a parameter. If the parameter passed to the function is your first name, it should return "Hello Boss", but if the parameter passed to the function is any other name, it should return the string "Hello" and the name parameter. + +function sayHello(name) { + console.log(name === "Sam" ? "Hello Boss" : "Hello " + name); +} + +sayHello("Sam"); +sayHello("Bency"); + +// Write a function called average which accepts an array as a parameter. The function should return the average of all of the numbers in the array (you can assume that the array passed to the function will contain only numbers) + +function average(arr) { + let total = 0; + for (var i = 0; i < arr.length; i++) { + total += arr[i]; + } + console.log(total / 2); +} + +average([1, 2, 3]); +average([1, 2, 4, 5, 6, 8, 9, 10]); + +//note:when adding always add a variable with initial value and update that variable +// Write a function called createStudent, which accepts two parameters both of which are strings. The function should return an object with the keys firstName and lastName and the values should be each of the + +function createStudent(stringOne, stringTwo) { + var functionObj = { + firstName: stringOne, + secondName: stringTwo + }; + return functionObj; +} + +var studentOne = createStudent("Sam", "Cherian"); +var studentTwo = createStudent("Bency", "Cherian"); +var studentThree = createStudent("Joe", "Ann"); + +var students = [studentOne, studentTwo, studentThree]; +console.table(students); + +//Write a function called findStudentByFirstName, which accepts one parameter, a string. This function should iterate through the students array you just made and if the parameter passed to the function is the same as one of the first name's of the students, the function should return true. Otherwise it should return false. This function should be case insensitive so that you can search successfully regardless of capitalization + +function findStudentByFirstName(name) { + var lowecaseapara = name.toLowerCase(); + for (var i = 0; i < students.length; i++) { + if (lowecaseapara === students[i].firstName.toLowerCase()) { + console.log("There is a student with firstname " + students[i].firstName); + } + } + return false; +} + +findStudentByFirstName("sam"); +findStudentByFirstName("joe"); +findStudentByFirstName("bency"); +findStudentByFirstName("Monika"); + +// Write a function called extractEveryThird which accepts an array as a parameter. The function should iterate over the array and return a new array with every 3rd element in the array passed to the function. + +function extractEveryThird(arraypara) { + let newArr = []; + for (let i = 2; i < arraypara.length; i += 3) { + newArr.push(arraypara[i]); + } + console.log(newArr); +} + +extractEveryThird([1, 3, 4, 5, 6, 7, 8, 9, 10]); + +// Write a function called countEvensAndOdds which accepts an array as a parameter. This function should return an object with the count of even numbers and the count of odd numbers. The object returned should have the keys oddCount and evenCount. +function countEvensAndOdds(oddevenarr) { + var odds = 0; + var evens = 0; + for (let i = 0; i < oddevenarr.length; i++) { + if (oddevenarr[i] % 2 === 0) { + evens++; + } else { + odds++; + } + } + var resultobj = { + oddCount: odds, + eventCount: evens + }; + console.log(resultobj); +} + +countEvensAndOdds([1, 2, 3, 4]); +countEvensAndOdds([1, 2, 3, 4, 5, 6, 7]); + +function onlyCapitalLetters(capString) { + var onlyCapitalLetters = ""; + for (let i = 0; i < capString.length; i++) { + onlyCapitalLetters = capString[i]; + if (onlyCapitalLetters === capString[i].toUpperCase()) { + console.log(onlyCapitalLetters); + } + } + return false; +} +onlyCapitalLetters("Amazing"); +onlyCapitalLetters("NothIng"); diff --git a/Functions/Exercises/functions-anoynmous-iife.js b/Functions/Exercises/functions-anoynmous-iife.js new file mode 100644 index 0000000..84b6031 --- /dev/null +++ b/Functions/Exercises/functions-anoynmous-iife.js @@ -0,0 +1,28 @@ +//Write a function called displayFullName, which should accept two parameters, firstName and lastName. The function should be immediately invoked and return the firstName + lastName. You should NOT have to call this function, it should invoke right away. + +(function displayFullName(firstName, lastName) { + console.log(firstName + " " + lastName); +})("Sam", "Cherian"); + +// Write a function called createCalculator, which should return an object that has four methods, add, subtract, multiply and divide. + +function createCalculator(numberOne, numberTwo) { + return { + add: function(numberOne, numberTwo) { + console.log(numberOne + numberTwo); + }, + minus: function(numberOne, numberTwo) { + return numberOne - numberTwo; + }, + multiply: function() { + return numberOne * numberTwo; + }, + multiply: function() { + return numberOne / numberTwo; + } + }; +} + +var calculating = createCalculator(); +console.log(calculating); +calculating.add(3, 5); diff --git a/Functions/Exercises/index.html b/Functions/Exercises/index.html index 1970dd9..d18bd6d 100644 --- a/Functions/Exercises/index.html +++ b/Functions/Exercises/index.html @@ -12,5 +12,7 @@ + + diff --git a/Functions/Exercises/script.js b/Functions/Exercises/script.js index 2ce94b9..0f426f3 100644 --- a/Functions/Exercises/script.js +++ b/Functions/Exercises/script.js @@ -84,127 +84,3 @@ function returnSecondHalf() { } returnSecondHalf(); - -// Make a function for add, subtract, multiply, and divide. Each of these functions should accept two parameters and return the sum, difference, product and quotient. - -function add(numbOne, numbTwo) { - console.log(numbOne + numbTwo); -} -function subtract(numbOne, numbTwo) { - console.log(numbOne - numbTwo); -} - -function multiply(numbOne, numbTwo) { - console.log(numbOne * numbTwo); -} - -function divide(numbOne, numbTwo) { - console.log(numbOne / numbTwo); -} -add(5, 6); -subtract(5, 6); -multiply(5, 6); -divide(5, 6); - -// Write a function called sayHello that takes in a string as a parameter. If the parameter passed to the function is your first name, it should return "Hello Boss", but if the parameter passed to the function is any other name, it should return the string "Hello" and the name parameter. - -function sayHello(name) { - console.log(name === "Sam" ? "Hello Boss" : "Hello " + name); -} - -sayHello("Sam"); -sayHello("Bency"); - -// Write a function called average which accepts an array as a parameter. The function should return the average of all of the numbers in the array (you can assume that the array passed to the function will contain only numbers) - -function average(arr) { - let total = 0; - for (var i = 0; i < arr.length; i++) { - total += arr[i]; - } - console.log(total / 2); -} - -average([1, 2, 3]); -average([1, 2, 4, 5, 6, 8, 9, 10]); - -//note:when adding always add a variable with initial value and update that variable -// Write a function called createStudent, which accepts two parameters both of which are strings. The function should return an object with the keys firstName and lastName and the values should be each of the - -function createStudent(stringOne, stringTwo) { - var functionObj = { - firstName: stringOne, - secondName: stringTwo - }; - return functionObj; -} - -var studentOne = createStudent("Sam", "Cherian"); -var studentTwo = createStudent("Bency", "Cherian"); -var studentThree = createStudent("Joe", "Ann"); - -var students = [studentOne, studentTwo, studentThree]; -console.table(students); - -//Write a function called findStudentByFirstName, which accepts one parameter, a string. This function should iterate through the students array you just made and if the parameter passed to the function is the same as one of the first name's of the students, the function should return true. Otherwise it should return false. This function should be case insensitive so that you can search successfully regardless of capitalization - -function findStudentByFirstName(name) { - var lowecaseapara = name.toLowerCase(); - for (var i = 0; i < students.length; i++) { - if (lowecaseapara === students[i].firstName.toLowerCase()) { - console.log("There is a student with firstname " + students[i].firstName); - } - } - return false; -} - -findStudentByFirstName("sam"); -findStudentByFirstName("joe"); -findStudentByFirstName("bency"); -findStudentByFirstName("Monika"); - -// Write a function called extractEveryThird which accepts an array as a parameter. The function should iterate over the array and return a new array with every 3rd element in the array passed to the function. - -function extractEveryThird(arraypara) { - let newArr = []; - for (let i = 2; i < arraypara.length; i += 3) { - newArr.push(arraypara[i]); - } - console.log(newArr); -} - -extractEveryThird([1, 3, 4, 5, 6, 7, 8, 9, 10]); - -// Write a function called countEvensAndOdds which accepts an array as a parameter. This function should return an object with the count of even numbers and the count of odd numbers. The object returned should have the keys oddCount and evenCount. -function countEvensAndOdds(oddevenarr) { - var odds = 0; - var evens = 0; - for (let i = 0; i < oddevenarr.length; i++) { - if (oddevenarr[i] % 2 === 0) { - evens++; - } else { - odds++; - } - } - var resultobj = { - oddCount: odds, - eventCount: evens - }; - console.log(resultobj); -} - -countEvensAndOdds([1, 2, 3, 4]); -countEvensAndOdds([1, 2, 3, 4, 5, 6, 7]); - -function onlyCapitalLetters(capString) { - var onlyCapitalLetters = ""; - for (let i = 0; i < capString.length; i++) { - onlyCapitalLetters = capString[i]; - if (onlyCapitalLetters === capString[i].toUpperCase()) { - console.log(onlyCapitalLetters); - } - } - return false; -} -onlyCapitalLetters("Amazing"); -onlyCapitalLetters("NothIng"); From 0eb1185fc14f3fb77c52d039dc92859b5e100b5b Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 14 Apr 2019 22:54:59 +0530 Subject: [PATCH 044/161] Function basics final exercise part one done --- .../function-basics-final-exercises.js | 80 +++++++++++++++++++ Functions/Exercises/index.html | 1 + 2 files changed, 81 insertions(+) create mode 100644 Functions/Exercises/function-basics-final-exercises.js diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js new file mode 100644 index 0000000..e2a5ea7 --- /dev/null +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -0,0 +1,80 @@ +//Part One +//difference + +function difference(numOne, numOne) { + return numOne - numOne; +} + +difference(2, 2); + +// product + +function product(numbOne, numbTwo) { + return numbOne * numbTwo; +} +product(2, 2); + +//printDay + +//this function takes in one parameter (a number from 1-7) and returns the day of the week (1 is Sunday, 2 is Monday, 3 is Tuesday etc.). If the number is less than 1 or greater than 7, the function should return undefined; + +function printDay(dayNumb) { + var days = { + 1: "Sunday", + 2: "Monday", + 3: "Tuesday", + 4: "Wednesday", + 5: "Thursday", + 6: "Friday", + 7: "Saturday" + }; + console.log(days[dayNumb]); +} + +printDay(5); +printDay(8); +//thought of using for loop with this, by comparing parameter with a number + +//lastElement + +//this function takes in one parameter (an array) and returns the last value in the array. It should return undefined if the array is empty. + +function lastElement(arrParam) { + let lastEle = arrParam[arrParam.length - 1]; + + console.log(lastEle); +} + +lastElement([2, 3, 4]); +lastElement([]); + +//numberCompare +// this function takes in two parameters (both numbers). If the first is greater than the second, this function returns "First is greater". If the second number is greater than the first, the function returns "Second is greater". Otherwise the function returns "Numbers are equal" +function numberCompare(fisrstNum, secondNum) { + if (fisrstNum > secondNum) { + console.log("First is greater"); + } else if (fisrstNum < secondNum) { + console.log("Second is greater"); + } else { + console.log("Numbers are equal"); + } +} + +numberCompare(1, 1); +numberCompare(2, 1); +numberCompare(1, 2); + +//singleLetterCount +//this function takes in two parameters (two strings). The first parameter should be a word and the second should be a letter. The function returns the number of times that letter appears in the word. The function should be case insensitive (does not matter if the input is lowercase or uppercase). If the letter is not found in the word, the function should return 0. + +function singleLetterCount(word, letter) { + var count = 0; + for (let i = 0; i < word.length; i++) { + if (word[i].toLowerCase() === letter.toLowerCase()) { + count++; + } + } + console.log(count); +} + +singleLetterCount("Adam", "b"); diff --git a/Functions/Exercises/index.html b/Functions/Exercises/index.html index d18bd6d..136e040 100644 --- a/Functions/Exercises/index.html +++ b/Functions/Exercises/index.html @@ -14,5 +14,6 @@ + From 7c5a11cfa8c3bc81d355b8c921f4d2fb02a1d633 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 15 Apr 2019 07:41:17 +0530 Subject: [PATCH 045/161] Part 2 function exercise started --- .../function-basics-final-exercises.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index e2a5ea7..a998abe 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -78,3 +78,23 @@ function singleLetterCount(word, letter) { } singleLetterCount("Adam", "b"); + +//PART 2 +//multipleLetterCount + +//this function takes in one parameter (a string) and returns an object with the keys being the letters and the values being the count of the letter. + +function multipleLetterCount(wordParam) { + let letterObj = {}; + for (let i = 0; i < wordParam.length; i++) { + console.log(wordParam[i]); + if (!(wordParam[i] in letterObj)) { + console.log((letterObj[wordParam[i]] = 1)); + } else { + letterObj[wordParam[i]]++; + } + } + console.log(letterObj); +} + +multipleLetterCount("hello"); From 0dcfbf770d8b8a090b0a3c90f6fb156f15899999 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 15 Apr 2019 21:21:39 +0530 Subject: [PATCH 046/161] stuck on part2 question, got solution from website. --- Functions/Exercises/function-basics-final-exercises.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index a998abe..ff4024c 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -77,17 +77,17 @@ function singleLetterCount(word, letter) { console.log(count); } -singleLetterCount("Adam", "b"); +// singleLetterCount("Adam", "b"); //PART 2 //multipleLetterCount //this function takes in one parameter (a string) and returns an object with the keys being the letters and the values being the count of the letter. - +//need to understand this function multipleLetterCount(wordParam) { let letterObj = {}; for (let i = 0; i < wordParam.length; i++) { - console.log(wordParam[i]); + // console.log(wordParam[i]); if (!(wordParam[i] in letterObj)) { console.log((letterObj[wordParam[i]] = 1)); } else { From 566623f34adbd9ef5098644a5b370d03814cf73c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 16 Apr 2019 07:36:41 +0530 Subject: [PATCH 047/161] array manipulation exercise completed --- .../function-basics-final-exercises.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index ff4024c..65550b6 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -98,3 +98,36 @@ function multipleLetterCount(wordParam) { } multipleLetterCount("hello"); + +//arrayManipulation +//this function should take in at most four parameters (an array, command, location, and value). +//If the command is "remove" and the location is "end", the function should remove the last value in the array and return the value removed. (In this case, the function only needs three arguments.) +//If the command is "remove" and the location is "beginning", the function should remove the first value in the array and return the value removed. (In this case, the function only needs three arguments.) +//If the command is "add" and the location is "beginning", the function should add the value (fourth parameter) to the beginning of the array and return the array. +//If the command is "add" and the location is "end", the function should add the value (fourth parameter) to the end of the array and return the array. + +//arrayManipulation([1,2,3], "remove", "end"); // 3 +//arrayManipulation([1,2,3], "remove", "beginning"); // 1 +//arrayManipulation([1,2,3], "add", "beginning", 20); // [20,1,2,3] +//arrayManipulation([1,2,3], "add", "end", 30); // [1,2,3,30] + +function arrayManipulation(arr, command, location, value) { + if (command === "remove" && location === "end") { + let removedValEnd = arr.pop(); + console.log(removedValEnd); + } else if (command === "remove" && location === "beginning") { + let removedValBeginning = arr.shift(); + console.log(removedValBeginning); + } else if (command === "add" && location === "beginning") { + arr.unshift(value); + console.log(arr); + } else if (command === "add" && location === "end") { + arr.push(value); + console.log(arr); + } +} + +arrayManipulation([1, 2, 3], "remove", "end"); +arrayManipulation([1, 2, 3], "remove", "beginning"); +arrayManipulation([1, 2, 3], "add", "beginning", 20); +arrayManipulation([1, 2, 3], "add", "end", 30); From 4dac2a07ce94c7b57184a554da9ba617c0a41ac8 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 17 Apr 2019 07:34:09 +0530 Subject: [PATCH 048/161] Paindrome test basics done --- .../function-basics-final-exercises.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 65550b6..c13e7fa 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -131,3 +131,20 @@ arrayManipulation([1, 2, 3], "remove", "end"); arrayManipulation([1, 2, 3], "remove", "beginning"); arrayManipulation([1, 2, 3], "add", "beginning", 20); arrayManipulation([1, 2, 3], "add", "end", 30); + +//isPalindrome + +function isPalindrome(str) { + let reversedString = ""; + for (let i = str.length - 1; i >= 0; i--) { + reversedString += str[i]; + } + if (reversedString === str) { + console.log(true); + console.log(reversedString); + } else { + console.log(false); + } +} + +isPalindrome("tacocat"); From 01690a11a25fd1dfd7ccf77c10771d5ddf6eeb72 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 17 Apr 2019 11:13:52 +0530 Subject: [PATCH 049/161] Update function-basics-final-exercises.js --- .../function-basics-final-exercises.js | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index c13e7fa..1fec8e4 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -134,17 +134,21 @@ arrayManipulation([1, 2, 3], "add", "end", 30); //isPalindrome -function isPalindrome(str) { - let reversedString = ""; - for (let i = str.length - 1; i >= 0; i--) { - reversedString += str[i]; - } - if (reversedString === str) { - console.log(true); - console.log(reversedString); - } else { - console.log(false); - } +function isPalindrome(str){ + let reversedStr = ""; + + for(let i=str.length-1;i>=0;i--){ + reversedStr += str[i]; + console.log(reversedStr); + } + console.log(reversedStr.toLowerCase()); + if(reversedStr.toLowerCase()===str.toLowerCase()){ + + console.log(true); + } + else{ + console.log(false); + } } -isPalindrome("tacocat"); +isPalindrome("Anna anna"); From 7887d7fba30a69562e723c6b7532799a02dde8ab Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 17 Apr 2019 11:15:36 +0530 Subject: [PATCH 050/161] Update function-basics-final-exercises.js --- Functions/Exercises/function-basics-final-exercises.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 1fec8e4..1d90302 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -136,15 +136,10 @@ arrayManipulation([1, 2, 3], "add", "end", 30); function isPalindrome(str){ let reversedStr = ""; - for(let i=str.length-1;i>=0;i--){ reversedStr += str[i]; - console.log(reversedStr); } - console.log(reversedStr.toLowerCase()); - if(reversedStr.toLowerCase()===str.toLowerCase()){ - - console.log(true); + if(reversedStr.toLowerCase()===str.toLowerCase()){ } else{ console.log(false); From 10798c23f3652d36e08ee8d20f7b602d7e9de0dd Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 17 Apr 2019 11:47:56 +0530 Subject: [PATCH 051/161] Update function-basics-final-exercises.js --- .../Exercises/function-basics-final-exercises.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 1d90302..c89960d 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -133,7 +133,8 @@ arrayManipulation([1, 2, 3], "add", "beginning", 20); arrayManipulation([1, 2, 3], "add", "end", 30); //isPalindrome - +//this is my solution +//but my solution doesn't solve the issue when you have long text with spaces in it. function isPalindrome(str){ let reversedStr = ""; for(let i=str.length-1;i>=0;i--){ @@ -147,3 +148,13 @@ function isPalindrome(str){ } isPalindrome("Anna anna"); +//another solution is to use inbuilt JS functions +function isPalindrome(str){ +console.log(str.toLowerCase().split("").reverse().join("")); +if(str.toLowerCase() === str.toLowerCase().split("").reverse().join("")){ +console.log(true); +} +} + + +} From 8abbf8c6a427919f6e94b23f95aaadb075ac09aa Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 18 Apr 2019 07:48:52 +0530 Subject: [PATCH 052/161] trying to correct palindrome exercise --- .../function-basics-final-exercises.js | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index c89960d..8878c3e 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -135,26 +135,36 @@ arrayManipulation([1, 2, 3], "add", "end", 30); //isPalindrome //this is my solution //but my solution doesn't solve the issue when you have long text with spaces in it. -function isPalindrome(str){ - let reversedStr = ""; - for(let i=str.length-1;i>=0;i--){ - reversedStr += str[i]; - } - if(reversedStr.toLowerCase()===str.toLowerCase()){ - } - else{ - console.log(false); - } -} +// function isPalindrome(str) { +// let reversedStr = ""; +// for (let i = str.length - 1; i >= 0; i--) { +// reversedStr += str[i]; +// } +// if (reversedStr.toLowerCase() === str.toLowerCase()) { +// } else { +// console.log(false); +// } +// } + +// isPalindrome("Anna anna"); -isPalindrome("Anna anna"); //another solution is to use inbuilt JS functions -function isPalindrome(str){ -console.log(str.toLowerCase().split("").reverse().join("")); -if(str.toLowerCase() === str.toLowerCase().split("").reverse().join("")){ -console.log(true); -} -} - - +function isPalindrome(str) { + var re = console.log( + str + .toLowerCase() + .replace(/\s/g, "") + .split("") + .reverse() + .join("") === str.toLowerCase() + ); + // console.log(str.length / 2); + // for (var i = 0; i < str.length / 2; i++) { + // if (str[i].toLowerCase() !== str[str.length - 1 - i].toLowerCase()) { + // return false; + // } + // } + // console.log(true); } + +isPalindrome("a man a plan a canal Panama"); From 431f2fedcfa1cfe1f7634b786cc90f95a6d35191 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 20 Apr 2019 23:33:05 +0530 Subject: [PATCH 053/161] Update function-basics-final-exercises.js --- Functions/Exercises/function-basics-final-exercises.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 8878c3e..587d767 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -168,3 +168,5 @@ function isPalindrome(str) { } isPalindrome("a man a plan a canal Panama"); + +//started last exrecise rock paper scissors From 599db44ee0b823b7c643a6d8ea6eb7dfb6127d34 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 Apr 2019 00:01:08 +0530 Subject: [PATCH 054/161] strated working on rock paper scissor --- .../function-basics-final-exercises.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 587d767..4be874d 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -152,11 +152,11 @@ arrayManipulation([1, 2, 3], "add", "end", 30); function isPalindrome(str) { var re = console.log( str - .toLowerCase() - .replace(/\s/g, "") - .split("") - .reverse() - .join("") === str.toLowerCase() + .toLowerCase() + .replace(/\s/g, "") + .split("") + .reverse() + .join("") === str.toLowerCase() ); // console.log(str.length / 2); // for (var i = 0; i < str.length / 2; i++) { @@ -170,3 +170,18 @@ function isPalindrome(str) { isPalindrome("a man a plan a canal Panama"); //started last exrecise rock paper scissors +var userChoice = prompt("Enter", "rock,paper,scissor"); + +function game() { + var gameChoices = ["rock", "paper", "scissor"]; + for (var i = 0; i < gameChoices.length; i++) { + var computerChoice = gameChoices[Math.floor(Math.random() * gameChoices.length)]; + if (userChoice === computerChoice) { + console.log("Both are equal") + } + break; + console.log(computerChoice); + } + +} +game(userChoice); \ No newline at end of file From 5aabf577c294091091a7a983cc8204afff22ab8f Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 Apr 2019 09:33:36 +0530 Subject: [PATCH 055/161] Update function-basics-final-exercises.js --- Functions/Exercises/function-basics-final-exercises.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 4be874d..9118a0f 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -179,9 +179,8 @@ function game() { if (userChoice === computerChoice) { console.log("Both are equal") } - break; console.log(computerChoice); } } -game(userChoice); \ No newline at end of file +game(userChoice); From e6f6798af9b9848af7c9d21d83505450c6c0233b Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 25 Apr 2019 23:52:09 +0530 Subject: [PATCH 056/161] Update function-basics-final-exercises.js --- Functions/Exercises/function-basics-final-exercises.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 9118a0f..4f039c0 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -170,6 +170,7 @@ function isPalindrome(str) { isPalindrome("a man a plan a canal Panama"); //started last exrecise rock paper scissors +//stuck on this var userChoice = prompt("Enter", "rock,paper,scissor"); function game() { From 7d9fe1c65eaafae5e9ac8bfb2d1c90e22648e430 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 26 Apr 2019 23:37:56 +0530 Subject: [PATCH 057/161] Made some code changes But not getting satisfactory result, search google. For Getting random element from array --- .../function-basics-final-exercises.js | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 4f039c0..86da390 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -170,18 +170,25 @@ function isPalindrome(str) { isPalindrome("a man a plan a canal Panama"); //started last exrecise rock paper scissors -//stuck on this -var userChoice = prompt("Enter", "rock,paper,scissor"); - function game() { + var userChoice = prompt("Enter", "rock,paper,scissor"); var gameChoices = ["rock", "paper", "scissor"]; for (var i = 0; i < gameChoices.length; i++) { var computerChoice = gameChoices[Math.floor(Math.random() * gameChoices.length)]; - if (userChoice === computerChoice) { - console.log("Both are equal") - } - console.log(computerChoice); + console.log(Math.floor(Math.random() * gameChoices.length)); + console.log(gameChoices.indexOf(computerChoice)); + break; + } + + if (userChoice !== computerChoice) { + console.log("Computer wins") + } else { + console.log("You win") + } + + if (userChoice === computerChoice) { + console.log("it's a tie") } } -game(userChoice); +game(); From 1426a2fb4bae0f8c6bee33755ecf8c06a2d18300 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 29 Apr 2019 07:52:06 +0530 Subject: [PATCH 058/161] Check for errors --- .../function-basics-final-exercises.js | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 86da390..3da4491 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -152,11 +152,11 @@ arrayManipulation([1, 2, 3], "add", "end", 30); function isPalindrome(str) { var re = console.log( str - .toLowerCase() - .replace(/\s/g, "") - .split("") - .reverse() - .join("") === str.toLowerCase() + .toLowerCase() + .replace(/\s/g, "") + .split("") + .reverse() + .join("") === str.toLowerCase() ); // console.log(str.length / 2); // for (var i = 0; i < str.length / 2; i++) { @@ -174,21 +174,23 @@ function game() { var userChoice = prompt("Enter", "rock,paper,scissor"); var gameChoices = ["rock", "paper", "scissor"]; for (var i = 0; i < gameChoices.length; i++) { - var computerChoice = gameChoices[Math.floor(Math.random() * gameChoices.length)]; - console.log(Math.floor(Math.random() * gameChoices.length)); - console.log(gameChoices.indexOf(computerChoice)); - break; - } - - if (userChoice !== computerChoice) { - console.log("Computer wins") - } else { - console.log("You win") + var computerChoice = + gameChoices[Math.floor(Math.random() * gameChoices.length)]; + return computerChoice; } if (userChoice === computerChoice) { - console.log("it's a tie") + console.log("it's a tie"); } + if (userChoice="scissor" && computerChoice="rock") { + console.log("Computer wins") + } + if(userChoice="paper" && computerChoice="scissor"){ + console.log("Computer Wins"); + } + if(userChoice="rock"&& computerChoice="paper"){ + console.log("Computer Wins"); + } } game(); From 35ade6585bf34b1e99733990c52f7fc00daf5848 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 29 Apr 2019 22:10:54 +0530 Subject: [PATCH 059/161] done with solutions --- .../function-basics-final-exercises.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index 3da4491..af795d5 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -176,21 +176,21 @@ function game() { for (var i = 0; i < gameChoices.length; i++) { var computerChoice = gameChoices[Math.floor(Math.random() * gameChoices.length)]; - return computerChoice; + break; } if (userChoice === computerChoice) { console.log("it's a tie"); } - if (userChoice="scissor" && computerChoice="rock") { - console.log("Computer wins") - } - if(userChoice="paper" && computerChoice="scissor"){ - console.log("Computer Wins"); - } - if(userChoice="rock"&& computerChoice="paper"){ - console.log("Computer Wins"); + if (userChoice === "scissor" && computerChoice === "rock") { + if (userChoice === "paper" && computerChoice === "scissor") { + if (userChoice === "rock" && computerChoice === "paper") { + console.log("Computer wins"); + } + } + } else { + console.log("you win"); } } game(); From 6836092469ec590f932f6a5bfaa9a8bed7fa5616 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 29 Apr 2019 23:00:37 +0530 Subject: [PATCH 060/161] Finally refined to get a working solution --- .../function-basics-final-exercises.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Functions/Exercises/function-basics-final-exercises.js b/Functions/Exercises/function-basics-final-exercises.js index af795d5..800539d 100644 --- a/Functions/Exercises/function-basics-final-exercises.js +++ b/Functions/Exercises/function-basics-final-exercises.js @@ -180,17 +180,26 @@ function game() { } if (userChoice === computerChoice) { - console.log("it's a tie"); + console.log("it's a tie" + userChoice + "=" + computerChoice); } if (userChoice === "scissor" && computerChoice === "rock") { - if (userChoice === "paper" && computerChoice === "scissor") { - if (userChoice === "rock" && computerChoice === "paper") { - console.log("Computer wins"); - } - } + console.log("Computer wins" + computerChoice); + return; + } + if (userChoice === "paper" && computerChoice === "scissor") { + console.log("Computer wins" + computerChoice); + return; + } + if (userChoice === "scissor" && computerChoice === "rock") { + console.log("Computer wins" + computerChoice); + return; + } + if (userChoice === "rock" && computerChoice === "paper") { + console.log("Computer wins" + computerChoice); + return; } else { - console.log("you win"); + console.log("you win" + userChoice); } } game(); From 1b86895e2322dd4cdbd4fccd3a83727479639484 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 30 Apr 2019 06:55:41 +0530 Subject: [PATCH 061/161] New Lessons on Debugging in JS --- Error and Debugging/Notes/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Error and Debugging/Notes/README.md diff --git a/Error and Debugging/Notes/README.md b/Error and Debugging/Notes/README.md new file mode 100644 index 0000000..a31ef8b --- /dev/null +++ b/Error and Debugging/Notes/README.md @@ -0,0 +1 @@ +### Notes Regarding Error and debugging From e22ced1326f20e49f7e0b5e712657ae713d3a99d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 30 Apr 2019 07:00:36 +0530 Subject: [PATCH 062/161] Added Read me --- Error and Debugging/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Error and Debugging/README.md diff --git a/Error and Debugging/README.md b/Error and Debugging/README.md new file mode 100644 index 0000000..cbdea49 --- /dev/null +++ b/Error and Debugging/README.md @@ -0,0 +1 @@ +### Exercises and Notes for the chapter Error and debugging From fee688da6b1a2332fbef6a0416ca7f9949cbe9ef Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 30 Apr 2019 07:13:36 +0530 Subject: [PATCH 063/161] JS Errors notes MD file added --- Error and Debugging/Notes/JS Errors.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Error and Debugging/Notes/JS Errors.md diff --git a/Error and Debugging/Notes/JS Errors.md b/Error and Debugging/Notes/JS Errors.md new file mode 100644 index 0000000..0be05eb --- /dev/null +++ b/Error and Debugging/Notes/JS Errors.md @@ -0,0 +1,8 @@ +

    All Errors in JS are objects that are created by a function called Error

    + +## Common JS Errors are + +SyntaxError-occurs when we errors with out syntax +ReferenceError-occurs when we try to access a variable that doesnt exist in that scope; +TypeError-occurs when invoking a function that does not exist or accessing properties on undefined. +RangeErrors-Occurs when we use recursive function. From d940b3da7ba2805129941da4e1e080abc2899503 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 30 Apr 2019 07:48:21 +0530 Subject: [PATCH 064/161] More notes added try/catch and throw --- Error and Debugging/Notes/JS Errors.md | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Error and Debugging/Notes/JS Errors.md b/Error and Debugging/Notes/JS Errors.md index 0be05eb..a66f6e7 100644 --- a/Error and Debugging/Notes/JS Errors.md +++ b/Error and Debugging/Notes/JS Errors.md @@ -2,7 +2,27 @@ ## Common JS Errors are -SyntaxError-occurs when we errors with out syntax -ReferenceError-occurs when we try to access a variable that doesnt exist in that scope; -TypeError-occurs when invoking a function that does not exist or accessing properties on undefined. +SyntaxError-occurs when we write wrong JS syntax; +ReferenceError-occurs when we try to access a variable that doesnt exist in that scope or access that has not been decalared; +TypeError-occurs when we use builtin js types incorrectly like accessing properties on undefined or invoking something that is not a function. RangeErrors-Occurs when we use recursive function. + +## Catching and Throwing Errors in JS + +### try/catch + +

    When we write a code to handle error gracefully we place code inside try block and if it throws error then it moves to catch block.

    + +
    +
    +try{
    +      thisVarnotthere;
    +}
    +
    +catch(e){
    +      console.log("error", e)
    +}
    +
    +
    + +to show proper messages we can use throw From 0f0c7c3d6e2a8b5d4f4f0344c03204a01722913a Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 1 May 2019 11:57:06 +0530 Subject: [PATCH 065/161] rename file and added definition for throw --- .../Notes/{JS Errors.md => JS Errors and debugging.md} | 2 +- Error and Debugging/Notes/debugging using sources.md | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Error and Debugging/Notes/{JS Errors.md => JS Errors and debugging.md} (93%) create mode 100644 Error and Debugging/Notes/debugging using sources.md diff --git a/Error and Debugging/Notes/JS Errors.md b/Error and Debugging/Notes/JS Errors and debugging.md similarity index 93% rename from Error and Debugging/Notes/JS Errors.md rename to Error and Debugging/Notes/JS Errors and debugging.md index a66f6e7..90322e1 100644 --- a/Error and Debugging/Notes/JS Errors.md +++ b/Error and Debugging/Notes/JS Errors and debugging.md @@ -25,4 +25,4 @@ catch(e){ -to show proper messages we can use throw +to show proper custom error messages we can use 'throw' diff --git a/Error and Debugging/Notes/debugging using sources.md b/Error and Debugging/Notes/debugging using sources.md new file mode 100644 index 0000000..e69de29 From b9b6b69feec32f9408387acd5b7656a7ac07aef7 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 1 May 2019 18:03:07 +0530 Subject: [PATCH 066/161] added what finally is --- Error and Debugging/Notes/JS Errors and debugging.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Error and Debugging/Notes/JS Errors and debugging.md b/Error and Debugging/Notes/JS Errors and debugging.md index 90322e1..459c291 100644 --- a/Error and Debugging/Notes/JS Errors and debugging.md +++ b/Error and Debugging/Notes/JS Errors and debugging.md @@ -25,4 +25,10 @@ catch(e){ +### throw + to show proper custom error messages we can use 'throw' + +### finally + +it will let to execute the code, after try and catch, regardless of the result. From da2fbcaf33e1054707e4155a5102338761fadbe4 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 2 May 2019 07:44:33 +0530 Subject: [PATCH 067/161] Started working on exercises in nested data structures --- Nested-Data-structures/Exercises/index.html | 12 ++++++++++ Nested-Data-structures/Exercises/script.js | 26 +++++++++++++++++++++ Nested-Data-structures/README.md | 3 +++ 3 files changed, 41 insertions(+) create mode 100644 Nested-Data-structures/Exercises/index.html create mode 100644 Nested-Data-structures/Exercises/script.js create mode 100644 Nested-Data-structures/README.md diff --git a/Nested-Data-structures/Exercises/index.html b/Nested-Data-structures/Exercises/index.html new file mode 100644 index 0000000..3f4ef02 --- /dev/null +++ b/Nested-Data-structures/Exercises/index.html @@ -0,0 +1,12 @@ + + + + + + + Codestin Search App + + + + + diff --git a/Nested-Data-structures/Exercises/script.js b/Nested-Data-structures/Exercises/script.js new file mode 100644 index 0000000..aaeee8d --- /dev/null +++ b/Nested-Data-structures/Exercises/script.js @@ -0,0 +1,26 @@ +var instructorData = { + name: "Elie", + additionalData: { + instructor: true, + favoriteHobbies: ["Playing Cello", "Tennis", "Coding"], + moreDetails: { + favoriteBasketballTeam: "New York Knicks", + numberOfSiblings: 3, + isYoungest: true, + hometown: { + city: "West Orange", + state: "NJ" + }, + citiesLivedIn: ["Seattle", "Providence", "New York"] + } + } +}; + +console.log(instructorData.name); +console.log(instructorData.additionalData.favoriteHobbies[2]); +console.log(instructorData.additionalData.moreDetails.favoriteBasketballTeam); +console.log(instructorData.additionalData.moreDetails.hometown.state); +console.log(instructorData.additionalData.moreDetails.citiesLivedIn); + +//Write a function called displayCities that console.logs all the values in the citiesLivedIn array: +function displayCities() {} diff --git a/Nested-Data-structures/README.md b/Nested-Data-structures/README.md new file mode 100644 index 0000000..e7e07e2 --- /dev/null +++ b/Nested-Data-structures/README.md @@ -0,0 +1,3 @@ +# Working with Nested Data structures + +Exercises on working with Nested Data Structures From d13876ccf4f862d6389725fc228641197f1aa413 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 3 May 2019 07:48:09 +0530 Subject: [PATCH 068/161] Functions to get result from nested data structure --- Nested-Data-structures/Exercises/script.js | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Nested-Data-structures/Exercises/script.js b/Nested-Data-structures/Exercises/script.js index aaeee8d..2e86d39 100644 --- a/Nested-Data-structures/Exercises/script.js +++ b/Nested-Data-structures/Exercises/script.js @@ -23,4 +23,25 @@ console.log(instructorData.additionalData.moreDetails.hometown.state); console.log(instructorData.additionalData.moreDetails.citiesLivedIn); //Write a function called displayCities that console.logs all the values in the citiesLivedIn array: -function displayCities() {} +function displayCities() { + for ( + var i = 0; + i < instructorData.additionalData.moreDetails.citiesLivedIn.length; + i++ + ) { + console.log(instructorData.additionalData.moreDetails.citiesLivedIn[i]); + } +} + +displayCities(); + +//Write a function called displayHometownData that console.logs all the values inside of the hometown object + +function displayHometownData() { + var homeTownList = instructorData.additionalData.moreDetails.hometown; + for (key in homeTownList) { + console.log(homeTownList[key]); + } +} + +displayHometownData(); From 3254410e33f2a6be96d9f93c03ae283d26c35a43 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 4 May 2019 20:34:54 +0530 Subject: [PATCH 069/161] Function adding detail to a nested object. --- Nested-Data-structures/Exercises/script.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Nested-Data-structures/Exercises/script.js b/Nested-Data-structures/Exercises/script.js index 2e86d39..ee15a74 100644 --- a/Nested-Data-structures/Exercises/script.js +++ b/Nested-Data-structures/Exercises/script.js @@ -45,3 +45,14 @@ function displayHometownData() { } displayHometownData(); + +//write a function called addDetail that accepts two parameters, a key and a value and adds the key and value to the moreDetails object and returns the moreDetails object + +function addDetail(key, val) { + var moredetails = instructorData.additionalData.moreDetails; + moredetails[key] = val; + console.log(moredetails); +} + +addDetail("isHilarous", true); +addDetail("previousApartments", ["Mission", "North Beach", "Nob Hill"]); From de825c00ad70aa2dbdfdd50d9e03673eef574ec0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 4 May 2019 21:11:03 +0530 Subject: [PATCH 070/161] removing the key in nested object using function --- Nested-Data-structures/Exercises/script.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Nested-Data-structures/Exercises/script.js b/Nested-Data-structures/Exercises/script.js index ee15a74..e83e877 100644 --- a/Nested-Data-structures/Exercises/script.js +++ b/Nested-Data-structures/Exercises/script.js @@ -51,8 +51,27 @@ displayHometownData(); function addDetail(key, val) { var moredetails = instructorData.additionalData.moreDetails; moredetails[key] = val; - console.log(moredetails); + return moredetails; + // console.log(moredetails); } addDetail("isHilarous", true); addDetail("previousApartments", ["Mission", "North Beach", "Nob Hill"]); + +//let's write a function called removeDetail that removes a key in the moreDetails object and returns the moreDetails object (the new keys added above are not included in these examples). + +function revmoeDetail(keyToBeRemoved) { + var moredetails = instructorData.additionalData.moreDetails; + // console.log(moredetails[keyToBeRemoved]); + // delete moredetails[keyToBeRemoved]; + + if (keyToBeRemoved in moredetails) { + delete moredetails[keyToBeRemoved]; + console.log(keyToBeRemoved + " has been deleted"); + } + console.log(moredetails); +} + +revmoeDetail("previousApartments"); +revmoeDetail("isHilarous"); +revmoeDetail("isHilarous"); From 712a68405ef5a9a3cf154432ae309cc0c96e0050 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 4 May 2019 21:19:01 +0530 Subject: [PATCH 071/161] remember to come back to these exercises --- Nested-Data-structures/Exercises/script.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Nested-Data-structures/Exercises/script.js b/Nested-Data-structures/Exercises/script.js index e83e877..4ceb3d1 100644 --- a/Nested-Data-structures/Exercises/script.js +++ b/Nested-Data-structures/Exercises/script.js @@ -69,9 +69,15 @@ function revmoeDetail(keyToBeRemoved) { delete moredetails[keyToBeRemoved]; console.log(keyToBeRemoved + " has been deleted"); } + + if (!(keyToBeRemoved in moredetails)) { + console.log(keyToBeRemoved + " does not exist"); + return; + } + console.log(moredetails); } revmoeDetail("previousApartments"); revmoeDetail("isHilarous"); -revmoeDetail("isHilarous"); +revmoeDetail("random"); From 3bbea966bbf185c7117c31f28da1471f3c789d75 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 5 May 2019 08:20:04 +0530 Subject: [PATCH 072/161] Started another set of exercises in nested objects --- .../advanced-nested-objects-exercises.js | 79 +++++++++++++++++++ Nested-Data-structures/Exercises/index.html | 1 + 2 files changed, 80 insertions(+) create mode 100644 Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js diff --git a/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js b/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js new file mode 100644 index 0000000..591ff6b --- /dev/null +++ b/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js @@ -0,0 +1,79 @@ +var nestedData = { + innerData: { + order: ["first", "second", "third"], + snacks: ["chips", "fruit", "crackers"], + numberData: { + primeNumbers: [2, 3, 5, 7, 11], + fibonnaci: [1, 1, 2, 3, 5, 8, 13] + }, + addSnack: function(snack) { + console.log("This is this " + this); + this.snacks.push(snack); + return this; + } + } +}; + +console.log("This is outer this " + this); +//Using a for loop, console.log all of the numbers in the primeNumbers array. + +var objPrimeNumbers = nestedData.innerData.numberData.primeNumbers; +var objEvenFiboNumbs = nestedData.innerData.numberData.fibonnaci; +for (let i = 0; i < objPrimeNumbers.length; i++) { + console.log("Prime Number are: " + objPrimeNumbers[i]); +} + +//Using a for loop, console.log all of the even Fibonnaci numbers. + +for (let i = 0; i < objEvenFiboNumbs.length; i++) { + if (objEvenFiboNumbs[i] % 2 === 0) { + console.log("Odd Fibonacci Number are: " + objEvenFiboNumbs[i]); + } +} + +//Console.log the value "second" inside the order array. +var orderArr = nestedData.innerData.order; +console.log(orderArr[1]); + +//Invoke the addSnack function and add the snack "chocolate". +var objFunc = nestedData.innerData.addSnack("chocolate"); +console.log(objFunc); + +var nestedObject = { + speakers: [{ name: "Elie" }, { name: "Tim" }, { name: "Matt" }], + data: { + continents: { + europe: { + countries: { + switzerland: { + capital: "Bern", + population: 8000000 + } + } + } + }, + languages: { + spanish: { + hello: "Hola" + }, + french: { + hello: "Bonjour" + } + } + } +}; + +// Write a function addSpeaker to add a speaker to the array of speakers. The speaker you add must be an object with a key of name and a value of whatever you'd like. + +function addSpeaker(speakerName) { + var speakerObj = nestedObject.speakers; + speakerObj.push({ name: speakerName }); + console.log(speakerObj); +} + +addSpeaker("Sam"); +addSpeaker("James Bond"); + +//Write a function addLanguage that adds a language to the languages object. The language object you add should have a key with the name of the language and the value of another object with a key of "hello" and a value of however you spell "hello" in the language you add. + +function addLanguage() {} diff --git a/Nested-Data-structures/Exercises/index.html b/Nested-Data-structures/Exercises/index.html index 3f4ef02..2ba401b 100644 --- a/Nested-Data-structures/Exercises/index.html +++ b/Nested-Data-structures/Exercises/index.html @@ -8,5 +8,6 @@ + From af31370c6d97054d65a7f84204602db7aaf45cae Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 5 May 2019 08:42:12 +0530 Subject: [PATCH 073/161] Completed Nested object exercises --- .../advanced-nested-objects-exercises.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js b/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js index 591ff6b..00a1f6c 100644 --- a/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js +++ b/Nested-Data-structures/Exercises/advanced-nested-objects-exercises.js @@ -76,4 +76,24 @@ addSpeaker("James Bond"); //Write a function addLanguage that adds a language to the languages object. The language object you add should have a key with the name of the language and the value of another object with a key of "hello" and a value of however you spell "hello" in the language you add. -function addLanguage() {} +function addLanguage(newLang, helloInLang) { + var langObj = nestedObject.data.languages; + langObj[newLang] = { hello: helloInLang }; + console.log(langObj); +} +addLanguage("Malayalam", "Namaskaram"); +addLanguage("German", "Hallo"); + +//Write a function addCountry that adds a European country to the countries object (inside of the continents object, inside of the countries object). The country you add should be an object with the key as name of the country and the value as an object with the keys of "capital" and "population" and their respective values. + +function addCountry(countryName, countryCapital, countryPopulation) { + let euObj = nestedObject.data.continents.europe.countries; + euObj[countryName] = { + capital: countryCapital, + population: countryPopulation + }; + console.log(euObj); +} + +addCountry("Germany", "Berlin", 500000); +addCountry("France", "Paris", 1000000); From b966ad361c90aef4f040af455b89a4b1bc51d94f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 7 May 2019 23:03:24 +0530 Subject: [PATCH 074/161] Exercises in Nested arrays --- Nested-Data-structures/Exercises/index.html | 1 + .../Exercises/nested-arrays.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 Nested-Data-structures/Exercises/nested-arrays.js diff --git a/Nested-Data-structures/Exercises/index.html b/Nested-Data-structures/Exercises/index.html index 2ba401b..a3c80b2 100644 --- a/Nested-Data-structures/Exercises/index.html +++ b/Nested-Data-structures/Exercises/index.html @@ -9,5 +9,6 @@ + diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js new file mode 100644 index 0000000..a60f687 --- /dev/null +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -0,0 +1,16 @@ +//Given the following array, write a function called printEvens that console.logs all of the even numbers +var nestedArr = [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12]]; + +function printEvens() { + for (let i = 0; i < nestedArr.length; i++) { + var singleArrVal = nestedArr[i]; + for (let j = 0; j < nestedArr[i].length; j++) { + var innerArrVal = nestedArr[i][j]; + if (innerArrVal % 2 === 0) { + console.log(innerArrVal); + } + } + } +} + +printEvens(); From b356f4a19915329fb0093de60d30e16179904011 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 8 May 2019 07:19:35 +0530 Subject: [PATCH 075/161] new exercise in nested arrays --- .../Exercises/nested-arrays.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js index a60f687..77f8d8d 100644 --- a/Nested-Data-structures/Exercises/nested-arrays.js +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -14,3 +14,19 @@ function printEvens() { } printEvens(); + +//Given the following array, write a function called sumTotal returns the sum of all numbers in the array + +var nestedArr = [[1, 2], [3, 4], [5, 6]]; + +function sumTotal() { + var innerArrVal = 0; + for (let i = 0; i < nestedArr.length; i++) { + for (let j = 0; j < nestedArr[i].length; j++) { + innerArrVal += nestedArr[i][j]; + } + } + console.log("Sum " + innerArrVal); +} + +sumTotal(); From 4c0fee83643cc709ee379fbbaa8fa1ef9f525533 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 10 May 2019 22:20:08 +0530 Subject: [PATCH 076/161] Vowel counting exercise --- .../Exercises/.vscode/launch.json | 15 ++++++++++++++ .../Exercises/nested-arrays.js | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Nested-Data-structures/Exercises/.vscode/launch.json diff --git a/Nested-Data-structures/Exercises/.vscode/launch.json b/Nested-Data-structures/Exercises/.vscode/launch.json new file mode 100644 index 0000000..48eb1f5 --- /dev/null +++ b/Nested-Data-structures/Exercises/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js index 77f8d8d..ac5c05e 100644 --- a/Nested-Data-structures/Exercises/nested-arrays.js +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -30,3 +30,23 @@ function sumTotal() { } sumTotal(); + +// Given the following array, write a function called countVowels, which returns the count of all of the vowels in each string regardless of case. To see if a value is an array, you can not use typeof since that will return 'object', so one way to do this is by using the Array.isArray function. + +var nestedArr = [ + "Elie", + ["Matt", ["Tim"]], + ["Colt", ["Whisky", ["Janey"], "Tom"]], + "Lorien" +]; + +function countVowels(arr) { + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + var innerVarVals = arr[i][j]; + console.log(innerVarVals); + } + } +} + +countVowels(nestedArr); From 52b27ef27f92425b1182ee89eb78390873de3def Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 11 May 2019 18:19:36 +0530 Subject: [PATCH 077/161] Tried to understand the nested array , kinda got a grasp of it. Come back to it. --- .../Exercises/nested-arrays.js | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js index ac5c05e..42ac578 100644 --- a/Nested-Data-structures/Exercises/nested-arrays.js +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -33,6 +33,13 @@ sumTotal(); // Given the following array, write a function called countVowels, which returns the count of all of the vowels in each string regardless of case. To see if a value is an array, you can not use typeof since that will return 'object', so one way to do this is by using the Array.isArray function. +// console.log(nestedArr[2][1][1][0]); +//Another way of looping through the array values +// for (const names of nestedArr) { +// console.log(names); +// } + +var vowels = ["a", "e", "i", "o", "u"]; var nestedArr = [ "Elie", ["Matt", ["Tim"]], @@ -41,12 +48,51 @@ var nestedArr = [ ]; function countVowels(arr) { + var count = 0; for (let i = 0; i < arr.length; i++) { - for (let j = 0; j < arr[i].length; j++) { - var innerVarVals = arr[i][j]; - console.log(innerVarVals); + if (Array.isArray(arr[i])) { + console.log(i, arr[i]); + for (let j = 0; j < arr[i].length; j++) { + if (Array.isArray(arr[i][j])) { + console.log(i, j, arr[i][j]); + for (let n = 0; n < arr[i][j].length; n++) { + if (Array.isArray(arr[i][j][n])) { + console.log(i, j, n, arr[i][j][n]); + for (let k = 0; k < arr[i][j][n].lenght; k++) { + count = countChars(arr[i][j][n][k], count); + console.log("dafa", count); + } + } else { + count = countChars(arr[i][j][n], count); + console.log(i, j, n, arr[i][j][n]); + } + } + } else { + count = countChars(arr[i][j], count); + console.log(i, j, arr[i][j]); + } + } + } else { + count = countChars(arr[i], count); + console.log(i, arr[i]); } } + return count; +} + +function checkVowels(char, count) { + if (vowels.includes(char.toLoweCase())) { + console.log(++count); + return ++count; + } + return count; +} + +function countChars(str, count) { + for (let i = 0; i < str.lenght; i++) { + count = checkVowels(str[i], count); + } + return count; } countVowels(nestedArr); From 85e3ea6b613560b9cf8153c2f587e4b4b47c5fbe Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 11 May 2019 19:22:37 +0530 Subject: [PATCH 078/161] Understanding nested loop and arrays --- .../Exercises/nested-arrays.js | 94 ++++++++++--------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js index 42ac578..d4a94f8 100644 --- a/Nested-Data-structures/Exercises/nested-arrays.js +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -47,52 +47,54 @@ var nestedArr = [ "Lorien" ]; -function countVowels(arr) { - var count = 0; - for (let i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - console.log(i, arr[i]); - for (let j = 0; j < arr[i].length; j++) { - if (Array.isArray(arr[i][j])) { - console.log(i, j, arr[i][j]); - for (let n = 0; n < arr[i][j].length; n++) { - if (Array.isArray(arr[i][j][n])) { - console.log(i, j, n, arr[i][j][n]); - for (let k = 0; k < arr[i][j][n].lenght; k++) { - count = countChars(arr[i][j][n][k], count); - console.log("dafa", count); - } - } else { - count = countChars(arr[i][j][n], count); - console.log(i, j, n, arr[i][j][n]); - } - } - } else { - count = countChars(arr[i][j], count); - console.log(i, j, arr[i][j]); - } - } - } else { - count = countChars(arr[i], count); - console.log(i, arr[i]); - } - } - return count; -} +// function countVowels(arr) { +// var count = 0; +// for (let i = 0; i < arr.length; i++) { +// if (Array.isArray(arr[i])) { +// console.log(i, arr[i]); +// for (let j = 0; j < arr[i].length; j++) { +// if (Array.isArray(arr[i][j])) { +// console.log(i, j, arr[i][j]); +// for (let n = 0; n < arr[i][j].length; n++) { +// if (Array.isArray(arr[i][j][n])) { +// console.log(i, j, n, arr[i][j][n]); +// for (let k = 0; k < arr[i][j][n].lenght; k++) { +// count = countChars(arr[i][j][n][k], count); +// console.log("dafa", count); +// } +// } else { +// count = countChars(arr[i][j][n], count); +// console.log(i, j, n, arr[i][j][n]); +// } +// } +// } else { +// count = countChars(arr[i][j], count); +// console.log(i, j, arr[i][j]); +// } +// } +// } else { +// count = countChars(arr[i], count); +// console.log(i, arr[i]); +// } +// } +// return count; +// } -function checkVowels(char, count) { - if (vowels.includes(char.toLoweCase())) { - console.log(++count); - return ++count; - } - return count; -} +// function checkVowels(char, count) { +// if (vowels.includes(char.toLoweCase())) { +// console.log(++count); +// return ++count; +// } +// return count; +// } -function countChars(str, count) { - for (let i = 0; i < str.lenght; i++) { - count = checkVowels(str[i], count); - } - return count; -} +// function countChars(str, count) { +// for (let i = 0; i < str.lenght; i++) { +// count = checkVowels(str[i], count); +// } +// return count; +// } + +// countVowels(nestedArr); -countVowels(nestedArr); +displayArrs(nestedArr); From b224b9ccfaf1a5cde510cf735040b6461e04971f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 12 May 2019 09:26:35 +0530 Subject: [PATCH 079/161] Nested arays exercise done, it was fun --- .../Exercises/nested-arrays.js | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Nested-Data-structures/Exercises/nested-arrays.js b/Nested-Data-structures/Exercises/nested-arrays.js index d4a94f8..10f8db2 100644 --- a/Nested-Data-structures/Exercises/nested-arrays.js +++ b/Nested-Data-structures/Exercises/nested-arrays.js @@ -97,4 +97,44 @@ var nestedArr = [ // countVowels(nestedArr); -displayArrs(nestedArr); +// displayArrs(nestedArr); + +//Write a function called rotate which takes an array and a number, and moves each element however many spaces the number is to the right. For the value at the end of the array, rotate should move it back to the beginning. + +function rotate(arr, num) { + var amt = num % arr.length; + console.log(amt); + for (let a = 0; a < amt; a++) { + console.log(a, arr); + console.log(arr.unshift(arr.pop())); + } + console.log(arr); +} + +rotate([1, 2, 3], 2); +//couldnt solve this, had a vague idea. why divide num by arr length? +//come back to this exercise. + +// Write a function called makeXOGrid which takes in two parameters, rows and columns, and returns an array of arrays with the number of values in each subarray equal to the columns parameter and the number of subarrays equal to the rows parameter. The values in the sub-arrays should switch between "X" and "O". + +function makeXOGrid(rows, columns) { + let parentArr = []; + let starWithX = true; + for (let a = 0; a < rows; a++) { + var rowArr = []; + for (let b = 0; b < columns; b++) { + if (starWithX) { + rowArr.push("X"); + } else { + rowArr.push("O"); + } + //switiching between true and false + // this is something to remember + starWithX = !starWithX; + } + parentArr.push(rowArr); + } + console.table(parentArr); +} + +makeXOGrid(10, 4); From 453f6091d3cdc01410524bef27654be4f614b433 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 16 May 2019 07:56:20 +0530 Subject: [PATCH 080/161] Higher order and calback function notes --- Higher-Order-Functions/higher-order-notes.md | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Higher-Order-Functions/higher-order-notes.md diff --git a/Higher-Order-Functions/higher-order-notes.md b/Higher-Order-Functions/higher-order-notes.md new file mode 100644 index 0000000..21b2a14 --- /dev/null +++ b/Higher-Order-Functions/higher-order-notes.md @@ -0,0 +1,28 @@ +# Higher order functions: + +Function that accept another function as parameters is called a higher order function. + +
    +function sendMessage(message, fn){
    +	return fn(message);
    +}
    +
    + +sendMessage("Hello World", console.log); + +sendMessage("Hello world", function(message){ +console.log(message + "from a callback function"); + +} +); + +var myanofunction = function(message){ +console.log(message + "from a callback function" + +}\_ + +sendMessage("Hello wordld", myanofunction); + +## Callback Function + +Function that is passed as a parameter into higher order function is called Callback function From 3d7bbad1b0f91064ad93adc39eed4bece2ed6725 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 17 May 2019 15:25:54 +0530 Subject: [PATCH 081/161] Higher Order and callbacks exercises done --- .../Exercises and Examples/README.md | 3 + .../Exercises and Examples/index.html | 16 ++++ .../Exercises and Examples/script.js | 86 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 Higher-Order-Functions/Exercises and Examples/README.md create mode 100644 Higher-Order-Functions/Exercises and Examples/index.html create mode 100644 Higher-Order-Functions/Exercises and Examples/script.js diff --git a/Higher-Order-Functions/Exercises and Examples/README.md b/Higher-Order-Functions/Exercises and Examples/README.md new file mode 100644 index 0000000..f44c525 --- /dev/null +++ b/Higher-Order-Functions/Exercises and Examples/README.md @@ -0,0 +1,3 @@ +# These Exercises are from + +https://www.rithmschool.com/courses/javascript/javascript-functions-functions-basic diff --git a/Higher-Order-Functions/Exercises and Examples/index.html b/Higher-Order-Functions/Exercises and Examples/index.html new file mode 100644 index 0000000..90a4a3c --- /dev/null +++ b/Higher-Order-Functions/Exercises and Examples/index.html @@ -0,0 +1,16 @@ + + + + + + + + Codestin Search App + + + + + diff --git a/Higher-Order-Functions/Exercises and Examples/script.js b/Higher-Order-Functions/Exercises and Examples/script.js new file mode 100644 index 0000000..75e2692 --- /dev/null +++ b/Higher-Order-Functions/Exercises and Examples/script.js @@ -0,0 +1,86 @@ +//Higher order examples + +function add(a, b) { + //this function is called a callback, since I am passing this into maths function as parameter + return a + b; +} + +function subtract(a, b) { + return a - b; +} + +function maths(a, b, fn) { + return fn(a, b); +} + +console.dir(maths(1, 2, add)); +console.dir(maths(1, 2, subtract)); + +// Let's try to write a function called each which accepts two parameters: an array and a callback function. The each function should loop over the array passed to it and run the callback function on each element in it. + +// var arrayVals = function(arr) { +// let arrVakues = ""; +// for (arrvale of arr) { +// arrVakues += arrvale; +// } +// return arrVakues * 2; +// }; + +// function each(arr, fn) { +// return fn(arr); +// } + +// console.log(each([1, 2, 3, 4], arrayVals)); +// console.log(each([1, 2, 3, 4], arrayVals)); + +function anotherEach(arrs, fn) { + for (let a = 0; a < arrs.length; a++) { + fn(arrs[a]); + } +} + +anotherEach([1, 2.3], function(val) { + console.log(val); +}); + +anotherEach([1, 5, 6], function(val) { + console.log(val * 5); +}); + +// Write a function called map which accepts two parameters: an array and a callback. The map function should return a new array with the result of each value being passed to the callback function. Here's an example: + +function mapArr(arr, fn) { + var newArr = []; + for (let a = 0; a < arr.length; a++) { + newArr.push(fn(arr[a])); + } + return newArr; +} + +mapArr([1, 2, 3, 4], function(val) { + console.log(val * 5); +}); + +// Write a function called reject which accepts two parameters an array and a callback. The function should return a new array with all of the values that do not return true to the callback. Here are two examples: + +function reject(arr, fn) { + let newArr = []; + for (let a = 0; a < arr.length; a++) { + if (fn(arr[a]) === false) { + newArr.push(arr[a]); + } + } + console.log(newArr); +} + +reject([1, 2, 3, 4], function(val) { + // console.log(val); + // console.log(val % 2 === 0); + return val % 2 === 0; +}); + +reject([1, 2, 3, 4], function(val) { + // console.log(val); + // console.log(val % 2 === 0); + return val > 2; +}); From b451ca686a12fdd68c13c02f35d7744e6e3747b2 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 19 May 2019 08:31:14 +0530 Subject: [PATCH 082/161] Update README.md --- Arrays/Notes/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Arrays/Notes/README.md b/Arrays/Notes/README.md index 0cb6455..a2e1c14 100644 --- a/Arrays/Notes/README.md +++ b/Arrays/Notes/README.md @@ -11,6 +11,15 @@ Array Methods: ### Add values to the start of the Array unshift("new array value"); + +NB: Adding function always returns the length of the array after new item has been added. + +### Removing values from the end of an array +arr.pop(); +### Removing values from the start of an array +arr.shift(); + +NB: Deleting function in arrays always returns the removed value ### splice() From 1a58c51a380a5db2cd39fd3397c818acccc5fc3f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 22 May 2019 22:13:58 +0530 Subject: [PATCH 083/161] Timers Notes --- .../Exercises and Examples/timers.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Higher-Order-Functions/Exercises and Examples/timers.md diff --git a/Higher-Order-Functions/Exercises and Examples/timers.md b/Higher-Order-Functions/Exercises and Examples/timers.md new file mode 100644 index 0000000..d156709 --- /dev/null +++ b/Higher-Order-Functions/Exercises and Examples/timers.md @@ -0,0 +1,22 @@ +# Timers-setInterval and setTimeout + +Both accepts a callback function a and time in milliseconds as paramaeters. + +setInterval executes the callback for infinite number of times-intil it is clearred. + +setTimeout executes the callback after a set time, once. + +When both executes return a unique id called timerId on execution, which can be used to stop the timer or clear the timer. + +
    +var timerId = setInterval(function(){
    +console.log("Hi")
    +},1000);
    +
    +setTimeout(function(){
    +	clearTimeout(timerId)
    +},3000);
    +
    + +In the above code after every one sec the hi is consoled, until setimeout executes after 3 secs which will clear it out using the timerId +that was produced while the setInterval got executed. From a6bc3e9d57791a8f074ebf97a7705f6e38ea8b1b Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 26 May 2019 10:46:14 +0530 Subject: [PATCH 084/161] Update script-iife.js --- Functions/Notes/script-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Functions/Notes/script-iife.js b/Functions/Notes/script-iife.js index 231b811..4f4c1ef 100644 --- a/Functions/Notes/script-iife.js +++ b/Functions/Notes/script-iife.js @@ -1,5 +1,5 @@ //The below code is called a IIFE -//it gets immedialely after its written +//it gets executed immedialely after its written //dont have to invoke it separately var result = (function() { let person = "Sam"; From 0eba4279287f3b0620d9cda9a87c36456ca3336b Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 26 May 2019 10:49:44 +0530 Subject: [PATCH 085/161] Update index.html --- Functions/Notes/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Functions/Notes/index.html b/Functions/Notes/index.html index beccf8c..5c1ece1 100644 --- a/Functions/Notes/index.html +++ b/Functions/Notes/index.html @@ -68,7 +68,7 @@

    Arrow Functions

    Closure In Functions

    Closure is a function that is enclosed within a function, that have access - to the outer functions variables and scope. This closes the outer + to the outer functions variables and scope even after the outer function has returned. This closes the outer functions variable within it and uses it when inner function is executed. Its like the inner function captures the variables from outer function and keeps it for later use. @@ -97,7 +97,7 @@

    Closure In Functions

    Explanation: When we pass the outer function to variable multiply we are calling the outer function and passing a parameter value along - with assigning the variable value to inner(). When we call the variable + with assigning the variable to inner(). When we call the variable multiply we call the inner function that has the outer functions value of 5 and uses to get the result. So even when the outer function got executed, the inner function still had the value of x and was able use From 551e81d4cbd7a8c356985a8a0586848d7c27b7f3 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 27 May 2019 06:44:56 +0530 Subject: [PATCH 086/161] Create notes.md --- Functions/Notes/notes.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Functions/Notes/notes.md diff --git a/Functions/Notes/notes.md b/Functions/Notes/notes.md new file mode 100644 index 0000000..6b6f082 --- /dev/null +++ b/Functions/Notes/notes.md @@ -0,0 +1,22 @@ +Every time a function runs we get access to specila kind of keyword called arguments + +we can use it to know the length of arguments passed in the function + +function test(){ +arguments.length; +} + +test(5,6) + +2 + +function testing(x,y){ +if(arguments.length<2) +{console.log("Please pass one more argument")} +else{return x*y} +} + +testing(2,2); + +here we are checking if the arguments passed is correct or not. + From 90e1c0b2ded317a984871eea343bb2db6f0a2d9b Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 27 May 2019 06:57:00 +0530 Subject: [PATCH 087/161] Update notes.md --- Functions/Notes/notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Functions/Notes/notes.md b/Functions/Notes/notes.md index 6b6f082..1e236aa 100644 --- a/Functions/Notes/notes.md +++ b/Functions/Notes/notes.md @@ -1,4 +1,4 @@ -Every time a function runs we get access to specila kind of keyword called arguments +Every time a function runs we get access to special kind of keyword called arguments. we can use it to know the length of arguments passed in the function From 4a5cfff8a32b71b6a09df1ac5f5525cbee75dffd Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 27 May 2019 06:59:43 +0530 Subject: [PATCH 088/161] Update notes.md --- Functions/Notes/notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Functions/Notes/notes.md b/Functions/Notes/notes.md index 1e236aa..fc6ff08 100644 --- a/Functions/Notes/notes.md +++ b/Functions/Notes/notes.md @@ -1,6 +1,8 @@ Every time a function runs we get access to special kind of keyword called arguments. +It is not an array but array-like-objects. we can use it to know the length of arguments passed in the function +we can also access the passed arguments through [] notation. function test(){ arguments.length; From 04614515753b714fad174eabd484638a0e801508 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 27 May 2019 07:21:03 +0530 Subject: [PATCH 089/161] Change in comment to make it more understanable --- Higher-Order-Functions/Exercises and Examples/script.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Higher-Order-Functions/Exercises and Examples/script.js b/Higher-Order-Functions/Exercises and Examples/script.js index 75e2692..738941d 100644 --- a/Higher-Order-Functions/Exercises and Examples/script.js +++ b/Higher-Order-Functions/Exercises and Examples/script.js @@ -8,7 +8,8 @@ function add(a, b) { function subtract(a, b) { return a - b; } - +//this maths function is called higher order function since it accepts +//another function as argument. function maths(a, b, fn) { return fn(a, b); } From ff6443d8c5193fbb26148a505075188904cf4009 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 28 May 2019 07:09:38 +0530 Subject: [PATCH 090/161] CountDown exercise done --- .../Exercises and Examples/exercises.js | 20 +++++++++++++++++++ .../Exercises and Examples/index.html | 1 + 2 files changed, 21 insertions(+) create mode 100644 Higher-Order-Functions/Exercises and Examples/exercises.js diff --git a/Higher-Order-Functions/Exercises and Examples/exercises.js b/Higher-Order-Functions/Exercises and Examples/exercises.js new file mode 100644 index 0000000..a8fdb2b --- /dev/null +++ b/Higher-Order-Functions/Exercises and Examples/exercises.js @@ -0,0 +1,20 @@ +// Higher Order Functions, Timers, and Closures Exercises + +// countdown + +// Write a function called countdown that accepts a number as a parameter and every 1000 milliseconds decrements the value and console.logs it. Once the value is 0 it should log "DONE!" and stop. + +function countdown(num) { + var intervalId = setInterval(decrementer, 1000); + + function decrementer() { + num--; + console.log(num); + if (num === 0) { + clearInterval(intervalId); + console.log("Done"); + } + } +} + +countdown(5); diff --git a/Higher-Order-Functions/Exercises and Examples/index.html b/Higher-Order-Functions/Exercises and Examples/index.html index 90a4a3c..f7e249a 100644 --- a/Higher-Order-Functions/Exercises and Examples/index.html +++ b/Higher-Order-Functions/Exercises and Examples/index.html @@ -12,5 +12,6 @@ + From 0afeabc4f3aee53b8fbe8f003223d7621786d87b Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 28 May 2019 07:31:55 +0530 Subject: [PATCH 091/161] randomGame exercise also done --- .../Exercises and Examples/exercises.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Higher-Order-Functions/Exercises and Examples/exercises.js b/Higher-Order-Functions/Exercises and Examples/exercises.js index a8fdb2b..73e3693 100644 --- a/Higher-Order-Functions/Exercises and Examples/exercises.js +++ b/Higher-Order-Functions/Exercises and Examples/exercises.js @@ -17,4 +17,23 @@ function countdown(num) { } } -countdown(5); +// countdown(5); + +//randomGame +// Write a function called randomGame that selects a random number between 0 and 1 every 1000 milliseconds and each time that a random number is picked, add 1 to a counter. If the number is greater than .75, stop the timer and return the number of tries it took before we found a number greater than .75. + +function randomGame() { + var timerId = setInterval(randomNumberPicker, 1000); + var counter = 0; + function randomNumberPicker() { + var randomNumer = Math.random(); + counter++; + console.log(randomNumer); + if (randomNumer > 0.75) { + clearInterval(timerId); + } + console.log(counter); + } +} + +randomGame(); From 05921f50f808c65f130ee17c2d45257124b3ba98 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 30 May 2019 07:22:08 +0530 Subject: [PATCH 092/161] Calback Exercises --- .../Exercises and Examples/exercises.js | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/Higher-Order-Functions/Exercises and Examples/exercises.js b/Higher-Order-Functions/Exercises and Examples/exercises.js index 73e3693..4e528b7 100644 --- a/Higher-Order-Functions/Exercises and Examples/exercises.js +++ b/Higher-Order-Functions/Exercises and Examples/exercises.js @@ -36,4 +36,92 @@ function randomGame() { } } -randomGame(); +// randomGame(); + +//Write a function called isEven which takes in a number and returns true if the number is even and returns false if it is not + +function isEven(num) { + if (num % 2 === 0) { + console.log("true"); + } else { + console.log("false"); + } +} + +// isEven(5); + +//Odd Number + +function isOdd(num) { + if (num % 2 !== 0) { + console.log("true"); + } else { + console.log("false"); + } +} + +//Write a function called isPrime which takes in a number and returns true if the number is a prime number (is greater than 1 and can only be divided in whole by itself and 1), otherwise returns false + +// function isPrime(num) { +// if (num > 1 && num % num === 0 && num % 1 === 0) { +// console.log("true"); +// } else { +// console.log("false"); +// } +// } + +// isPrime(8); + +//Write a function called numberFact which takes in a number and a callback and returns the result of the callback with the number passed to it + +function numberFact(num, fn) { + return fn(num); +} + +// numberFact(6, isEven); +// numberFact(6, isOdd); + +//find +//Write a function called find. It should take in an array and a callback and return the first value found in the array that matches the condition. + +function find(arr, fn) { + for (let a = 0; a < arr.length; a++) { + if (fn(arr[a])) { + console.log(arr[a]); + return arr[a]; + } + } +} + +find([8, 11, 4, 27], function(val) { + return val >= 10; +}); + +find([8, 11, 4, 27], function(val) { + return val < 10; +}); + +//findIndex + +//Write a function called findIndex. It should take in an array and a callback and return the index of first value found in the array that matches the condition. + +function findIndex(arr, fn) { + for (let a = 0; a < arr.length; a++) { + if (fn(arr[a])) { + console.log(arr.indexOf(arr[a])); + return arr.indexOf(arr[a]); + } + } +} + +findIndex([8, 11, 4, 27], function(val) { + return val >= 10; +}); + +findIndex([8, 11, 4, 27], function(val) { + return val < 7; +}); + +//specialMultiply + +// Write a function called specialMultiply which accepts two parameters. If the function is passed both parameters, it should return the product of the two. If the function is only passed one parameter - it should return a function which can later be passed another parameter to return the product. You will have to use closure and arguments to solve this. From 5fb7df83fb5d323c3c07b723c13c8b0d3f060cce Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 31 May 2019 07:45:12 +0530 Subject: [PATCH 093/161] completed exercise in higher order functions --- .../Exercises and Examples/exercises.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Higher-Order-Functions/Exercises and Examples/exercises.js b/Higher-Order-Functions/Exercises and Examples/exercises.js index 4e528b7..4c86fc1 100644 --- a/Higher-Order-Functions/Exercises and Examples/exercises.js +++ b/Higher-Order-Functions/Exercises and Examples/exercises.js @@ -125,3 +125,14 @@ findIndex([8, 11, 4, 27], function(val) { //specialMultiply // Write a function called specialMultiply which accepts two parameters. If the function is passed both parameters, it should return the product of the two. If the function is only passed one parameter - it should return a function which can later be passed another parameter to return the product. You will have to use closure and arguments to solve this. + +function specialMultiply(x, y) { + if (arguments.length === 1) { + return function(y) { + return x * y; + }; + } + return x * y; +} + +console.log(specialMultiply(5, 4)); From a51adc40d0a42f7cb00fbb9da8b9d37203cfa4fd Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 31 May 2019 07:49:08 +0530 Subject: [PATCH 094/161] Tested the function for test cases its working fine --- Higher-Order-Functions/Exercises and Examples/exercises.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Higher-Order-Functions/Exercises and Examples/exercises.js b/Higher-Order-Functions/Exercises and Examples/exercises.js index 4c86fc1..79a2ac2 100644 --- a/Higher-Order-Functions/Exercises and Examples/exercises.js +++ b/Higher-Order-Functions/Exercises and Examples/exercises.js @@ -136,3 +136,5 @@ function specialMultiply(x, y) { } console.log(specialMultiply(5, 4)); +console.log(specialMultiply(5)); +console.log(specialMultiply(5)(3)); From 370a3de06e10b4e8f6875cc485f5dd8c1b47fdc6 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 2 Jun 2019 14:49:03 +0530 Subject: [PATCH 095/161] DOM Manipulation and Traversal exercises --- DOM/README.md | 2 ++ DOM/index.html | 24 ++++++++++++++++++++++++ DOM/script.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 DOM/README.md create mode 100644 DOM/index.html create mode 100644 DOM/script.js diff --git a/DOM/README.md b/DOM/README.md new file mode 100644 index 0000000..93e0dc3 --- /dev/null +++ b/DOM/README.md @@ -0,0 +1,2 @@ +# Eloquent-Javascript-Exercises +This repository is created to track all my solutions from the book Eloquent JavaScript diff --git a/DOM/index.html b/DOM/index.html new file mode 100644 index 0000000..527a3fa --- /dev/null +++ b/DOM/index.html @@ -0,0 +1,24 @@ + + + + + Codestin Search App + + +

    +
    +
      +
    • one
    • +
    • two
    • +
    • three
    • +
    +
      +
    1. one
    2. +
    3. two
    4. +
    5. three
    6. +
    +
    + + + + diff --git a/DOM/script.js b/DOM/script.js new file mode 100644 index 0000000..2112ef1 --- /dev/null +++ b/DOM/script.js @@ -0,0 +1,51 @@ +//Select the section with an id of container without using querySelector. + +document.getElementById("container"); + +//Select the section with an id of container using querySelector. + +document.querySelector("#container"); + +//Select all of the list items with a class of "second". + +document.querySelector(".second"); + +//Select a list item with a class of third, but only the list item inside of the ol tag. + +var ol = document.querySelector("ol .third"); + +//Give the section with an id of container the text "Hello!". + +// document.getElementById("container").innerText = "Hello"; + +//Add the class main to the div with a class of footer. + +var footer = document.querySelector(".footer"); +footer.classList.add("main"); +footer.classList.remove("main"); + +//Create a new li element. + +var newLi = document.createElement("LI"); +var ulList = document.querySelector("ul"); +newLi.innerText = "Four"; +ulList.appendChild(newLi); + +//Loop over all of the lis inside the ol tag and give them a background color of "green". +//Even list will be teal-Thats from my side + +var oL = document.querySelectorAll("ol li"); +//Note:querySelectorAll returns a NodeList we can use length on that. +console.log(oL); + +for (let a = 0; a < oL.length; a++) { + if (a % 2 === 0) { + oL[a].style.backgroundColor = "brown"; + } else { + oL[a].style.backgroundColor = "teal"; + } +} + +//Remove the div with a class of footer. + +footer.remove(); From f7b19af967cc0f35636ff873c9a5404c15443208 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 2 Jun 2019 18:45:02 +0530 Subject: [PATCH 096/161] Added DOM definition in notes --- DOM/DOM-Notes.md | 4 ++++ DOM/README.md | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 DOM/DOM-Notes.md diff --git a/DOM/DOM-Notes.md b/DOM/DOM-Notes.md new file mode 100644 index 0000000..a603b26 --- /dev/null +++ b/DOM/DOM-Notes.md @@ -0,0 +1,4 @@ +## What is DOM? + +

    Document Object Model is a structured presentation of HTML document in Nodes and Objects, which has properties and Methods related to it, which can be used to alter the structure, style and content of the DOM. This can done using the JS or other programming languages. +

    diff --git a/DOM/README.md b/DOM/README.md index 93e0dc3..f725878 100644 --- a/DOM/README.md +++ b/DOM/README.md @@ -1,2 +1,4 @@ # Eloquent-Javascript-Exercises -This repository is created to track all my solutions from the book Eloquent JavaScript + +This repository is created for DOM manipulation and Traversal Notes and exercises +from https://www.rithmschool.com/courses/intermediate-javascript From 459c791ee09b7c201cc3c4de67d78d99a68d2c66 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 4 Jun 2019 22:16:37 +0530 Subject: [PATCH 097/161] First exercise from Codewars --- Codewar-Exercises/README.md | 1 + Codewar-Exercises/Unique-Elements/README.md | 1 + Codewar-Exercises/Unique-Elements/index.html | 17 +++++++++++ Codewar-Exercises/Unique-Elements/script.js | 32 ++++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 Codewar-Exercises/README.md create mode 100644 Codewar-Exercises/Unique-Elements/README.md create mode 100644 Codewar-Exercises/Unique-Elements/index.html create mode 100644 Codewar-Exercises/Unique-Elements/script.js diff --git a/Codewar-Exercises/README.md b/Codewar-Exercises/README.md new file mode 100644 index 0000000..fff5606 --- /dev/null +++ b/Codewar-Exercises/README.md @@ -0,0 +1 @@ +Exercise from codewars diff --git a/Codewar-Exercises/Unique-Elements/README.md b/Codewar-Exercises/Unique-Elements/README.md new file mode 100644 index 0000000..fff5606 --- /dev/null +++ b/Codewar-Exercises/Unique-Elements/README.md @@ -0,0 +1 @@ +Exercise from codewars diff --git a/Codewar-Exercises/Unique-Elements/index.html b/Codewar-Exercises/Unique-Elements/index.html new file mode 100644 index 0000000..675d8e6 --- /dev/null +++ b/Codewar-Exercises/Unique-Elements/index.html @@ -0,0 +1,17 @@ + + + + + + + + + Codestin Search App + + + + + diff --git a/Codewar-Exercises/Unique-Elements/script.js b/Codewar-Exercises/Unique-Elements/script.js new file mode 100644 index 0000000..93e40f3 --- /dev/null +++ b/Codewar-Exercises/Unique-Elements/script.js @@ -0,0 +1,32 @@ +// var uniqueInOrder = function(iterable) { +// var arr = []; +// // console.log(iterable); +// for (let a = 0; a < iterable.length; a++) { +// console.log(arr.indexOf(iterable[a])); +// if (arr.indexOf(iterable[a]) === -1) { +// arr.push(iterable[a]); +// } +// } +// return arr; +// }; + +// uniqueInOrder("AAAABBBCCDAABBB"); + +var uniqueInOrder = function(iterable) { + var arr = []; + for (let a = 0; a < iterable.length; a++) { + // console.log("iterable[a] ", iterable[a]); + // console.log("iterable[a + 1]", iterable[a + 1]); + // console.log(iterable[a] === iterable[a + 1]); + if (iterable[a] === iterable[a + 1]) continue; + //continue is way of skipping in for..loop + //if true continue for loop + //if false push it to array + //so cool! + arr.push(iterable[a]); + } + return arr; +}; + +uniqueInOrder("AAAABBBCCDAABBB"); +//answer should be ABCDAB From a08b11e8ef57c0623344cf70837ea4d9ca6c5851 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 5 Jun 2019 21:37:20 +0530 Subject: [PATCH 098/161] Learning about Events in JS --- Events and LocalStorage/index.html | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Events and LocalStorage/index.html diff --git a/Events and LocalStorage/index.html b/Events and LocalStorage/index.html new file mode 100644 index 0000000..bd03dde --- /dev/null +++ b/Events and LocalStorage/index.html @@ -0,0 +1,40 @@ + + + + + Codestin Search App + + + + +
    + Click on me! +
    +
    + If you click on EM, the handler on + DIV runs. +
    + +
    + FORM +
    + DIV +

    P

    +
    +
    + + From 68816d9c9e43bdf42a9bbcc46c45f4093a07c1c8 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 6 Jun 2019 20:34:48 +0530 Subject: [PATCH 099/161] Notes related to events.target --- Events and LocalStorage/Events-notes.md | 6 ++++ Events and LocalStorage/index.html | 45 +++++++++---------------- 2 files changed, 21 insertions(+), 30 deletions(-) create mode 100644 Events and LocalStorage/Events-notes.md diff --git a/Events and LocalStorage/Events-notes.md b/Events and LocalStorage/Events-notes.md new file mode 100644 index 0000000..3cdfa17 --- /dev/null +++ b/Events and LocalStorage/Events-notes.md @@ -0,0 +1,6 @@ +When an event is triggered there is special object called event object which we can access for few useful values. + +eg:event.target gets the element on which the event happened. + +one more parameter can be passed into a addEventListener function called useCapture which determines if we use capturing or bubbling +(note:research more on this topic) diff --git a/Events and LocalStorage/index.html b/Events and LocalStorage/index.html index bd03dde..f9cb399 100644 --- a/Events and LocalStorage/index.html +++ b/Events and LocalStorage/index.html @@ -3,38 +3,23 @@ Codestin Search App - + + +
      + Ingredients +
    • Sugar
    • +
    • Milk
    • +
    • Eggs
    • +
    • Flour
    • +
    • Baking Soda
    • +
    - - -
    - Click on me! -
    -
    - If you click on EM, the handler on - DIV runs. -
    - -
    - FORM -
    - DIV -

    P

    -
    -
    From 9786b8c3db35fb4a1940f9508e03e24c3ba786f8 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 8 Jun 2019 11:56:32 +0530 Subject: [PATCH 100/161] Exercises on Event and DOM manipulation --- Events and LocalStorage/Exercises/index.html | 61 +++++++++++ Events and LocalStorage/Exercises/script.js | 101 +++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 Events and LocalStorage/Exercises/index.html create mode 100644 Events and LocalStorage/Exercises/script.js diff --git a/Events and LocalStorage/Exercises/index.html b/Events and LocalStorage/Exercises/index.html new file mode 100644 index 0000000..78fabe0 --- /dev/null +++ b/Events and LocalStorage/Exercises/index.html @@ -0,0 +1,61 @@ + + + + + Codestin Search App + + + +

    Change Me!

    + SELECTED COLOR None! +
    +
    +
    +
    +
    +
    +

    Race!

    + +
    +
    +
    +
    + + + diff --git a/Events and LocalStorage/Exercises/script.js b/Events and LocalStorage/Exercises/script.js new file mode 100644 index 0000000..d545bfd --- /dev/null +++ b/Events and LocalStorage/Exercises/script.js @@ -0,0 +1,101 @@ +window.addEventListener("DOMContentLoaded", function() { + let heading = document.getElementById("change_heading"); + heading.innerText = "Hello World"; + + let selectedColor = document.querySelector(".selected"); + let colorSection = document.querySelector("section"); + //colored Boxes + // let colorBoxBrown = document.querySelector(".brown"); + // let colorBoxGreen = document.querySelector(".green"); + // let colorBoxBlue = document.querySelector(".blue"); + // let colorBoxYellow = document.querySelector(".yellow"); + + // colorBoxBrown.addEventListener("mouseover", function() { + // selectedColor.innerText = colorBoxBrown.className; + // }); + + // colorBoxGreen.addEventListener("mouseover", function() { + // selectedColor.innerText = colorBoxGreen.className; + // }); + + // colorBoxBlue.addEventListener("mouseover", function() { + // selectedColor.innerText = colorBoxBlue.className; + // }); + + // colorBoxYellow.addEventListener("mouseover", function() { + // selectedColor.innerText = colorBoxYellow.className; + // }); + + //the above commented code can be written more efficiently like this + colorSection.addEventListener("mouseover", function(event) { + selectedColor.innerText = event.target.className; + }); + + //adding new div with class purple + let purpleDiv = document.createElement("DIV"); + purpleDiv.setAttribute("class", "purple"); + colorSection.appendChild(purpleDiv); + + //Race car + let carOne = document.querySelector(".car1"); + let carTwo = document.querySelector(".car2"); + carOne.style.marginLeft = 0; + carTwo.style.marginLeft = 0; + + let raceStarterBtn = document.querySelector("button"); + // function reset(carOne, carTwo) { + // clearTimeout(carOne.timer); + // clearTimeout(carTwo.timer); + // carOne.style.marginLeft = 0; + // carTwo.style.marginLeft = 0; + // } + + // raceStarterBtn.addEventListener("click", function() { + // carOne.timer = setInterval(function() { + // carOne.style.marginLeft = + // parseInt(carOne.style.marginLeft) + Math.random() * 60 + "px"; + // if (parseInt(carOne.style.marginLeft) > window.innerWidth) { + // alert("Yeah, I win"); + // reset(carOne, carTwo); + // } + // }, 60); + + // carTwo.timer = setInterval(function() { + // carTwo.style.marginLeft = + // parseInt(carTwo.style.marginLeft) + Math.random() * 60 + "px"; + // if (parseInt(carTwo.style.marginLeft) > window.innerWidth) { + // alert("Yeah, I win"); + // reset(carOne, carTwo); + // } + // }, 60); + // }); + + carOne.style.marginLeft = 0; + carTwo.style.marginLeft = 0; + + function reset(carOne, carTwo) { + clearTimeout(carOne.timer); + clearTimeout(carTwo.timer); + carOne.style.marginLeft = 0; + carTwo.style.marginLeft = 0; + } + + raceStarterBtn.addEventListener("click", function() { + carOne.timer = setInterval(function() { + carOne.style.marginLeft = + parseInt(carOne.style.marginLeft) + Math.random() * 60 + "px"; + if (parseInt(carOne.style.marginLeft) > window.innerWidth) { + alert("Yes, I win-car one"); + reset(carOne, carTwo); + } + }, 60); + carTwo.timer = setInterval(function() { + carTwo.style.marginLeft = + parseInt(carTwo.style.marginLeft) + Math.random() * 60 + "px"; + if (parseInt(carTwo.style.marginLeft) > window.innerWidth) { + alert("Yes, I win-car two"); + reset(carOne, carTwo); + } + }, 60); + }); +}); From 6febe704779eb39d3533ec0f98a6552c1edf25ed Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 11 Jun 2019 23:51:00 +0530 Subject: [PATCH 101/161] Update DOM-Notes.md --- DOM/DOM-Notes.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/DOM/DOM-Notes.md b/DOM/DOM-Notes.md index a603b26..cfa3a93 100644 --- a/DOM/DOM-Notes.md +++ b/DOM/DOM-Notes.md @@ -2,3 +2,17 @@

    Document Object Model is a structured presentation of HTML document in Nodes and Objects, which has properties and Methods related to it, which can be used to alter the structure, style and content of the DOM. This can done using the JS or other programming languages.

    + + +There are 12 node types. In practice we usually work with 4 of them: + +document – the “entry point” into DOM. +element nodes – HTML-tags, the tree building blocks. +text nodes – contain text. +comments – sometimes we can put the information there, it won’t be shown, but JS can read it from the DOM. + +An HTML/XML document is represented inside the browser as the DOM tree. + +Tags become element nodes and form the structure. +Text becomes text nodes. +…etc, everything in HTML has its place in DOM, even comments. From 21f09fa333e52918263dbe8307af785693118447 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 12 Jun 2019 23:08:44 +0530 Subject: [PATCH 102/161] Added notes on differnt methods of accessing DOM --- DOM/DOM-Notes.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DOM/DOM-Notes.md b/DOM/DOM-Notes.md index a603b26..68a7374 100644 --- a/DOM/DOM-Notes.md +++ b/DOM/DOM-Notes.md @@ -2,3 +2,8 @@

    Document Object Model is a structured presentation of HTML document in Nodes and Objects, which has properties and Methods related to it, which can be used to alter the structure, style and content of the DOM. This can done using the JS or other programming languages.

    + +'querySelector': selects single element it can be using class or give the tag name; +'querySelectorAll':Selects more than one element with the same name and returns a array like object. + +'classList':Adds class to already existing class, we can ise add() and remove(); From 54c4ee0432bea3614b8e50580788ad5fd87d24e8 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 13 Jun 2019 21:29:39 +0530 Subject: [PATCH 103/161] Learning ForEach --- Iterators-forEach, Map, and Filter/index.html | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Iterators-forEach, Map, and Filter/index.html diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html new file mode 100644 index 0000000..da9fbd9 --- /dev/null +++ b/Iterators-forEach, Map, and Filter/index.html @@ -0,0 +1,23 @@ + + + + + + + Codestin Search App + + + + + From 106e17771b0ccff6294dd4f51fae6ace95ba1f9c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 15 Jun 2019 07:51:35 +0530 Subject: [PATCH 104/161] For each exercise done --- Iterators-forEach, Map, and Filter/index.html | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index da9fbd9..db6c27f 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -9,13 +9,9 @@ From 2694a732ef14ce42373edc5f9fa4c18f52006c07 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 15 Jun 2019 08:08:04 +0530 Subject: [PATCH 105/161] Completed exercise for for Each --- Iterators-forEach, Map, and Filter/index.html | 9 +------- Iterators-forEach, Map, and Filter/script.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 Iterators-forEach, Map, and Filter/script.js diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index db6c27f..e6ff5d2 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -7,13 +7,6 @@ Codestin Search App - + diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js new file mode 100644 index 0000000..35b0900 --- /dev/null +++ b/Iterators-forEach, Map, and Filter/script.js @@ -0,0 +1,22 @@ +function printFirstAndLast(arr) { + arr.forEach(element => { + console.log(element[0] + element[element.length - 1]); + }); +} +printFirstAndLast(["awesome", "example", "of", "forEach"]); + +//Write a function called addKeyAndValue which accepts three parameters, an array (of objects), a key and a value. This function should return the array of objects after each key and value have been added to each object in the array. + +function addKeyAndValue(arr, key, value) { + arr.forEach(element => { + console.log((element[key] = value)); + }); + console.table(arr); + return arr; +} + +addKeyAndValue( + [{ name: "Elie" }, { name: "Tim" }, { name: "Elie" }], + "isInstructor", + true +); From 82aaca03a4da3a60890ca3ab51b3a450e8eca073 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 15 Jun 2019 08:08:10 +0530 Subject: [PATCH 106/161] Added note for iterators. --- Iterators-forEach, Map, and Filter/Iterators-notes.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Iterators-forEach, Map, and Filter/Iterators-notes.md diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md new file mode 100644 index 0000000..a23904b --- /dev/null +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -0,0 +1 @@ +For each iterators From 656afa8ee32bc8f0186efd93febaf65f8e547763 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 15 Jun 2019 08:41:00 +0530 Subject: [PATCH 107/161] Notes on forEach() array method. --- .../Iterators-notes.md | 21 ++++++++++++++++++- Iterators-forEach, Map, and Filter/script.js | 9 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index a23904b..07a59a5 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -1 +1,20 @@ -For each iterators +## For each iterators + +

    +The forEach() method executes a callback function once for each array element. +The callback takes in three arguements-the array currentValue, index(optional) and the array itself(optional); +It always returns 'undefined'. +

    + +
    +
    +let arr = [5, 6, 8];
    +
    +arr.forEach(function(value, index, array) {
    +  console.log(value);
    +  console.log(index);
    +  console.log(array);
    +  console.log(this);
    +});
    +
    +
    diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js index 35b0900..850b893 100644 --- a/Iterators-forEach, Map, and Filter/script.js +++ b/Iterators-forEach, Map, and Filter/script.js @@ -20,3 +20,12 @@ addKeyAndValue( "isInstructor", true ); + +let arr = [5, 6, 8]; + +arr.forEach(function(value, index, array) { + console.log(value); + console.log(index); + console.log(array); + console.log(this); +}); From 6a309108ed4b31d7553bce5e62731121a40717b3 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 15 Jun 2019 10:29:09 +0530 Subject: [PATCH 108/161] Map and filter notes and exercises --- .../Iterators-notes.md | 19 ++++- Iterators-forEach, Map, and Filter/script.js | 84 +++++++++++++++++-- 2 files changed, 95 insertions(+), 8 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index 07a59a5..0b68299 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -1,4 +1,4 @@ -## For each iterators +## For each iterators-forEach()

    The forEach() method executes a callback function once for each array element. @@ -18,3 +18,20 @@ arr.forEach(function(value, index, array) { }); + +## Map - map() + +

    +map returns a new array. Syntax for map is the same as forEach and it takes the same parameters-value,index and array. +Since it is a higher order function we can assign it to an variable and get the returned array. +

    + +## Filter-filter() + +So points to rememember: + +1. forEach processes each value with calback function-return undefined. This processed value have to be saved in some variable. + +2. map processes each value with callback function-returns an array, so the returned array can be assigned in an variable. + +3. filter processes each value only if it satisfies a condition to become true and returns an array. It is sort of a testing function. diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js index 850b893..362715e 100644 --- a/Iterators-forEach, Map, and Filter/script.js +++ b/Iterators-forEach, Map, and Filter/script.js @@ -1,7 +1,32 @@ +//Testing forEach() +let arr = [5, 6, 8]; + +arr.forEach(function(value, index, array) { + console.log(value); + console.log(index); + console.log(array); + console.log(this); +}); + +//testing map() +let arrMap = [5, 6, 8]; + +let testMap = arrMap.map(function(value) { + console.log(value + 2); + return value + 2; +}); + +console.log(testMap); + +//Exercises starts here + function printFirstAndLast(arr) { + let newVals = ""; arr.forEach(element => { console.log(element[0] + element[element.length - 1]); + newVals += element[0] + element[element.length - 1]; }); + console.log(newVals); } printFirstAndLast(["awesome", "example", "of", "forEach"]); @@ -21,11 +46,56 @@ addKeyAndValue( true ); -let arr = [5, 6, 8]; +//map exercises -arr.forEach(function(value, index, array) { - console.log(value); - console.log(index); - console.log(array); - console.log(this); -}); +//Write a function called valTimesIndex which accepts an array of numbers and returns a new array with each value multiplied by the index it is at in the array: + +let arrMaps = [5, 6, 7]; + +function valTimesIndex(arr) { + let mappedValsNum = arr.map(function(val, index) { + return val * index; + }); + let newDiv = document.createElement("div"); + document.body.appendChild(newDiv); + newDiv.innerText = mappedValsNum; + console.log(mappedValsNum); +} + +valTimesIndex(arrMaps); + +//Write a function called extractKey which accepts two parameters, an array of objects, and the name of a key and returns an array with just the values for that key: + +function extractKey(arr, key) { + let mappedVals = arr.map(function(value) { + return value[key]; + }); + // document.body.innerHTML = mappedVals; + console.log(mappedVals); +} + +extractKey( + [ + { name: "Elie", isInstructor: true }, + { name: "Tim", isInstructor: true }, + { name: "Matt", isInstructor: true } + ], + "name" +); + +//Write a function called filterLetters which accepts an array of letters and returns the number of occurrences of a specific letter. This function should be case insensitive + +function filterLetters(arr, letter) { + let counter = 0; + arr.filter(function(value) { + if (value.toLowerCase() === letter.toLowerCase()) { + return counter++; + } + }); + + console.log(counter); +} + +filterLetters(["a", "a", "b", "c", "A"], "a"); +filterLetters(["a", "a", "b", "c", "A"], "z"); +filterLetters(["a", "a", "b", "c", "A"], "B"); From 6ccb1f2ce842248c8e532a766f63bbdf7f7f2227 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 16 Jun 2019 17:16:17 +0530 Subject: [PATCH 109/161] Trying to understand reduce --- Iterators-forEach, Map, and Filter/index.html | 3 +- Iterators-forEach, Map, and Filter/reduce.js | 35 +++++++++++++++++++ Iterators-forEach, Map, and Filter/script.js | 18 ++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 Iterators-forEach, Map, and Filter/reduce.js diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index e6ff5d2..e0f2431 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -7,6 +7,7 @@ Codestin Search App - + + diff --git a/Iterators-forEach, Map, and Filter/reduce.js b/Iterators-forEach, Map, and Filter/reduce.js new file mode 100644 index 0000000..22403ad --- /dev/null +++ b/Iterators-forEach, Map, and Filter/reduce.js @@ -0,0 +1,35 @@ +//Write a function called extractKey which accepts two parameters, an array of objects, and the name of a key and returns an array with just the values for that key + +function extractKey(arrObj, key) { + arrObj.reduce(function(acc, next) { + acc.push(next[key]); + console.log(acc); + return acc; + }, []); +} + +extractKey( + [ + { name: "Elie", isInstructor: true }, + { name: "Tim", isInstructor: true }, + { name: "Matt", isInstructor: true } + ], + "name" +); + +//Write a function called filterLetters which accepts an array of letters and returns the number of occurrences of a specific letter. This function should be case insensitive + +// function filterLetters(arr, letter) { +// arr.reduce(function(acc, next) { +// if (next.toLowerCase() !== letter.toLowerCase()) { +// return acc; +// } else { +// console.log(acc + 1); +// return acc + 1; +// } +// }, 0); +// } + +// filterLetters(["a", "a", "b", "c", "A"], "a"); // 3 +// filterLetters(["a", "a", "b", "c", "A"], "z"); // 0 +// filterLetters(["a", "a", "b", "c", "A"], "B"); // 1 diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js index 362715e..e7cb591 100644 --- a/Iterators-forEach, Map, and Filter/script.js +++ b/Iterators-forEach, Map, and Filter/script.js @@ -99,3 +99,21 @@ function filterLetters(arr, letter) { filterLetters(["a", "a", "b", "c", "A"], "a"); filterLetters(["a", "a", "b", "c", "A"], "z"); filterLetters(["a", "a", "b", "c", "A"], "B"); + +//Write a function called filterKey which accepts two parameters, an array of objects, and the name of a key and returns an array with only those objects which have truthy values for that key: + +function filterKey(arrObj, key) { + let truthyKeys = arrObj.filter(function(elem) { + return elem[key] === true; + }); + console.log(truthyKeys); +} + +filterKey( + [ + { name: "Elie", isInstructor: true, isHilarious: false }, + { name: "Tim", isInstructor: true, isHilarious: true }, + { name: "Matt", isInstructor: true } + ], + "isHilarious" +); From b8edde5ab11db48bf4e33d2394d695dda96c9c9d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 16 Jun 2019 22:44:06 +0530 Subject: [PATCH 110/161] Completed all the exercises in reduce. That felt soo good. --- Iterators-forEach, Map, and Filter/reduce.js | 52 ++++++++++++++------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/reduce.js b/Iterators-forEach, Map, and Filter/reduce.js index 22403ad..9d1ef2e 100644 --- a/Iterators-forEach, Map, and Filter/reduce.js +++ b/Iterators-forEach, Map, and Filter/reduce.js @@ -3,7 +3,6 @@ function extractKey(arrObj, key) { arrObj.reduce(function(acc, next) { acc.push(next[key]); - console.log(acc); return acc; }, []); } @@ -19,17 +18,40 @@ extractKey( //Write a function called filterLetters which accepts an array of letters and returns the number of occurrences of a specific letter. This function should be case insensitive -// function filterLetters(arr, letter) { -// arr.reduce(function(acc, next) { -// if (next.toLowerCase() !== letter.toLowerCase()) { -// return acc; -// } else { -// console.log(acc + 1); -// return acc + 1; -// } -// }, 0); -// } - -// filterLetters(["a", "a", "b", "c", "A"], "a"); // 3 -// filterLetters(["a", "a", "b", "c", "A"], "z"); // 0 -// filterLetters(["a", "a", "b", "c", "A"], "B"); // 1 +function filterLetters(arr, letter) { + arr.reduce(function(acc, next) { + if (next.toLowerCase() !== letter.toLowerCase()) { + return acc; + } else { + return acc + 1; + } + }, 0); +} + +filterLetters(["a", "a", "b", "c", "A"], "a"); // 3 +filterLetters(["a", "a", "b", "c", "A"], "z"); // 0 +filterLetters(["a", "a", "b", "c", "A"], "B"); // 1 + +// var arr = [1, 2, 3, 4]; +// arr.reduce(function(acc, next) { +// if (next >= 2) { +// acc["key" + next] = next; +// } +// return acc; +// }, {}); + +//Write a function called addKeyAndValue which accepts three parameters, an array (of objects), a key and a value. This function should return the array of objects after each key and value has been added. You can do this a few ways, either by reducing starting with an empty array and making copies of the object or by starting with the actual array! + +function addKeyAndValue(arr, key, val) { + arr.reduce(function(acc, next) { + next[key] = val; + acc.push(next); + return acc; + }, []); +} + +addKeyAndValue( + [{ name: "Elie" }, { name: "Tim" }, { name: "Elie" }], + "isInstructor", + true +); From 83d7abcc14b1fd4d2398f522816a054b634e945d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 16 Jun 2019 23:19:15 +0530 Subject: [PATCH 111/161] Reduce notes added with one more exercise --- .../Iterators-notes.md | 14 ++++++++++++++ Iterators-forEach, Map, and Filter/reduce.js | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index 0b68299..e34d748 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -35,3 +35,17 @@ So points to rememember: 2. map processes each value with callback function-returns an array, so the returned array can be assigned in an variable. 3. filter processes each value only if it satisfies a condition to become true and returns an array. It is sort of a testing function. + +4. reduce-Takes four parameters. It runs a callback once through each value in the array using the following parameters: + accumulator + currentValue + currentIndex + array + + There are two conditions to decide which one in accumulator and currenValue + Condition One: + If an initial value is set then the accumulator is that value and currentValue becomes first value in the array. + Condition Two: + If an initial value is not set then the accumulator is the first value in the array and currentValue becomes the second value in the array + + NOTE:Always to get a returned value from a function assing that function to an variable. diff --git a/Iterators-forEach, Map, and Filter/reduce.js b/Iterators-forEach, Map, and Filter/reduce.js index 9d1ef2e..bfa24a4 100644 --- a/Iterators-forEach, Map, and Filter/reduce.js +++ b/Iterators-forEach, Map, and Filter/reduce.js @@ -43,11 +43,12 @@ filterLetters(["a", "a", "b", "c", "A"], "B"); // 1 //Write a function called addKeyAndValue which accepts three parameters, an array (of objects), a key and a value. This function should return the array of objects after each key and value has been added. You can do this a few ways, either by reducing starting with an empty array and making copies of the object or by starting with the actual array! function addKeyAndValue(arr, key, val) { - arr.reduce(function(acc, next) { + var newObject = arr.reduce(function(acc, next) { next[key] = val; acc.push(next); return acc; }, []); + console.log(newObject); } addKeyAndValue( @@ -55,3 +56,14 @@ addKeyAndValue( "isInstructor", true ); + +var myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"]; + +var orderArray = myArray.reduce(function(acc, nextVal) { + if (acc.indexOf(nextVal) === -1) { + acc.push(nextVal); + } + return acc; +}, []); + +console.log(orderArray); From d2d16808be517d1db7a6530ccbf7860eb14fcd1f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 17 Jun 2019 07:30:25 +0530 Subject: [PATCH 112/161] learning reduceRight --- Iterators-forEach, Map, and Filter/reduce.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/reduce.js b/Iterators-forEach, Map, and Filter/reduce.js index bfa24a4..2fa2dd2 100644 --- a/Iterators-forEach, Map, and Filter/reduce.js +++ b/Iterators-forEach, Map, and Filter/reduce.js @@ -19,13 +19,14 @@ extractKey( //Write a function called filterLetters which accepts an array of letters and returns the number of occurrences of a specific letter. This function should be case insensitive function filterLetters(arr, letter) { - arr.reduce(function(acc, next) { + var letterCount = arr.reduce(function(acc, next) { if (next.toLowerCase() !== letter.toLowerCase()) { return acc; } else { return acc + 1; } }, 0); + console.log(letterCount); } filterLetters(["a", "a", "b", "c", "A"], "a"); // 3 @@ -67,3 +68,17 @@ var orderArray = myArray.reduce(function(acc, nextVal) { }, []); console.log(orderArray); + +var myArrayTest = [5, 4, 2, 1]; +var reduceRightVals = myArrayTest.reduceRight(function(acc, nextVal) { + return acc - nextVal; +}, 5); + +console.log(reduceRightVals); + +var myArrayTest = [5, 4, 2, 1]; +var normalReduce = myArrayTest.reduce(function(acc, nextVal) { + return acc - nextVal; +}, 5); + +console.log(normalReduce); From fe1b1a23cbdea411e60ae1c539a322664e89c094 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 18 Jun 2019 07:36:25 +0530 Subject: [PATCH 113/161] Added notes on some new iterators and testing filter and for each iterators --- .../Iterators-notes.md | 15 ++++++++++++++- Iterators-forEach, Map, and Filter/index.html | 4 ++-- Iterators-forEach, Map, and Filter/script.js | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index e34d748..0d2cfd4 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -48,4 +48,17 @@ So points to rememember: Condition Two: If an initial value is not set then the accumulator is the first value in the array and currentValue becomes the second value in the array - NOTE:Always to get a returned value from a function assing that function to an variable. + NOTE:Always to get a returned value from a function assigning that function to an variable. + + Other Array iterators are: + + ### some + + If ANY value passes a contiotion it returns a boolean + + ### every + + if ALL the value from array satisfies the condtion it returns a boolean true. + + ###find + Added in ES2015, to find an element from an array based on condition. Will return first value in the array that stisfies a condtition(if the experssion gives true). diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index e0f2431..90ae115 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -7,7 +7,7 @@ Codestin Search App - - + + diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js index e7cb591..65c1f44 100644 --- a/Iterators-forEach, Map, and Filter/script.js +++ b/Iterators-forEach, Map, and Filter/script.js @@ -117,3 +117,20 @@ filterKey( ], "isHilarious" ); + +//filter out a and move it to front of array + +var name = "sachina"; +var nameSplit = name.split(""); +var emptyArr = []; + +var filteredA = nameSplit.filter(function(val) { + return val === "a"; +}); + +var newarraywithA = filteredA.forEach(function(val) { + nameSplit.unshift(val); +}); + +console.log(filteredA); +console.log(nameSplit); From a2754c7385f6d66da9ed39d1067652c1dbaf4a47 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 19 Jun 2019 07:04:06 +0530 Subject: [PATCH 114/161] array Iterator notes added --- .../Iterators-notes.md | 13 +++++++++---- Iterators-forEach, Map, and Filter/script.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index 0d2cfd4..b9b20f3 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -54,11 +54,16 @@ So points to rememember: ### some - If ANY value passes a contiotion it returns a boolean + If ANY value passes a condition it returns a boolean ### every - if ALL the value from array satisfies the condtion it returns a boolean true. + if ALL the value from array satisfies the condition it returns a boolean true. - ###find - Added in ES2015, to find an element from an array based on condition. Will return first value in the array that stisfies a condtition(if the experssion gives true). + ### find + + Added in ES2015, to find an element from an array based on condition. Will return first value in the array that satisfies a condtition(if the experssion gives true). + + ### findIndex + + Find the first index from an array that satisfies a condition(an expression that returns true in the callback) diff --git a/Iterators-forEach, Map, and Filter/script.js b/Iterators-forEach, Map, and Filter/script.js index 65c1f44..47c9674 100644 --- a/Iterators-forEach, Map, and Filter/script.js +++ b/Iterators-forEach, Map, and Filter/script.js @@ -134,3 +134,20 @@ var newarraywithA = filteredA.forEach(function(val) { console.log(filteredA); console.log(nameSplit); + +//combining iterators + +var arrNum = [1, 2, 3, 4, 5]; + +var finalVals = arrNum + .filter(function(val) { + return val % 2 !== 0; + }) + .map(function(val) { + return val * 2; + }) + .reduce(function(acc, currentVal) { + return acc + currentVal; + }, 0); + +console.log(finalVals); From 0765692918e4415b0197af8a804d74515fa23d8a Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 19 Jun 2019 07:31:23 +0530 Subject: [PATCH 115/161] Started with main exrecises for iterators --- Iterators-forEach, Map, and Filter/index.html | 3 +- .../iterators-exercises.js | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Iterators-forEach, Map, and Filter/iterators-exercises.js diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index 90ae115..a6bc379 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -7,7 +7,8 @@ Codestin Search App - + + diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js new file mode 100644 index 0000000..70eceb4 --- /dev/null +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -0,0 +1,86 @@ +//Part I +//Use the following object for this set of questions: + +var users = [ + { + username: "larry", + email: "larry@foo.com", + yearsExperience: 22.1, + favoriteLanguages: ["Perl", "Java", "C++"], + favoriteEditor: "Vim", + hobbies: ["Fishing", "Sailing", "Hiking"], + hometown: { + city: "San Francisco", + state: "CA" + } + }, + { + username: "jane", + email: "jane@test.com", + yearsExperience: 33.9, + favoriteLanguages: ["Haskell", "Clojure", "PHP"], + favoriteEditor: "Emacs", + hobbies: ["Swimming", "Biking", "Hiking"], + hometown: { + city: "New York", + state: "NY" + } + }, + { + username: "sam", + email: "sam@test.com", + yearsExperience: 8.2, + favoriteLanguages: ["JavaScript", "Ruby", "Python", "Go"], + favoriteEditor: "Atom", + hobbies: ["Golf", "Cooking", "Archery"], + hometown: { + city: "Fargo", + state: "SD" + } + }, + { + username: "anne", + email: "anne@test.com", + yearsExperience: 4, + favoriteLanguages: ["C#", "C++", "F#"], + favoriteEditor: "Visual Studio Code", + hobbies: ["Tennis", "Biking", "Archery"], + hometown: { + city: "Albany", + state: "NY" + } + }, + { + username: "david", + email: "david@test.com", + yearsExperience: 12.5, + favoriteLanguages: ["JavaScript", "C#", "Swift"], + favoriteEditor: "VS Code", + hobbies: ["Volunteering", "Biking", "Coding"], + hometown: { + city: "Los Angeles", + state: "CA" + } + } +]; + +//Write a function called printEmails which console.log's each email for the users. +function printEmails() { + var userEmails = users.map(function(val) { + return val.email; + }); + console.log(userEmails); +} + +printEmails(); + +//Write a function called printHobbies which console.log's each hobby for each user. + +function printHobbies() { + var userHobby = users.find(function(val) { + return val.hobbies; + }); + console.log(userHobby); +} + +printHobbies(); From f116ea14a9f4cac3ac91f5a9f98b2ec3fa9dfec0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 20 Jun 2019 07:58:07 +0530 Subject: [PATCH 116/161] Iterator exercise --- .../iterators-exercises.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 70eceb4..084b6f1 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -84,3 +84,14 @@ function printHobbies() { } printHobbies(); + +// Write a function called findHometownByState which returns the first user which has a hometown of the state that is passed in + +function findHometownByState(userstate) { + var filterByState = users.find(function(val) { + return val.hometown.state === userstate; + }); + console.log(filterByState); +} + +findHometownByState("CA"); From d8e91b5bdf5aa36611fbced016c5a818e0aec1d0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 21 Jun 2019 23:54:16 +0530 Subject: [PATCH 117/161] Added new exercise --- Iterators-forEach, Map, and Filter/iterators-exercises.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 084b6f1..358b0b3 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -95,3 +95,6 @@ function findHometownByState(userstate) { } findHometownByState("CA"); + +//Write a function called allLanguages which returns an array of all of the unique values +function allLanguages() {} From 7e6650d942bebd485ad1566758ed41733adb0570 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 29 Jun 2019 09:19:29 +0530 Subject: [PATCH 118/161] Corrected the iterator exercises solutions --- .../iterators-exercises.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 358b0b3..8edb9bf 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -66,10 +66,11 @@ var users = [ //Write a function called printEmails which console.log's each email for the users. function printEmails() { - var userEmails = users.map(function(val) { - return val.email; + users.forEach(function(val) { + console.log(val.email); }); - console.log(userEmails); + + // console.log(userEmails); } printEmails(); @@ -77,10 +78,11 @@ printEmails(); //Write a function called printHobbies which console.log's each hobby for each user. function printHobbies() { - var userHobby = users.find(function(val) { - return val.hobbies; + users.map(function(val) { + return val.hobbies.forEach(function(val) { + console.log(val); + }); }); - console.log(userHobby); } printHobbies(); From e1a6418fca1bc1641ac6753465add97d6a20287e Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 1 Jul 2019 07:22:04 +0530 Subject: [PATCH 119/161] Exercise finding unique language done. Atlast! --- .../iterators-exercises.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 8edb9bf..c6ea337 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -99,4 +99,24 @@ function findHometownByState(userstate) { findHometownByState("CA"); //Write a function called allLanguages which returns an array of all of the unique values -function allLanguages() {} +function allLanguages() { + var newLangsArr = []; + users.map(function(val) { + return val.favoriteLanguages.forEach(function(val) { + newLangsArr.push(val); + }); + }); + + var uniquLangs = newLangsArr.reduce(function(acc, currentVal) { + if (acc.indexOf(currentVal) === -1) { + acc.push(currentVal); + } + + return acc; + }, []); + + console.log(uniquLangs); + return uniquLangs; +} + +allLanguages(); From a00bd981ba72f21e0c33057d2125c59f1c7b8db2 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 1 Jul 2019 07:34:00 +0530 Subject: [PATCH 120/161] Exercise using some done --- .../iterators-exercises.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index c6ea337..64eca62 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -120,3 +120,14 @@ function allLanguages() { } allLanguages(); + +//Write a function called hasFavoriteEditor which returns a boolean if any of the users have the editor passed in + +function hasFavoriteEditor(editor) { + var booleanEditor = users.some(function(val) { + return val.favoriteEditor === editor; + }); + console.log(booleanEditor); +} + +hasFavoriteEditor("Eclipse"); From 20c2c17daef539cd2b151b92c5a8424161c0c27f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 2 Jul 2019 06:58:23 +0530 Subject: [PATCH 121/161] Part one of iterators exercise done --- .../iterators-exercises.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 64eca62..6ce32a7 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -131,3 +131,20 @@ function hasFavoriteEditor(editor) { } hasFavoriteEditor("Eclipse"); + +//Write a function called findByUsername which takes in a string and returns an object in the users array that has that username +function findByUsername(name) { + var firstuser = users.find(function(val) { + return val.username === name; + }); + console.log(firstuser); + return firstuser; +} + +findByUsername("sam"); + +//PART TWO + +//Write a function called vowelCount that accepts a string and returns an object with each key being the vowel and the value being the number of times the vowel occurs in the string (the order of keys in the object does not matter). + +function vowelCount() {} From 064ccc3e6f1a98c67454dd05ac9d142775f289df Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 2 Jul 2019 07:39:37 +0530 Subject: [PATCH 122/161] part two exercise started --- .../iterators-exercises.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 6ce32a7..bca37e6 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -147,4 +147,19 @@ findByUsername("sam"); //Write a function called vowelCount that accepts a string and returns an object with each key being the vowel and the value being the number of times the vowel occurs in the string (the order of keys in the object does not matter). -function vowelCount() {} +function vowelCount(string) { + var stringSplit = string.split(""); + var vowels = ["a", "e", "i", "o", "u"]; + var counter = 0; + + var vowelobj = stringSplit.reduce(function(acc, nextVal) { + if (vowels.includes(nextVal)) { + acc[nextVal] = counter++; + } + + return acc; + }, {}); + console.log(vowelobj); +} + +vowelCount("awesome"); From 5fe37079db9da3f133b595eb9812a2380724f7b1 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 3 Jul 2019 07:43:08 +0530 Subject: [PATCH 123/161] iterators exercise --- .../iterators-exercises.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index bca37e6..a7bef4f 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -162,4 +162,21 @@ function vowelCount(string) { console.log(vowelobj); } -vowelCount("awesome"); +vowelCount("awesome man"); + +//Write a function called removeVowels that accepts a string and returns an array of each character that is not a vowel (y should not count as a vowel for this function). + +function removeVowels(string) { + var stringSplit = string.split(""); + var vowels = ["a", "e", "i", "o", "u"]; + var vowelobj = stringSplit.reduce(function(acc, nextVal) { + if (vowels.includes(nextVal)) { + acc.push(nextVal); + } + + return acc; + }, []); + console.log(vowelobj); +} + +removeVowels("amazing"); From d575a6cadf9e4c730e6797511a10deacf7481666 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 4 Jul 2019 07:40:14 +0530 Subject: [PATCH 124/161] Part two exercise in iterators done --- Iterators-forEach, Map, and Filter/iterators-exercises.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index a7bef4f..d0fb96b 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -170,7 +170,7 @@ function removeVowels(string) { var stringSplit = string.split(""); var vowels = ["a", "e", "i", "o", "u"]; var vowelobj = stringSplit.reduce(function(acc, nextVal) { - if (vowels.includes(nextVal)) { + if (vowels.indexOf(nextVal) === -1) { acc.push(nextVal); } @@ -180,3 +180,5 @@ function removeVowels(string) { } removeVowels("amazing"); +removeVowels("fun"); +removeVowels("silly"); From 9632c6e3805de67e8f408f92a37b687d13205057 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Jul 2019 07:54:14 +0530 Subject: [PATCH 125/161] Iteration finding number of vowels successfully done --- .../iterators-exercises.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index d0fb96b..048acee 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -150,19 +150,19 @@ findByUsername("sam"); function vowelCount(string) { var stringSplit = string.split(""); var vowels = ["a", "e", "i", "o", "u"]; - var counter = 0; - - var vowelobj = stringSplit.reduce(function(acc, nextVal) { - if (vowels.includes(nextVal)) { - acc[nextVal] = counter++; - } + var filteredVowels = stringSplit.filter(function(val) { + return vowels.includes(val); + }); + var vowelobj = filteredVowels.reduce(function(acc, curVal) { + acc[curVal] = ++acc[curVal] || 1; return acc; }, {}); - console.log(vowelobj); + console.log("filteredVowels", filteredVowels); + console.log("vowelobj", vowelobj); } -vowelCount("awesome man"); +vowelCount("awesome man"); //Write a function called removeVowels that accepts a string and returns an array of each character that is not a vowel (y should not count as a vowel for this function). @@ -173,7 +173,6 @@ function removeVowels(string) { if (vowels.indexOf(nextVal) === -1) { acc.push(nextVal); } - return acc; }, []); console.log(vowelobj); From 4a1291ef71c9fde4db234060598951310f7ac408 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Jul 2019 07:56:53 +0530 Subject: [PATCH 126/161] Notes related to exercise finding vowel count added in md file --- Iterators-forEach, Map, and Filter/Iterators-notes.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/Iterators-notes.md b/Iterators-forEach, Map, and Filter/Iterators-notes.md index b9b20f3..304c9a5 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-notes.md +++ b/Iterators-forEach, Map, and Filter/Iterators-notes.md @@ -67,3 +67,10 @@ So points to rememember: ### findIndex Find the first index from an array that satisfies a condition(an expression that returns true in the callback) + +this piece of code helps to find the number of times a value appears in a array and puts it in an object + +var vowelobj = filteredVowels.reduce(function(acc, curVal) { +acc[curVal] = ++acc[curVal] || 1; +return acc; +}, {}); From 467445bdcedf987431c6ee131ede8b70a3dcb51d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 5 Jul 2019 23:18:45 +0530 Subject: [PATCH 127/161] Changes in exercise answers --- Iterators-forEach, Map, and Filter/iterators-exercises.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Iterators-forEach, Map, and Filter/iterators-exercises.js b/Iterators-forEach, Map, and Filter/iterators-exercises.js index 048acee..3f3bc57 100644 --- a/Iterators-forEach, Map, and Filter/iterators-exercises.js +++ b/Iterators-forEach, Map, and Filter/iterators-exercises.js @@ -155,6 +155,7 @@ function vowelCount(string) { }); var vowelobj = filteredVowels.reduce(function(acc, curVal) { + // console.log(++acc[curVal] || 1); acc[curVal] = ++acc[curVal] || 1; return acc; }, {}); @@ -162,7 +163,7 @@ function vowelCount(string) { console.log("vowelobj", vowelobj); } -vowelCount("awesome man"); +vowelCount("awesome"); //Write a function called removeVowels that accepts a string and returns an array of each character that is not a vowel (y should not count as a vowel for this function). @@ -173,6 +174,7 @@ function removeVowels(string) { if (vowels.indexOf(nextVal) === -1) { acc.push(nextVal); } + return acc; }, []); console.log(vowelobj); From 48db5c22dce3f6a7f02b9efa9af0d0511889fd2c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 6 Jul 2019 09:33:38 +0530 Subject: [PATCH 128/161] Another exercise on array iterators --- .../Iterators-exercise-two.js | 1831 +++++++++++++++++ Iterators-forEach, Map, and Filter/index.html | 3 +- 2 files changed, 1833 insertions(+), 1 deletion(-) create mode 100644 Iterators-forEach, Map, and Filter/Iterators-exercise-two.js diff --git a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js new file mode 100644 index 0000000..0356d3f --- /dev/null +++ b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js @@ -0,0 +1,1831 @@ +var songs = [ + { + name: "Smooth", + artist: "Santana featuring Rob Thomas", + year: 1999, + month: 10, + day: 23, + weeksAtNumberOne: 12, + duration: "4:00" + }, + { + name: "What a Girl Wants", + artist: "Christina Aguilera", + year: 2000, + month: 1, + day: 15, + weeksAtNumberOne: 2, + duration: "3:20" + }, + { + name: "I Knew I Loved You", + artist: "Savage Garden", + year: 2000, + month: 1, + day: 29, + weeksAtNumberOne: 4, + duration: "4:10" + }, + { + name: "Thank God I Found You", + artist: "Mariah Carey featuring Joe and 98 Degrees", + year: 2000, + month: 2, + day: 19, + weeksAtNumberOne: 2, + duration: "4:17" + }, + { + name: "Amazed", + artist: "Lonestar", + year: 2000, + month: 3, + day: 4, + weeksAtNumberOne: 2, + duration: "4:00" + }, + { + name: "Say My Name", + artist: "Destiny's Child", + year: 2000, + month: 3, + day: 18, + weeksAtNumberOne: 3, + duration: "4:31" + }, + { + name: "Maria Maria", + artist: "Santana featuring The Product G&B", + year: 2000, + month: 4, + day: 8, + weeksAtNumberOne: 10, + duration: "4:23" + }, + { + name: "Try Again", + artist: "Aaliyah", + year: 2000, + month: 6, + day: 17, + weeksAtNumberOne: 1, + duration: "4:49" + }, + { + name: "Be With You", + artist: "Enrique Iglesias", + year: 2000, + month: 6, + day: 24, + weeksAtNumberOne: 3, + duration: "3:40" + }, + { + name: "Everything You Want", + artist: "Vertical Horizon", + year: 2000, + month: 7, + day: 15, + weeksAtNumberOne: 1, + duration: "4:06" + }, + { + name: "Bent", + artist: "Matchbox Twenty", + year: 2000, + month: 7, + day: 22, + weeksAtNumberOne: 1, + duration: "4:16" + }, + { + name: "It's Gonna Be Me", + artist: "NSYNC", + year: 2000, + month: 7, + day: 29, + weeksAtNumberOne: 2, + duration: "3:12" + }, + { + name: "Incomplete", + artist: "Sisqo", + year: 2000, + month: 8, + day: 12, + weeksAtNumberOne: 2, + duration: "4:12" + }, + { + name: "Doesn't Really Matter", + artist: "Janet Jackson", + year: 2000, + month: 8, + day: 26, + weeksAtNumberOne: 3, + duration: "4:25" + }, + { + name: "Music", + artist: "Madonna", + year: 2000, + month: 9, + day: 16, + weeksAtNumberOne: 4, + duration: "3:44" + }, + { + name: "Come On Over Baby (All I Want Is You)", + artist: "Christina Aguilera", + year: 2000, + month: 8, + day: 26, + weeksAtNumberOne: 4, + duration: "3:23" + }, + { + name: "With Arms Wide Open", + artist: "Creed", + year: 2000, + month: 11, + day: 11, + weeksAtNumberOne: 1, + duration: "3:53" + }, + { + name: "Independent Woman Part I", + artist: "Destiny's Child", + year: 2000, + month: 11, + day: 18, + weeksAtNumberOne: 11, + duration: "3:37" + }, + { + name: "It Wasn't Me", + artist: 'Shaggy featuring Ricardo "RikRok" Ducent', + year: 2001, + month: 2, + day: 3, + weeksAtNumberOne: 2, + duration: "3:47" + }, + { + name: "Ms. Jackson", + artist: "OutKast", + year: 2001, + month: 2, + day: 17, + weeksAtNumberOne: 1, + duration: "4:03" + }, + { + name: "Stutter", + artist: "Joe featuring Mystikal", + year: 2001, + month: 2, + day: 24, + weeksAtNumberOne: 4, + duration: "3:33" + }, + { + name: "Butterfly", + artist: "Crazy Town", + year: 2001, + month: 3, + day: 24, + weeksAtNumberOne: 2, + duration: "3:36" + }, + { + name: "Angel", + artist: "Shaggy featuring Rayvon", + year: 2001, + month: 3, + day: 31, + weeksAtNumberOne: 1, + duration: "3:31" + }, + { + name: "All for You", + artist: "Janet Jackson", + year: 2001, + month: 4, + day: 14, + weeksAtNumberOne: 7, + duration: "4:24" + }, + { + name: "Lady Marmalade", + artist: "Christina Aguilera, Lil' Kim, Mya, and Pink", + year: 2001, + month: 6, + day: 2, + weeksAtNumberOne: 5, + duration: "4:24" + }, + { + name: "U Remind Me", + artist: "Usher", + year: 2001, + month: 7, + day: 7, + weeksAtNumberOne: 4, + duration: "4:27" + }, + { + name: "Bootylicious", + artist: "Destiny's Child", + year: 2001, + month: 8, + day: 4, + weeksAtNumberOne: 2, + duration: "3:27" + }, + { + name: "Fallin", + artist: "Alicia Keys", + year: 2001, + month: 8, + day: 18, + weeksAtNumberOne: 6, + duration: "3:30" + }, + { + name: "I'm Real", + artist: "Jennifer Lopez featuring Ja Rule", + year: 2001, + month: 9, + day: 8, + weeksAtNumberOne: 5, + duration: "4:15" + }, + { + name: "Family Affair", + artist: "Mary J. Blige", + year: 2001, + month: 11, + day: 3, + weeksAtNumberOne: 6, + duration: "4:04" + }, + { + name: "U Got It Bad", + artist: "Usher", + year: 2001, + month: 12, + day: 15, + weeksAtNumberOne: 6, + duration: "4:07" + }, + { + name: "How You Remind Me", + artist: "Nickelback", + year: 2001, + month: 12, + day: 22, + weeksAtNumberOne: 4, + duration: "3:43" + }, + { + name: "Always on Time", + artist: "Ja Rule featuring Ashanti", + year: 2002, + month: 2, + day: 23, + weeksAtNumberOne: 2, + duration: "4:05" + }, + { + name: "Ain't It Funny", + artist: "Jennifer Lopez featuring Ja Rule", + year: 2002, + month: 3, + day: 9, + weeksAtNumberOne: 6, + duration: "3:36" + }, + { + name: "Foolish", + artist: "Ashanti", + year: 2002, + month: 4, + day: 20, + weeksAtNumberOne: 10, + duration: "3:47" + }, + { + name: "Hot in Herre", + artist: "Nelly", + year: 2002, + month: 6, + day: 29, + weeksAtNumberOne: 7, + duration: "3:49" + }, + { + name: "Dilemma", + artist: "Nelly featuring Kelly Rowland", + year: 2002, + month: 8, + day: 17, + weeksAtNumberOne: 10, + duration: "3:55" + }, + { + name: "A Moment Like This", + artist: "Kelly Clarkson", + year: 2002, + month: 10, + day: 5, + weeksAtNumberOne: 2, + duration: "3:49" + }, + { + name: "Lose Yourself", + artist: "Eminem", + year: 2002, + month: 11, + day: 9, + weeksAtNumberOne: 12, + duration: "4:27" + }, + { + name: "Bump, Bump, Bump", + artist: "B2K featuring P. Diddy", + year: 2003, + month: 2, + day: 1, + weeksAtNumberOne: 1, + duration: "3:56" + }, + { + name: "All I Have", + artist: "Jennifer Lopez featuring LL Cool J", + year: 2003, + month: 2, + day: 8, + weeksAtNumberOne: 4, + duration: "4:17" + }, + { + name: "In da Club", + artist: "50 Cent", + year: 2003, + month: 3, + day: 8, + weeksAtNumberOne: 9, + duration: "3:40" + }, + { + name: "Get Busy", + artist: "Sean Paul", + year: 2003, + month: 5, + day: 10, + weeksAtNumberOne: 3, + duration: "3:32" + }, + { + name: "21 Questions", + artist: "50 Cent featuring Nate Dogg", + year: 2003, + month: 5, + day: 31, + weeksAtNumberOne: 4, + duration: "3:44" + }, + { + name: "This Is the Night", + artist: "Clay Aiken", + year: 2003, + month: 6, + day: 28, + weeksAtNumberOne: 2, + duration: "3:32" + }, + { + name: "Crazy in Love", + artist: "Beyonce featuring Jay-Z", + year: 2003, + month: 7, + day: 12, + weeksAtNumberOne: 8, + duration: "3:56" + }, + { + name: "Shake Ya Tailfeather", + artist: "Nelly, P. Diddy, and Murphy Lee", + year: 2003, + month: 9, + day: 6, + weeksAtNumberOne: 4, + duration: "4:53" + }, + { + name: "Baby Boy", + artist: "Beyonce featuring Sean Paul", + year: 2003, + month: 10, + day: 4, + weeksAtNumberOne: 9, + duration: "4:04" + }, + { + name: "Stand Up", + artist: "Ludacris featuring Shawna", + year: 2003, + month: 12, + day: 6, + weeksAtNumberOne: 1, + duration: "3:33" + }, + { + name: "Hey Ya!", + artist: "OutKast", + year: 2003, + month: 12, + day: 13, + weeksAtNumberOne: 9, + duration: "3:55" + }, + { + name: "The Way You Move", + artist: "OutKast featuring Sleepy Brown", + year: 2004, + month: 2, + day: 14, + weeksAtNumberOne: 1, + duration: "3:55" + }, + { + name: "Slow Jamz", + artist: "Twista featuring Kanye West and Jamie Foxx", + year: 2004, + month: 2, + day: 21, + weeksAtNumberOne: 1, + duration: "3:34" + }, + { + name: "Yeah!", + artist: "Usher featuring Lil Jon and Ludacris", + year: 2004, + month: 2, + day: 28, + weeksAtNumberOne: 12, + duration: "4:10" + }, + { + name: "Burn", + artist: "Usher", + year: 2004, + month: 5, + day: 22, + weeksAtNumberOne: 8, + duration: "4:15" + }, + { + name: "I Believe", + artist: "Fantasia", + year: 2004, + month: 7, + day: 10, + weeksAtNumberOne: 1, + duration: "4:07" + }, + { + name: "Confessions Part II", + artist: "Usher", + year: 2004, + month: 7, + day: 24, + weeksAtNumberOne: 2, + duration: "3:49" + }, + { + name: "Slow Motion", + artist: "Juvenile featuring Soulja Slim", + year: 2004, + month: 8, + day: 7, + weeksAtNumberOne: 2, + duration: "4:08" + }, + { + name: "Lean Back", + artist: "Terror Squad", + year: 2004, + month: 8, + day: 21, + weeksAtNumberOne: 3, + duration: "4:07" + }, + { + name: "Goodies", + artist: "Ciara featuring Petey Pablo", + year: 2004, + month: 9, + day: 11, + weeksAtNumberOne: 7, + duration: "3:43" + }, + { + name: "My Boo", + artist: "Usher and Alicia Keys", + year: 2004, + month: 10, + day: 30, + weeksAtNumberOne: 6, + duration: "3:42" + }, + { + name: "Drop It Like It's Hot", + artist: "Snoop Dogg featuring Pharrell", + year: 2004, + month: 12, + day: 11, + weeksAtNumberOne: 3, + duration: "4:30" + }, + { + name: "Let Me Love You", + artist: "Mario", + year: 2005, + month: 1, + day: 1, + weeksAtNumberOne: 9, + duration: "4:09" + }, + { + name: "Candy Shop", + artist: "50 Cent featuring Olivia", + year: 2005, + month: 3, + day: 5, + weeksAtNumberOne: 9, + duration: "3:29" + }, + { + name: "Hollaback Girl", + artist: "Gwen Stefani", + year: 2005, + month: 5, + day: 7, + weeksAtNumberOne: 4, + duration: "3:19" + }, + { + name: "We Belong Together", + artist: "Mariah Carey", + year: 2005, + month: 6, + day: 4, + weeksAtNumberOne: 14, + duration: "3:21" + }, + { + name: "Inside Your Heaven", + artist: "Carrie Underwood", + year: 2005, + month: 7, + day: 2, + weeksAtNumberOne: 1, + duration: "3:45" + }, + { + name: "Gold Digger", + artist: "Kanye West featuring Jamie Foxx", + year: 2005, + month: 9, + day: 17, + weeksAtNumberOne: 10, + duration: "3:28" + }, + { + name: "Run It!", + artist: "Chris Brown", + year: 2005, + month: 11, + day: 26, + weeksAtNumberOne: 5, + duration: "3:12" + }, + { + name: "Don't Forget About Us", + artist: "Mariah Carey", + year: 2005, + month: 12, + day: 31, + weeksAtNumberOne: 2, + duration: "3:53" + }, + { + name: "Laffy Taffy", + artist: "D4L", + year: 2006, + month: 1, + day: 14, + weeksAtNumberOne: 1, + duration: "3:44" + }, + { + name: "Grillz", + artist: "Nelly featuring Paul Wall, Ali & Gipp", + year: 2006, + month: 1, + day: 21, + weeksAtNumberOne: 2, + duration: "4:30" + }, + { + name: "Check on It", + artist: "Beyonce featuring Slim Thug", + year: 2006, + month: 2, + day: 4, + weeksAtNumberOne: 5, + duration: "3:30" + }, + { + name: "You're Beautiful", + artist: "James Blunt", + year: 2006, + month: 3, + day: 11, + weeksAtNumberOne: 1, + duration: "3:22" + }, + { + name: "So Sick", + artist: "Ne-Yo", + year: 2006, + month: 3, + day: 18, + weeksAtNumberOne: 2, + duration: "3:27" + }, + { + name: "Temperature", + artist: "Sean Paul", + year: 2006, + month: 4, + day: 1, + weeksAtNumberOne: 1, + duration: "3:36" + }, + { + name: "Bad Day", + artist: "Daniel Powter", + year: 2006, + month: 4, + day: 8, + weeksAtNumberOne: 5, + duration: "3:54" + }, + { + name: "SOS", + artist: "Rihanna", + year: 2006, + month: 5, + day: 13, + weeksAtNumberOne: 3, + duration: "3:59" + }, + { + name: "Ridin'", + artist: "Chamillionaire featuring Krayzie Bone", + year: 2006, + month: 6, + day: 3, + weeksAtNumberOne: 2, + duration: "4:08" + }, + { + name: "Hips Don't Lie", + artist: "Shakira featuring Wyclef Jean", + year: 2006, + month: 6, + day: 17, + weeksAtNumberOne: 2, + duration: "3:38" + }, + { + name: "Do I Make You Proud", + artist: "Taylor Hicks", + year: 2006, + month: 7, + day: 1, + weeksAtNumberOne: 1, + duration: "4:13" + }, + { + name: "Promiscuous", + artist: "Nelly Furtado featuring Timbaland", + year: 2006, + month: 7, + day: 8, + weeksAtNumberOne: 6, + duration: "4:02" + }, + { + name: "London Bridge", + artist: "Fergie", + year: 2006, + month: 8, + day: 19, + weeksAtNumberOne: 3, + duration: "3:28" + }, + { + name: "SexyBack", + artist: "Justin Timberlake", + year: 2006, + month: 9, + day: 9, + weeksAtNumberOne: 7, + duration: "4:02" + }, + { + name: "Money Maker", + artist: "Ludacris featuring Pharrell", + year: 2006, + month: 10, + day: 28, + weeksAtNumberOne: 2, + duration: "3:54" + }, + { + name: "My Love", + artist: "Justin Timberlake featuring T.I.", + year: 2006, + month: 11, + day: 11, + weeksAtNumberOne: 3, + duration: "4:41" + }, + { + name: "I Wanna Love You", + artist: "Akon featuring Snoop Dogg", + year: 2006, + month: 12, + day: 2, + weeksAtNumberOne: 2, + duration: "4:07" + }, + { + name: "Irreplaceable", + artist: "Beyonce", + year: 2006, + month: 12, + day: 16, + weeksAtNumberOne: 10, + duration: "3:47" + }, + { + name: "Say It Right", + artist: "Nelly Furtado", + year: 2007, + month: 2, + day: 24, + weeksAtNumberOne: 1, + duration: "3:43" + }, + { + name: "What Goes Around...Comes Around", + artist: "Justin Timberlake", + year: 2007, + month: 3, + day: 3, + weeksAtNumberOne: 1, + duration: "5:09" + }, + { + name: "This Is Why I'm Hot", + artist: "Mims", + year: 2007, + month: 3, + day: 10, + weeksAtNumberOne: 2, + duration: "4:13" + }, + { + name: "Glamorous", + artist: "Fergie featuring Ludacris", + year: 2007, + month: 3, + day: 24, + weeksAtNumberOne: 2, + duration: "4:06" + }, + { + name: "Don't Matter", + artist: "Akon", + year: 2007, + month: 4, + day: 7, + weeksAtNumberOne: 2, + duration: "4:54" + }, + { + name: "Give It to Me", + artist: "Timbaland featuring Nelly Furtado and Justin Timberlake", + year: 2007, + month: 4, + day: 21, + weeksAtNumberOne: 2, + duration: "3:58" + }, + { + name: "Girlfriend", + artist: "Avril Lavigne", + year: 2007, + month: 5, + day: 5, + weeksAtNumberOne: 1, + duration: "3:37" + }, + { + name: "Makes Me Wonder", + artist: "Maroon 5", + year: 2007, + month: 5, + day: 12, + weeksAtNumberOne: 3, + duration: "3:31" + }, + { + name: "Buy U a Drank (Shawty Snappin')", + artist: "T-Pain featuring Yung Joc", + year: 2007, + month: 5, + day: 26, + weeksAtNumberOne: 1, + duration: "3:48" + }, + { + name: "Umbrella", + artist: "Rihanna featuring Jay-Z", + year: 2007, + month: 6, + day: 9, + weeksAtNumberOne: 7, + duration: "4:36" + }, + { + name: "Hey There Delilah", + artist: "Plain White T's", + year: 2007, + month: 7, + day: 28, + weeksAtNumberOne: 2, + duration: "3:52" + }, + { + name: "Beautiful Girls", + artist: "Sean Kingston", + year: 2007, + month: 8, + day: 11, + weeksAtNumberOne: 4, + duration: "3:43" + }, + { + name: "Big Girls Don't Cry", + artist: "Fergie", + year: 2007, + month: 9, + day: 8, + weeksAtNumberOne: 1, + duration: "4:28" + }, + { + name: "Crank That (Soulja Boy)", + artist: "Soulja Boy Tell 'Em", + year: 2007, + month: 9, + day: 15, + weeksAtNumberOne: 7, + duration: "3:42" + }, + { + name: "Stronger", + artist: "Kanye West", + year: 2007, + month: 9, + day: 29, + weeksAtNumberOne: 1, + duration: "4:04" + }, + { + name: "Kiss Kiss", + artist: "Chris Brown featuring T-Pain", + year: 2007, + month: 11, + day: 10, + weeksAtNumberOne: 3, + duration: "4:10" + }, + { + name: "No One", + artist: "Alicia Keys", + year: 2007, + month: 12, + day: 1, + weeksAtNumberOne: 5, + duration: "4:10" + }, + { + name: "Low", + artist: "Flo Rida featuring T-Pain", + year: 2008, + month: 1, + day: 5, + weeksAtNumberOne: 10, + duration: "3:50" + }, + { + name: "Love in This Club", + artist: "Usher featuring Young Jeezy", + year: 2008, + month: 3, + day: 15, + weeksAtNumberOne: 3, + duration: "4:19" + }, + { + name: "Bleeding Love", + artist: "Leona Lewis", + year: 2008, + month: 4, + day: 5, + weeksAtNumberOne: 4, + duration: "4:22" + }, + { + name: "Touch My Body", + artist: "Mariah Carey", + year: 2008, + month: 4, + day: 12, + weeksAtNumberOne: 2, + duration: "3:24" + }, + { + name: "Lollipop", + artist: "Lil Wayne featuring Static Major", + year: 2008, + month: 5, + day: 3, + weeksAtNumberOne: 5, + duration: "4:07" + }, + { + name: "Take a Bow", + artist: "Rihanna", + year: 2008, + month: 5, + day: 24, + weeksAtNumberOne: 1, + duration: "3:48" + }, + { + name: "Viva la Vida", + artist: "Coldplay", + year: 2008, + month: 6, + day: 28, + weeksAtNumberOne: 1, + duration: "3:45" + }, + { + name: "I Kissed a Girl", + artist: "Katy Perry", + year: 2008, + month: 7, + day: 5, + weeksAtNumberOne: 7, + duration: "3:00" + }, + { + name: "Disturbia", + artist: "Rihanna", + year: 2008, + month: 8, + day: 23, + weeksAtNumberOne: 2, + duration: "3:59" + }, + { + name: "Whatever You Like", + artist: "T.I.", + year: 2008, + month: 9, + day: 6, + weeksAtNumberOne: 7, + duration: "3:40" + }, + { + name: "So What", + artist: "Pink", + year: 2008, + month: 9, + day: 27, + weeksAtNumberOne: 1, + duration: "3:35" + }, + { + name: "Live Your Life", + artist: "T.I. featuring Rihanna", + year: 2008, + month: 10, + day: 18, + weeksAtNumberOne: 6, + duration: "4:01" + }, + { + name: "Womanizer", + artist: "Britney Spears", + year: 2008, + month: 10, + day: 25, + weeksAtNumberOne: 1, + duration: "3:44" + }, + { + name: "Single Ladies (Put a Ring on It)", + artist: "Beyonce", + year: 2008, + month: 12, + day: 13, + weeksAtNumberOne: 4, + duration: "3:13" + }, + { + name: "Just Dance", + artist: "Lady Gaga featuring Colby O'Donis", + year: 2009, + month: 1, + day: 17, + weeksAtNumberOne: 3, + duration: "4:01" + }, + { + name: "My Life Would Suck Without You", + artist: "Kelly Clarkson", + year: 2009, + month: 2, + day: 7, + weeksAtNumberOne: 2, + duration: "3:31" + }, + { + name: "Crack a Bottle", + artist: "Eminem, Dr. Dre, and 50 Cent", + year: 2009, + month: 2, + day: 21, + weeksAtNumberOne: 1, + duration: "4:57" + }, + { + name: "Right Round", + artist: "Flo Rida", + year: 2009, + month: 2, + day: 28, + weeksAtNumberOne: 6, + duration: "3:27" + }, + { + name: "Poker Face", + artist: "Lady Gaga", + year: 2009, + month: 4, + day: 11, + weeksAtNumberOne: 1, + duration: "3:58" + }, + { + name: "Boom Boom Pow", + artist: "The Black Eyed Peas", + year: 2009, + month: 4, + day: 18, + weeksAtNumberOne: 12, + duration: "3:38" + }, + { + name: "I Gotta Feeling", + artist: "The Black Eyed Peas", + year: 2009, + month: 7, + day: 11, + weeksAtNumberOne: 14, + duration: "4:05" + }, + { + name: "Down", + artist: "Jay Sean featuring Lil Wayne", + year: 2009, + month: 10, + day: 17, + weeksAtNumberOne: 2, + duration: "3:32" + }, + { + name: "3", + artist: "Britney Spears", + year: 2009, + month: 10, + day: 24, + weeksAtNumberOne: 1, + duration: "3:33" + }, + { + name: "Fireflies", + artist: "Owl City", + year: 2009, + month: 11, + day: 7, + weeksAtNumberOne: 2, + duration: "3:48" + }, + { + name: "Whatcha Say", + artist: "Jason Derulo", + year: 2009, + month: 11, + day: 14, + weeksAtNumberOne: 1, + duration: "3:46" + }, + { + name: "Empire State of Mind", + artist: "Jay-Z and Alicia Keys", + year: 2009, + month: 11, + day: 28, + weeksAtNumberOne: 5, + duration: "4:36" + }, + { + name: "Tik Tok", + artist: "Kesha", + year: 2010, + month: 1, + day: 2, + weeksAtNumberOne: 9, + duration: "3:21" + }, + { + name: "Imma Be", + artist: "The Black Eyed Peas", + year: 2010, + month: 3, + day: 6, + weeksAtNumberOne: 2, + duration: "3:53" + }, + { + name: "Break Your Heart", + artist: "Taio Cruz featuring Ludacris", + year: 2010, + month: 3, + day: 20, + weeksAtNumberOne: 1, + duration: "3:23" + }, + { + name: "Rude Boy", + artist: "Rihanna", + year: 2010, + month: 3, + day: 27, + weeksAtNumberOne: 5, + duration: "3:43" + }, + { + name: "Nothin' on You", + artist: "B.o.B. featuring Bruno Mars", + year: 2010, + month: 5, + day: 1, + weeksAtNumberOne: 2, + duration: "4:29" + }, + { + name: "OMG", + artist: "Usher featuring will.i.am", + year: 2010, + month: 5, + day: 15, + weeksAtNumberOne: 4, + duration: "4:28" + }, + { + name: "Not Afraid", + artist: "Eminem", + year: 2010, + month: 5, + day: 22, + weeksAtNumberOne: 1, + duration: "4:10" + }, + { + name: "California Gurls", + artist: "Katy Perry", + year: 2010, + month: 6, + day: 19, + weeksAtNumberOne: 6, + duration: "3:56" + }, + { + name: "Love the Way You Lie", + artist: "Eminem featuring Rihanna", + year: 2010, + month: 7, + day: 31, + weeksAtNumberOne: 7, + duration: "4:23" + }, + { + name: "Teenage Dream", + artist: "Katy Perry", + year: 2010, + month: 9, + day: 18, + weeksAtNumberOne: 2, + duration: "3:47" + }, + { + name: "Just the Way You Are", + artist: "Bruno Mars", + year: 2010, + month: 10, + day: 2, + weeksAtNumberOne: 4, + duration: "3:41" + }, + { + name: "Like a G6", + artist: "Far East Movement featuring The Cataracs and Dev", + year: 2010, + month: 10, + day: 30, + weeksAtNumberOne: 3, + duration: "3:38" + }, + { + name: "We R Who We R", + artist: "Kesha", + year: 2010, + month: 11, + day: 13, + weeksAtNumberOne: 1, + duration: "3:24" + }, + { + name: "What's My Name?", + artist: "Rihanna featuring Drake", + year: 2010, + month: 11, + day: 20, + weeksAtNumberOne: 1, + duration: "4:23" + }, + { + name: "Only Girl (In the World)", + artist: "Rihanna", + year: 2010, + month: 12, + day: 4, + weeksAtNumberOne: 1, + duration: "3:55" + }, + { + name: "Raise Your Glass", + artist: "Pink", + year: 2010, + month: 12, + day: 11, + weeksAtNumberOne: 1, + duration: "3:23" + }, + { + name: "Firework", + artist: "Katy Perry", + year: 2010, + month: 12, + day: 18, + weeksAtNumberOne: 4, + duration: "3:46" + }, + { + name: "Grenade", + artist: "Bruno Mars", + year: 2011, + month: 1, + day: 8, + weeksAtNumberOne: 4, + duration: "3:42" + }, + { + name: "Hold It Against Me", + artist: "Britney Spears", + year: 2011, + month: 1, + day: 29, + weeksAtNumberOne: 1, + duration: "3:49" + }, + { + name: "Black and Yellow", + artist: "Wiz Khalifa", + year: 2011, + month: 2, + day: 19, + weeksAtNumberOne: 1, + duration: "3:37" + }, + { + name: "Born This Way", + artist: "Lady Gaga", + year: 2011, + month: 2, + day: 26, + weeksAtNumberOne: 6, + duration: "4:20" + }, + { + name: "E.T.", + artist: "Katy Perry featuring Kanye West", + year: 2011, + month: 4, + day: 9, + weeksAtNumberOne: 5, + duration: "3:51" + }, + { + name: "S&M", + artist: "Rihanna", + year: 2011, + month: 4, + day: 30, + weeksAtNumberOne: 1, + duration: "4:03" + }, + { + name: "Rolling in the Deep", + artist: "Adele", + year: 2011, + month: 5, + day: 21, + weeksAtNumberOne: 7, + duration: "3:48" + }, + { + name: "Give Me Everything", + artist: "Pitbull featuring Ne-Yo, Afrojack, and Nayer", + year: 2011, + month: 7, + day: 9, + weeksAtNumberOne: 1, + duration: "4:12" + }, + { + name: "Party Rock Anthem", + artist: "LMFAO featuring Lauren Bennett and GoonRock", + year: 2011, + month: 7, + day: 16, + weeksAtNumberOne: 6, + duration: "3:52" + }, + { + name: "Last Friday Night (T.G.I.F.)", + artist: "Katy Perry", + year: 2011, + month: 8, + day: 27, + weeksAtNumberOne: 2, + duration: "3:50" + }, + { + name: "Moves Like Jagger", + artist: "Maroon 5 featuring Christina Aguilera", + year: 2011, + month: 9, + day: 10, + weeksAtNumberOne: 4, + duration: "3:21" + }, + { + name: "Someone Like You", + artist: "Adele", + year: 2011, + month: 9, + day: 17, + weeksAtNumberOne: 5, + duration: "4:45" + }, + { + name: "We Found Love", + artist: "Rihanna featuring Calvin Harris", + year: 2011, + month: 11, + day: 12, + weeksAtNumberOne: 10, + duration: "3:35" + }, + { + name: "Sexy and I Know It", + artist: "LMFAO", + year: 2012, + month: 1, + day: 7, + weeksAtNumberOne: 2, + duration: "3:19" + }, + { + name: "Set Fire to the Rain", + artist: "Adele", + year: 2012, + month: 2, + day: 4, + weeksAtNumberOne: 2, + duration: "4:02" + }, + { + name: "Stronger (What Doesn't Kill You)", + artist: "Kelly Clarkson", + year: 2012, + month: 2, + day: 18, + weeksAtNumberOne: 3, + duration: "3:42" + }, + { + name: "Part of Me", + artist: "Katy Perry", + year: 2012, + month: 3, + day: 3, + weeksAtNumberOne: 1, + duration: "3:35" + }, + { + name: "We Are Young", + artist: "Fun featuring Janelle Monae", + year: 2012, + month: 3, + day: 17, + weeksAtNumberOne: 6, + duration: "4:10" + }, + { + name: "Somebody That I Used to Know", + artist: "Gotye featuring Kimbra", + year: 2012, + month: 4, + day: 28, + weeksAtNumberOne: 8, + duration: "3:33" + }, + { + name: "Call Me Maybe", + artist: "Carly Rae Jepsen", + year: 2012, + month: 6, + day: 23, + weeksAtNumberOne: 9, + duration: "3:13" + }, + { + name: "Whistle", + artist: "Flo Rida", + year: 2012, + month: 8, + day: 25, + weeksAtNumberOne: 2, + duration: "3:45" + }, + { + name: "We Are Never Ever Getting Back Together", + artist: "Taylor Swift", + year: 2012, + month: 9, + day: 1, + weeksAtNumberOne: 3, + duration: "3:12" + }, + { + name: "One More Night", + artist: "Maroon 5", + year: 2012, + month: 9, + day: 29, + weeksAtNumberOne: 9, + duration: "3:39" + }, + { + name: "Diamonds", + artist: "Rihanna", + year: 2012, + month: 12, + day: 1, + weeksAtNumberOne: 3, + duration: "3:45" + }, + { + name: "Locked Out of Heaven", + artist: "Bruno Mars", + year: 2012, + month: 12, + day: 22, + weeksAtNumberOne: 6, + duration: "3:53" + }, + { + name: "Thrift Shop", + artist: "Macklemore & Ryan Lewis featuring Wanz", + year: 2013, + month: 2, + day: 2, + weeksAtNumberOne: 6, + duration: "3:55" + }, + { + name: "Harlem Shake", + artist: "Baauer", + year: 2013, + month: 3, + day: 2, + weeksAtNumberOne: 5, + duration: "3:16" + }, + { + name: "When I Was Your Man", + artist: "Bruno Mars", + year: 2013, + month: 4, + day: 20, + weeksAtNumberOne: 1, + duration: "3:34" + }, + { + name: "Just Give Me a Reason", + artist: "Pink featuring Nate Ruess", + year: 2013, + month: 4, + day: 27, + weeksAtNumberOne: 3, + duration: "4:02" + }, + { + name: "Can't Hold Us", + artist: "Macklemore & Ryan Lewis featuring Ray Dalton", + year: 2013, + month: 5, + day: 18, + weeksAtNumberOne: 5, + duration: "3:48" + }, + { + name: "Blurred Lines", + artist: "Robin Thicke featuring T.I. and Pharrell", + year: 2013, + month: 6, + day: 22, + weeksAtNumberOne: 12, + duration: "4:25" + }, + { + name: "Roar", + artist: "Katy Perry", + year: 2013, + month: 9, + day: 14, + weeksAtNumberOne: 2, + duration: "3:42" + }, + { + name: "Wrecking Ball", + artist: "Miley Cyrus", + year: 2013, + month: 9, + day: 28, + weeksAtNumberOne: 3, + duration: "3:41" + }, + { + name: "Royals", + artist: "Lorde", + year: 2013, + month: 10, + day: 12, + weeksAtNumberOne: 9, + duration: "3:10" + }, + { + name: "The Monster", + artist: "Eminem featuring Rihanna", + year: 2013, + month: 12, + day: 21, + weeksAtNumberOne: 4, + duration: "4:10" + }, + { + name: "Timber", + artist: "Pitbull featuring Kesha", + year: 2014, + month: 1, + day: 18, + weeksAtNumberOne: 3, + duration: "3:24" + }, + { + name: "Dark Horse", + artist: "Katy Perry featuring Juicy J", + year: 2014, + month: 2, + day: 8, + weeksAtNumberOne: 4, + duration: "3:35" + }, + { + name: "Happy", + artist: "Pharrell Williams", + year: 2014, + month: 3, + day: 8, + weeksAtNumberOne: 10, + duration: "3:53" + }, + { + name: "All of Me", + artist: "John Legend", + year: 2014, + month: 5, + day: 17, + weeksAtNumberOne: 3, + duration: "4:29" + }, + { + name: "Fancy", + artist: "Iggy Izalea featuring Charli XCX", + year: 2014, + month: 6, + day: 7, + weeksAtNumberOne: 7, + duration: "3:19" + }, + { + name: "Rude", + artist: "Magic!", + year: 2014, + month: 7, + day: 26, + weeksAtNumberOne: 6, + duration: "3:44" + }, + { + name: "Shake It Off", + artist: "Taylor Swift", + year: 2014, + month: 9, + day: 6, + weeksAtNumberOne: 4, + duration: "3:39" + }, + { + name: "All About That Bass", + artist: "Meghan Trainor", + year: 2014, + month: 9, + day: 20, + weeksAtNumberOne: 8, + duration: "3:11" + }, + { + name: "Blank Space", + artist: "Taylor Swift", + year: 2014, + month: 11, + day: 29, + weeksAtNumberOne: 7, + duration: "3:51" + }, + { + name: "Uptown Funk", + artist: "Mark Ronson featuring Bruno Mars", + year: 2015, + month: 1, + day: 17, + weeksAtNumberOne: 14, + duration: "3:58" + }, + { + name: "See You Again", + artist: "Wiz Khalifa featuring Charlie Puth", + year: 2015, + month: 4, + day: 25, + weeksAtNumberOne: 12, + duration: "3:57" + }, + { + name: "Bad Blood", + artist: "Taylor Swift featuring Kendrick Lamar", + year: 2015, + month: 6, + day: 6, + weeksAtNumberOne: 1, + duration: "3:19" + }, + { + name: "Cheerleader", + artist: "OMI", + year: 2015, + month: 7, + day: 25, + weeksAtNumberOne: 6, + duration: "2:58" + }, + { + name: "Can't Feel My Face", + artist: "The Weeknd", + year: 2015, + month: 8, + day: 22, + weeksAtNumberOne: 3, + duration: "3:35" + }, + { + name: "What Do You Mean?", + artist: "Justin Bieber", + year: 2015, + month: 9, + day: 19, + weeksAtNumberOne: 1, + duration: "3:26" + }, + { + name: "The Hills", + artist: "The Weeknd", + year: 2015, + month: 10, + day: 3, + weeksAtNumberOne: 6, + duration: "4:04" + }, + { + name: "Hello", + artist: "Adele", + year: 2015, + month: 11, + day: 14, + weeksAtNumberOne: 10, + duration: "4:55" + } +]; + +//forEach +//1.Write a function called listNames which takes in an array of songs and console.logs the name of each one. +function listNames(songarr) { + songarr.forEach(element => { + console.log(element.name); + }); +} + +listNames(songs); + +//2.Write a function called listSongDetails which takes in an array of songs and console.logs details about each one. The details should be in the following example format: "Smooth, by Santana featuring Rob Thomas (4:00)". + +function listSongDetails(songDetailArr) { + songDetailArr.forEach(element => { + console.log( + element.name + " by " + element.artist, + "(" + element.duration + ")" + ); + }); +} + +listSongDetails(songs); + +//3.Write a function called summerJamCount which takes in an array of songs and returns the number of songs which hit #1 on the charts in June, July, or August. + +function summerJamCount(songarr) { + var count = 0; + songarr.forEach(element => { + if (element.month >= 6 && element.month <= 8) { + count++; + } + }); + console.log(count++); + return count; +} + +summerJamCount(songs); diff --git a/Iterators-forEach, Map, and Filter/index.html b/Iterators-forEach, Map, and Filter/index.html index a6bc379..55377e4 100644 --- a/Iterators-forEach, Map, and Filter/index.html +++ b/Iterators-forEach, Map, and Filter/index.html @@ -9,6 +9,7 @@ - + + From ddc51e97133317fb6454663a9bfaa05cd86e0b0d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 7 Jul 2019 08:01:17 +0530 Subject: [PATCH 129/161] map exercise started --- .../Iterators-exercise-two.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js index 0356d3f..c8feee9 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js +++ b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js @@ -1829,3 +1829,32 @@ function summerJamCount(songarr) { } summerJamCount(songs); + +//map + +//Write a function called getDurations which takes in an array of songs and returns an array of each song's duration. + +function getDurations(songarr) { + var songinDur = songarr.map(element => { + return element.duration; + }); + console.log(songinDur); +} + +getDurations(songs); + +//Write a function called getDurationInSeconds which takes in an array of songs and returns an array of each song's duration in seconds. + +function getDurationInSeconds(songarr) { + var songinSec = songarr.map(element => { + var duration = element.duration.split(":"); + var min = +duration[0]; + var secs = +duration[1]; + return 60 * min + secs; + }); + console.log(songinSec); +} + +getDurationInSeconds(songs); + +//Write a function called getMainArtists which takes in an array of songs and returns an array of the primary artists on the recordings. If there's only one artist, that artist should be returned; if there are featured artists, they should be ignored (so only the artist to the left of "featuring" is kept.) From 1fcf29722ab74549048a270cbaccc8b88b5f06fb Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 7 Jul 2019 08:20:23 +0530 Subject: [PATCH 130/161] Completed map() related exercises --- .../Iterators-exercise-two.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js index c8feee9..6281024 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js +++ b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js @@ -1858,3 +1858,16 @@ function getDurationInSeconds(songarr) { getDurationInSeconds(songs); //Write a function called getMainArtists which takes in an array of songs and returns an array of the primary artists on the recordings. If there's only one artist, that artist should be returned; if there are featured artists, they should be ignored (so only the artist to the left of "featuring" is kept.) + +function getMainArtists(songArr) { + var songMainartist = songArr.map(songs => { + var splitString = songs.artist.split("featuring"); + return splitString[0].trim(); + }); + console.log(songMainartist); +} + +getMainArtists(songs); + +//Filter +// From cf80505503be180150152c1ee296e9838a14f6cc Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 7 Jul 2019 12:36:09 +0530 Subject: [PATCH 131/161] All the iterators exercise done --- .../Iterators-exercise-two.js | 104 +++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js index 6281024..ca43c23 100644 --- a/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js +++ b/Iterators-forEach, Map, and Filter/Iterators-exercise-two.js @@ -1852,7 +1852,7 @@ function getDurationInSeconds(songarr) { var secs = +duration[1]; return 60 * min + secs; }); - console.log(songinSec); + console.log("songinSec", songinSec); } getDurationInSeconds(songs); @@ -1870,4 +1870,104 @@ function getMainArtists(songArr) { getMainArtists(songs); //Filter -// +//Write a function called getBigHits which takes an array of songs and returns an array of songs which were number one for 10 or more weeks. + +function getBigHits(songArr) { + var bigHits = songArr.filter(songs => { + return songs.weeksAtNumberOne >= 10; + }); + console.log(bigHits); +} + +getBigHits(songs); + +//Write a function called getShortSongs which takes an array of songs and returns an array of songs which are shorter than 3 minutes. + +function getShortSongs(songArr) { + // var songInmins = songArr.duration.split(":"); + var songLessThanThree = songArr.filter(songs => { + //here because of coercion the song.duration[0] which is a string is coerced into number; + return songs.duration[0] < 3; + }); + console.log(songLessThanThree); +} + +getShortSongs(songs); + +//Write a function called getSongsByArtist which takes in an array of songs and the name of an artist and returns an array of songs by that artist. + +function getSongsByArtist(songArr, artistname) { + var songByArtist = songArr.filter(songs => { + return songs.artist === artistname; + }); + console.log(songByArtist); +} + +getSongsByArtist(songs, "Aaliyah"); + +function summerJamCountReduce(songarr) { + var reduceRefactor = songarr.reduce(function(acc, nextVal) { + if (nextVal.month >= 6 && nextVal.month <= 8) { + acc++; + } + return acc; + }, 0); + console.log(reduceRefactor); +} + +summerJamCountReduce(songs); + +//Write a function called getTotalDurationInSeconds which takes in an array of songs and returns the total amount of time (in seconds) it would take to listen to all of the songs. (Hint: can you use anything you've written already to help solve this problem?) + +function getTotalDurationInSeconds(songArr) { + var totalDuration = songArr.reduce((acc, nextVal) => { + var timeSplit = nextVal.duration.split(":"); + var mins = +timeSplit[0]; + var secs = +timeSplit[1]; + var durationToSecs = 60 * mins + secs; + return durationToSecs + acc; + }, 0); + console.log(totalDuration); + return totalDuration; +} + +getTotalDurationInSeconds(songs); + +//Write a function called getSongCountByArtist which takes in an array of songs and returns an object. The keys in the object should be artist names, and the values should be the number of songs by that artist in the orignal array. + +function getSongCountByArtist(arr) { + var answer = arr.reduce((acc, nextVal) => { + acc[nextVal.artist] = ++acc[nextVal.artist] || 1; + return acc; + }, {}); + console.log(answer); +} + +getSongCountByArtist(songs); + +// function getSongCountByArtist(songArr) { +// var answer = songArr.reduce(function(obj, nextSong) { +// var artist = nextSong.artist; +// if (obj[artist]) { +// obj[artist]++; +// } else { +// obj[artist] = 1; +// } +// return obj; +// }, {}); +// console.log(answer); +// } + +// getSongCountByArtist(songs); + +//Write a function called averageWeeksAtNumberOne which takes in an array of songs and returns the average number of weeks that songs on the list were #1. + +function averageWeeksAtNumberOne(arr) { + var averageWeek = + arr.reduce((acc, nextVal) => { + return acc + nextVal.weeksAtNumberOne / 2; + }, 0) / arr.length; + console.log(averageWeek); +} + +averageWeeksAtNumberOne(songs); From c30721115253ce0357c736f39a5b76ad00a9b94d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 21 Jul 2019 08:02:50 +0530 Subject: [PATCH 132/161] Testing using Jasmine --- Testing-with-Jasmine/index.html | 20 ++++++++++++++++++++ Testing-with-Jasmine/test-jasmine.js | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Testing-with-Jasmine/index.html create mode 100644 Testing-with-Jasmine/test-jasmine.js diff --git a/Testing-with-Jasmine/index.html b/Testing-with-Jasmine/index.html new file mode 100644 index 0000000..6aa6d39 --- /dev/null +++ b/Testing-with-Jasmine/index.html @@ -0,0 +1,20 @@ + + + + + + + Codestin Search App + + + +
    + + + + + + diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js new file mode 100644 index 0000000..2217456 --- /dev/null +++ b/Testing-with-Jasmine/test-jasmine.js @@ -0,0 +1,17 @@ +var earth = { + isround: true, + positionFromSun: 3, + density: 5.5 +}; + +describe("Earth", function() { + it("is round", function() { + expect(earth.isround).toEqual(true); + }); + it("third from Sun", function() { + expect(earth.positionFromSun).toEqual(3); + }); + it("density", function() { + expect(earth.density).toEqual(5.5); + }); +}); From 8effaedfac25c0a3ed53245bed07ab0b9dced9b1 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 23 Jul 2019 00:09:57 +0530 Subject: [PATCH 133/161] testing code with jasmine exercise-writing the code --- Testing-with-Jasmine/index.html | 8 ++-- Testing-with-Jasmine/test-jasmine.js | 57 ++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Testing-with-Jasmine/index.html b/Testing-with-Jasmine/index.html index 6aa6d39..c339a8f 100644 --- a/Testing-with-Jasmine/index.html +++ b/Testing-with-Jasmine/index.html @@ -5,16 +5,16 @@ Codestin Search App - + /> -->
    - + diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 2217456..730d084 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -1,17 +1,42 @@ -var earth = { - isround: true, - positionFromSun: 3, - density: 5.5 -}; +// var earth = { +// isround: true, +// positionFromSun: 3, +// density: 5.5 +// }; -describe("Earth", function() { - it("is round", function() { - expect(earth.isround).toEqual(true); - }); - it("third from Sun", function() { - expect(earth.positionFromSun).toEqual(3); - }); - it("density", function() { - expect(earth.density).toEqual(5.5); - }); -}); +// describe("Earth", function() { +// it("is round", function() { +// expect(earth.isround).toEqual(true); +// }); +// it("third from Sun", function() { +// expect(earth.positionFromSun).toEqual(3); +// }); +// it("density", function() { +// expect(earth.density).toEqual(5.5); +// }); +// }); + +//Testing exercise One + +//Write a function called replaceWith that takes in a string, a character to replace and a character to replace it with and returns the string with the replacements. Write tests to make sure this is case sensitive + +// describe("replacewith", function() { +// it("is case sensitive", function() { +// expect(replaceWith("awesome", "e", "b")).toEqual("awesome", "e", "b"); +// }); +// }); + +function replaceWith(string, displacedChar, newChar) { + var splitString = string.split(""); + // console.log(splitString); + let chars = ""; + for (let i = 0; i < splitString.length; i++) { + if (splitString[i] === displacedChar) { + chars += splitString.splice(0, i, newChar); + } + } + console.log(chars); + console.log(splitString); +} + +replaceWith("awesome", "e", "b"); From 5b6494d1d675b897d3f8b46e5ba8d55bf41154e0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 23 Jul 2019 07:52:28 +0530 Subject: [PATCH 134/161] code done testing pending --- Testing-with-Jasmine/index.html | 8 ++++---- Testing-with-Jasmine/test-jasmine.js | 19 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Testing-with-Jasmine/index.html b/Testing-with-Jasmine/index.html index c339a8f..6aa6d39 100644 --- a/Testing-with-Jasmine/index.html +++ b/Testing-with-Jasmine/index.html @@ -5,16 +5,16 @@ Codestin Search App - + />
    - + diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 730d084..20edbb5 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -20,23 +20,20 @@ //Write a function called replaceWith that takes in a string, a character to replace and a character to replace it with and returns the string with the replacements. Write tests to make sure this is case sensitive -// describe("replacewith", function() { -// it("is case sensitive", function() { -// expect(replaceWith("awesome", "e", "b")).toEqual("awesome", "e", "b"); -// }); -// }); +replaceWith("awesome", "e", "B"); + +describe("replacewith", function() { + it("is case sensitive", function() { + expect(replaceWith("awesome", "e", "b")).toEqual("awesome", "e", "B"); + }); +}); function replaceWith(string, displacedChar, newChar) { var splitString = string.split(""); - // console.log(splitString); - let chars = ""; for (let i = 0; i < splitString.length; i++) { if (splitString[i] === displacedChar) { - chars += splitString.splice(0, i, newChar); + splitString[i] = newChar; } } - console.log(chars); console.log(splitString); } - -replaceWith("awesome", "e", "b"); From e969fa455b07da206ef574781d9bd7f486c80bd2 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 24 Jul 2019 23:08:51 +0530 Subject: [PATCH 135/161] Completed first exercise in Jasmine sucessfully --- Testing-with-Jasmine/test-jasmine.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 20edbb5..69d954d 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -20,11 +20,9 @@ //Write a function called replaceWith that takes in a string, a character to replace and a character to replace it with and returns the string with the replacements. Write tests to make sure this is case sensitive -replaceWith("awesome", "e", "B"); - describe("replacewith", function() { it("is case sensitive", function() { - expect(replaceWith("awesome", "e", "b")).toEqual("awesome", "e", "B"); + expect(replaceWith("Awesome", "e", "b")).toBe("awbsomb"); }); }); @@ -35,5 +33,6 @@ function replaceWith(string, displacedChar, newChar) { splitString[i] = newChar; } } - console.log(splitString); + var newString = splitString.join(""); + return newString; } From b39f3db0ecbf6593559cd2cd086d7a23ad9a859f Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 25 Jul 2019 08:06:22 +0530 Subject: [PATCH 136/161] Started with new jasmine exercise --- Testing-with-Jasmine/test-jasmine.js | 38 ++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 69d954d..9f3d046 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -20,19 +20,35 @@ //Write a function called replaceWith that takes in a string, a character to replace and a character to replace it with and returns the string with the replacements. Write tests to make sure this is case sensitive -describe("replacewith", function() { - it("is case sensitive", function() { - expect(replaceWith("Awesome", "e", "b")).toBe("awbsomb"); +// describe("replacewith", function() { +// it("is case sensitive", function() { +// expect(replaceWith("for Me", "e", "b")).toBe("for Mb"); +// }); +// }); + +// function replaceWith(string, displacedChar, newChar) { +// var splitString = string.split(""); +// for (let i = 0; i < splitString.length; i++) { +// if (splitString[i] === displacedChar) { +// splitString[i] = newChar; +// } +// } +// var newString = splitString.join(""); +// return newString; +// } + +//Write a function called expand which takes an array and a number and returns a copy of the array with as many numbers as specified + +describe("expand", function() { + it("returns any array", function() { + expect(expand([1, 2, 3], 3)).toBe(jasmine.any(Array)); }); }); -function replaceWith(string, displacedChar, newChar) { - var splitString = string.split(""); - for (let i = 0; i < splitString.length; i++) { - if (splitString[i] === displacedChar) { - splitString[i] = newChar; - } +function expand(arr, repeatArr) { + var newArr = []; + for (let i = 0; i < repeatArr; i++) { + newArr.push(arr.slice(0, arr.length)); } - var newString = splitString.join(""); - return newString; + console.log(newArr); } From 29425b15be36a9c21b36196a1a5798ca4a52a050 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 29 Jul 2019 23:04:20 +0530 Subject: [PATCH 137/161] Finally completed the second Jasmine related exercise --- Testing-with-Jasmine/test-jasmine.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 9f3d046..423515b 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -41,7 +41,7 @@ describe("expand", function() { it("returns any array", function() { - expect(expand([1, 2, 3], 3)).toBe(jasmine.any(Array)); + expect(expand(["fruits", "vegetables"], 4)).toEqual(jasmine.any(Array)); }); }); @@ -50,5 +50,10 @@ function expand(arr, repeatArr) { for (let i = 0; i < repeatArr; i++) { newArr.push(arr.slice(0, arr.length)); } - console.log(newArr); + newCompleteArr = [].concat.apply([], newArr); + return newCompleteArr; } + +//Write a function called acceptNumbersOnly which takes in any number of arguments and returns true if all of them are numbers. Watch out for NaN - it is a typeof "number"! + +// function acceptNumbersOnly() From 414d92dd712bf4288c0a61368c1051a587042007 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 30 Jul 2019 23:57:10 +0530 Subject: [PATCH 138/161] Started Jasmine second exercise --- Testing-with-Jasmine/test-jasmine.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 423515b..6312719 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -56,4 +56,15 @@ function expand(arr, repeatArr) { //Write a function called acceptNumbersOnly which takes in any number of arguments and returns true if all of them are numbers. Watch out for NaN - it is a typeof "number"! -// function acceptNumbersOnly() +describe("acceptNumbersOnly", function() { + it("Allows only numbers", function() { + expect(acceptNumbersOnly(1, 223, 45)).toEqual(jasmine.any(Number)); + }); +}); + +function acceptNumbersOnly() { + for (let i = 0; i < arguments.length; i++) { + console.log(arguments[i]); + return typeof arguments[i] === Number; + } +} From ebe69c6b53b4629e75db480d4c9a4683906bd925 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 1 Aug 2019 22:36:16 +0530 Subject: [PATCH 139/161] second exercise in jasmine testing complete --- Testing-with-Jasmine/test-jasmine.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 6312719..866867b 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -58,13 +58,13 @@ function expand(arr, repeatArr) { describe("acceptNumbersOnly", function() { it("Allows only numbers", function() { - expect(acceptNumbersOnly(1, 223, 45)).toEqual(jasmine.any(Number)); + expect(acceptNumbersOnly(1, 223, 45)).toBeTruthy(); }); }); function acceptNumbersOnly() { for (let i = 0; i < arguments.length; i++) { - console.log(arguments[i]); - return typeof arguments[i] === Number; + console.log(typeof arguments[i] === "number"); + return typeof arguments[i] === "number"; } } From ca4758a5fca51e5e05d76f6020a82f90abaa952c Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 12 Aug 2019 23:59:12 +0530 Subject: [PATCH 140/161] Testing for number still searching for solution --- Testing-with-Jasmine/test-jasmine.js | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index 866867b..b0e1d65 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -39,32 +39,34 @@ //Write a function called expand which takes an array and a number and returns a copy of the array with as many numbers as specified -describe("expand", function() { - it("returns any array", function() { - expect(expand(["fruits", "vegetables"], 4)).toEqual(jasmine.any(Array)); - }); -}); +// describe("expand", function() { +// it("returns any array", function() { +// expect(expand(["fruits", "vegetables"], 4)).toEqual(jasmine.any(Array)); +// }); +// }); -function expand(arr, repeatArr) { - var newArr = []; - for (let i = 0; i < repeatArr; i++) { - newArr.push(arr.slice(0, arr.length)); - } - newCompleteArr = [].concat.apply([], newArr); - return newCompleteArr; -} +// function expand(arr, repeatArr) { +// var newArr = []; +// for (let i = 0; i < repeatArr; i++) { +// newArr.push(arr.slice(0, arr.length)); +// } +// newCompleteArr = [].concat.apply([], newArr); +// return newCompleteArr; +// } //Write a function called acceptNumbersOnly which takes in any number of arguments and returns true if all of them are numbers. Watch out for NaN - it is a typeof "number"! describe("acceptNumbersOnly", function() { it("Allows only numbers", function() { - expect(acceptNumbersOnly(1, 223, 45)).toBeTruthy(); + expect(acceptNumbersOnly(1, "7", 45, 5)).toBe(true); }); }); function acceptNumbersOnly() { for (let i = 0; i < arguments.length; i++) { - console.log(typeof arguments[i] === "number"); - return typeof arguments[i] === "number"; + console.log(arguments[i]); + if (typeof arguments[i] === "number") { + return true; + } } } From 06104bd4e73cf7db7d3e39c4c4c06e19f1d43680 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 21 Aug 2019 07:14:00 +0530 Subject: [PATCH 141/161] Jasmine exercise completed --- Testing-with-Jasmine/test-jasmine.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Testing-with-Jasmine/test-jasmine.js b/Testing-with-Jasmine/test-jasmine.js index b0e1d65..fac73f4 100644 --- a/Testing-with-Jasmine/test-jasmine.js +++ b/Testing-with-Jasmine/test-jasmine.js @@ -58,14 +58,16 @@ describe("acceptNumbersOnly", function() { it("Allows only numbers", function() { - expect(acceptNumbersOnly(1, "7", 45, 5)).toBe(true); + expect(acceptNumbersOnly(1, "5", 45, 5)).toBe(true); }); }); function acceptNumbersOnly() { - for (let i = 0; i < arguments.length; i++) { - console.log(arguments[i]); - if (typeof arguments[i] === "number") { + var args = Array.from(arguments); + console.log(args); + for (let i = 0; i < args.length; i++) { + console.log(args[i]); + if (typeof args[i] === "number") { return true; } } From cddd4b2b92f3c8221efeecb4ed50056ecc92f033 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 21 Aug 2019 07:50:26 +0530 Subject: [PATCH 142/161] This keyword notes in js --- this keyowrd/this-notes.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 this keyowrd/this-notes.md diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md new file mode 100644 index 0000000..cadddc5 --- /dev/null +++ b/this keyowrd/this-notes.md @@ -0,0 +1,11 @@ +Overview of this keyword in js + +Different kinds of binding + +Default-this will always point to window object +Implicit-this will take the object within which it is declared as its parent +Explicit- this explicitly setting the value for 'this' using call, bind and apply + +There is something called new used in constructor function. + +Need to expand these notes From 97b8c2776325fd67a810eece98184b6c12f146bf Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 27 Aug 2019 07:49:51 +0530 Subject: [PATCH 143/161] This realated notes expanded --- this keyowrd/this-notes.md | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index cadddc5..c068444 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -1,11 +1,28 @@ -Overview of this keyword in js +Overview of 'this' keyword in JS Different kinds of binding -Default-this will always point to window object -Implicit-this will take the object within which it is declared as its parent -Explicit- this explicitly setting the value for 'this' using call, bind and apply +Default-this will always point to window object. +Implicit-this will take the closest object within which it is declared as its parent. -There is something called new used in constructor function. +Explicit- this is explicitly setting the value for 'this' using functions call, bind and apply. This allows us to set the value of this whatever we want it to be. + +call(): takes in two parameters thisArg that is what should the this and n number of other arguments. It invokes the function immedialtely. +Eg: + +
    + var animal = {
    +	introduce:function(){
    +		return this.name+" is a "+this.type+" and says " + this.sound + "!";
    +	}
    +}
     
    -Need to expand these notes
    +var whisky = {
    +	name:"Whiskey",
    +	type:"dog",
    +	sound:"woof"
    +}
    +animal.introduce.call(whisky);
    +
    + +There is something called new used in constructor function. From 074dac1518bf43e8e759df1beba853e87427bd14 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 28 Aug 2019 07:12:55 +0530 Subject: [PATCH 144/161] Notes about call() --- this keyowrd/this-notes.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index c068444..0d35565 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -25,4 +25,32 @@ var whisky = { animal.introduce.call(whisky); +With call() we are changing the parent of 'this' to whisky the object. + +here's another example of changing 'this' parent to another object + +
    +
    +var animal = {
    +name:"Tiger",
    +type:"wild",
    +sound:"roar",
    +introduce:function(){
    +	return this.name + " is a " + this.type + " and says " + this.sound + "!";
    +}
    +}
    +
    +animal.introduce();
    +"Tiger is a wild and says roar!"
    +var alpha = {
    + name:"Alpha",
    +type:"dog",
    +sound:"woof"
    +}
    +
    +animal.introduce.call(alpha);
    +"Alpha is a dog and says woof!"
    +
    +
    + There is something called new used in constructor function. From 58452eba2df693f7754f3a256f3fef8ce3387ee0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 28 Aug 2019 07:37:28 +0530 Subject: [PATCH 145/161] more notes on call() --- this keyowrd/this-notes.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index 0d35565..40ffd89 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -53,4 +53,31 @@ animal.introduce.call(alpha); -There is something called new used in constructor function. +One of the most common use cases for call is to convert an array-like object into an actual array. + +Normally this what is returned from arguments an array like object +and any normal array related methods doesnt work on it and throws up an error + +
    +function sumArguments(){
    +	return arguments;
    +}
    +
    +sumArguments(5,8,9);
    +
    +Result:
    +Arguments(3) [5, 8, 9, callee: ƒ, Symbol(Symbol.iterator): ƒ]
    +
    + +Here we can use the slice method along with call() to convert array like objects to normal array. + +
    +function sumArgumentsCall(){
    +	return [].slice.call(arguments);
    +}
    +
    +sumArgumentsCall(4,5,6);
    +
    +Result:
    +[4, 5, 6]
    +
    From 01c49dc96014cad184618096aeec049677292ea2 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 29 Aug 2019 07:56:02 +0530 Subject: [PATCH 146/161] Plying around with call and apply methods --- this keyowrd/README.md | 3 +++ this keyowrd/index.html | 18 ++++++++++++++++++ this keyowrd/script.js | 18 ++++++++++++++++++ this keyowrd/style.css | 26 ++++++++++++++++++++++++++ this keyowrd/this-notes.md | 6 ++++-- 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 this keyowrd/README.md create mode 100644 this keyowrd/index.html create mode 100644 this keyowrd/script.js create mode 100644 this keyowrd/style.css diff --git a/this keyowrd/README.md b/this keyowrd/README.md new file mode 100644 index 0000000..8eade75 --- /dev/null +++ b/this keyowrd/README.md @@ -0,0 +1,3 @@ +# Object Related exercises from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/this keyowrd/index.html b/this keyowrd/index.html new file mode 100644 index 0000000..d6ddd5e --- /dev/null +++ b/this keyowrd/index.html @@ -0,0 +1,18 @@ + + + + + + + + + Codestin Search App + + + + + + diff --git a/this keyowrd/script.js b/this keyowrd/script.js new file mode 100644 index 0000000..f1b1e0f --- /dev/null +++ b/this keyowrd/script.js @@ -0,0 +1,18 @@ +var colt = { + firstName: "Colt", + sayHi: function() { + return "Hi " + this.firstName; + }, + addNumber: function(a, b, c, d) { + return this.firstName + " just added " + (a + b + c + d); + } +}; + +var sam = { + firstName: "Sam" +}; + +console.log(colt.sayHi.call(sam)); +console.log(colt.addNumber.call(sam, 1, 2, 3, 4)); +console.log(colt.addNumber.apply(sam, [1, 2, 3, 4])); //Apply parameters have to be in array +//that's the difference between call and apply diff --git a/this keyowrd/style.css b/this keyowrd/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/this keyowrd/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index 40ffd89..d628526 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -7,7 +7,7 @@ Implicit-this will take the closest object within which it is declared as its pa Explicit- this is explicitly setting the value for 'this' using functions call, bind and apply. This allows us to set the value of this whatever we want it to be. -call(): takes in two parameters thisArg that is what should the this and n number of other arguments. It invokes the function immedialtely. +call(): takes in two parameters thisArg that is what should the this be and n number of other arguments. It invokes the function immedialtely. Eg:
    @@ -55,7 +55,7 @@ animal.introduce.call(alpha);
     
     One of the most common use cases for call is to convert an array-like object into an actual array.
     
    -Normally this what is returned from arguments an array like object
    +Normally this is what returned from arguments an array like object
     and any normal array related methods doesnt work on it and throws up an error
     
     
    @@ -66,6 +66,7 @@ function sumArguments(){
     sumArguments(5,8,9);
     
     Result:
    +--------------------------
     Arguments(3) [5, 8, 9, callee: ƒ, Symbol(Symbol.iterator): ƒ]
     
    @@ -79,5 +80,6 @@ function sumArgumentsCall(){ sumArgumentsCall(4,5,6); Result: +-------------------------- [4, 5, 6]
    From 271c28428d399ba8fd1a4d55cb29f0f5393d3ba0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 1 Sep 2019 19:56:27 +0530 Subject: [PATCH 147/161] Learned morebaout call, apply and bind. Added new notes --- this keyowrd/index.html | 1 - this keyowrd/script.js | 57 ++++++++++++++++++++++++++++++++++++-- this keyowrd/this-notes.md | 57 +++++++++++++++++++++++++++++++++++--- 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/this keyowrd/index.html b/this keyowrd/index.html index d6ddd5e..b2c4ff1 100644 --- a/this keyowrd/index.html +++ b/this keyowrd/index.html @@ -13,6 +13,5 @@ - diff --git a/this keyowrd/script.js b/this keyowrd/script.js index f1b1e0f..69204cb 100644 --- a/this keyowrd/script.js +++ b/this keyowrd/script.js @@ -12,7 +12,58 @@ var sam = { firstName: "Sam" }; -console.log(colt.sayHi.call(sam)); -console.log(colt.addNumber.call(sam, 1, 2, 3, 4)); -console.log(colt.addNumber.apply(sam, [1, 2, 3, 4])); //Apply parameters have to be in array +// console.log(colt.sayHi.call(sam)); +// console.log(colt.addNumber.call(sam, 1, 2, 3, 4)); +// console.log(colt.addNumber.apply(sam, [1, 2, 3, 4])); //Apply parameters have to be in array //that's the difference between call and apply + +//Bind +function add(a, b) { + return a + b; +} + +var partialAdd = add.bind(this, 2); +console.log(add.bind(this, 2)); +console.log(partialAdd(4)); + +function outerFunc(val) { + var a = val; + return function innerFunction(b) { + return a + b; + }; +} + +var add = outerFunc(4); +console.log(add(10)); + +//default binding +//here 'this' is the window object +//since function is within global scope or within window object + +function showDimensions() { + console.log(window.innerHeight); + var width = this.innerWidth; + var height = this.innerHeight; + return [width, height]; +} + +console.log(showDimensions()); + +//borrowing function +//here call is used to borrow the method from person object and use for secondPerson obj + +var person = { + firstName: "Sam", + secondName: "Cherian", + getFullName: function() { + return this.firstName + " " + this.secondName; + } +}; + +var secondPerson = { + firstName: "Jane", + secondName: "Doe" +}; + +console.log(person.getFullName()); +console.log(person.getFullName.call(secondPerson)); diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index d628526..d0298ca 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -5,9 +5,12 @@ Different kinds of binding Default-this will always point to window object. Implicit-this will take the closest object within which it is declared as its parent. -Explicit- this is explicitly setting the value for 'this' using functions call, bind and apply. This allows us to set the value of this whatever we want it to be. +Explicit- this is explicitly setting the value for 'this' using functions call, bind and apply. This allows us to set the value of this whatever we want it to be. These are methods on the function object. + +

    call()

    : + +Takes in two parameters thisArg that is what should the this be and n number of other arguments. It invokes the function immedialtely. -call(): takes in two parameters thisArg that is what should the this be and n number of other arguments. It invokes the function immedialtely. Eg:
    @@ -25,7 +28,7 @@ var whisky = {
     animal.introduce.call(whisky);
     
    -With call() we are changing the parent of 'this' to whisky the object. +With call() we are changing the parent of 'this' to whisky the object or other words we are borrowing the introudce method from animal object and using it with the object whisky. here's another example of changing 'this' parent to another object @@ -39,9 +42,10 @@ introduce:function(){ return this.name + " is a " + this.type + " and says " + this.sound + "!"; } } - +result: animal.introduce(); "Tiger is a wild and says roar!" + var alpha = { name:"Alpha", type:"dog", @@ -49,6 +53,7 @@ sound:"woof" } animal.introduce.call(alpha); +result: "Alpha is a dog and says woof!" @@ -83,3 +88,47 @@ sumArgumentsCall(4,5,6); -------------------------- [4, 5, 6] + +

    apply()

    : +Apply also does the same functionality as call, difference is it takes the second argument as array of parameters + +syntax:fn.apply(thisArg,[arg1,arg2,arg3]) + +One way of using apply and call method is to borrow method from one object to be used by another obj. +var person = { +firstName: "Sam", +secondName: "Cherian", +getFullName: function() { +return this.firstName + " " + this.secondName; +} +}; + +var secondPerson = { +firstName: "Jane", +secondName: "Doe" +}; + +console.log(person.getFullName()); +console.log(person.getFullName.call(secondPerson)); + +

    bind()

    : + +Bind is also used to change the context of this but function doesnt get invoked as soon as its applied instead returns the copy of the function on which it was applied. + +one instance of using bind, Function Currying + +
    +function multiply(a,b){
    +	return a*b;
    +}
    +
    +var muliplyByTwo = multiply.bind(this,2);
    +muliplyByTwo(4);
    +muliplyByTwo(5);
    +muliplyByTwo(3);
    +muliplyByTwo(2);
    +
    +
    +
    + +In the above example we giving a permanent paramater of two to the function using bind, this is called function currying where we give a preset parameter to the function. From 02aa1c68c4aab4120e83abdc7b646f3f245fd7e0 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 1 Sep 2019 22:34:43 +0530 Subject: [PATCH 148/161] Added notes on 'new' for determining the context of this --- this keyowrd/script.js | 9 +++++++++ this keyowrd/this-notes.md | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/this keyowrd/script.js b/this keyowrd/script.js index 69204cb..8f81311 100644 --- a/this keyowrd/script.js +++ b/this keyowrd/script.js @@ -67,3 +67,12 @@ var secondPerson = { console.log(person.getFullName()); console.log(person.getFullName.call(secondPerson)); + +//changing context of this using new in Object constructor +function Person(fName, lName) { + this.fName = fName; + this.lName = lName; +} + +var personOne = new Person("Sam", "Cherian"); +console.log(personOne); diff --git a/this keyowrd/this-notes.md b/this keyowrd/this-notes.md index d0298ca..ee9d015 100644 --- a/this keyowrd/this-notes.md +++ b/this keyowrd/this-notes.md @@ -131,4 +131,20 @@ muliplyByTwo(2); -In the above example we giving a permanent paramater of two to the function using bind, this is called function currying where we give a preset parameter to the function. +In the above example we are giving a permanent paramater of two to the function using bind, this is called function currying where we give a preset parameter to the function. + +Using new in Object constructor also changes the context of 'this'. + +Example: + +
    +
    +//changing context of this using new in Object constructor
    +function Person(fName, lName) {
    +  this.fName = fName;
    +  this.lName = lName;
    +}
    +
    +var personOne = new Person("Sam", "Cherian");
    +console.log(personOne);
    +
    From a2a5d1aaf1815627df1974aea1bae7d8cdeca6f5 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Mon, 2 Sep 2019 22:59:22 +0530 Subject: [PATCH 149/161] Completed part one of the exercise --- this keyowrd/exercises.js | 75 +++++++++++++++++++++++++++++++++++++++ this keyowrd/index.html | 3 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 this keyowrd/exercises.js diff --git a/this keyowrd/exercises.js b/this keyowrd/exercises.js new file mode 100644 index 0000000..1617265 --- /dev/null +++ b/this keyowrd/exercises.js @@ -0,0 +1,75 @@ +//What is the value of the keyword this in the following example: +var data = this; +console.log(data); +//Answer: window object + +//What does this function output? Why? +function logThis() { + return this; +} + +console.log(logThis()); //function will output window object, because the function is in global scope and this will always point to the window object + +//What does this function output? Why? +var instructor = { + firstName: "Tim", + sayHi: function() { + console.log("Hello! " + this.firstName); + } +}; + +console.log(instructor.sayHi()); // My answer: this will outpur "Hello! Tim", because here this context is the instructor object +//Actually answer it will outpur both "Hello! Tim" since its being consoled and undefined since the sayHi function is not returning anything. + +//What does this function output? Why? +var instructor = { + firstName: "Tim", + info: { + catOwner: true, + boatOwner: true + }, + displayInfo: function() { + console.log(this); + return "Cat owner? " + this.catOwner; + } +}; + +console.log(instructor.displayInfo()); //I thought it will throw a refrence error the the obj instructor doesnt have +//the key catOwner but instead the it returned undefined + +//What does this function output? Why? +var instructor = { + firstName: "Tim", + info: { + catOwner: true, + boatOwner: true, + displayLocation: function() { + console.log(this); + return this.data.location; + }, + data: { + location: "Oakland" + } + } +}; + +instructor.info.displayLocation(); // this will return Oakland because this here refers to info object. + +//What does this function output? Why? +var instructor = { + firstName: "Tim", + info: { + catOwner: true, + boatOwner: true, + displayLocation: function() { + return this.location; + }, + data: { + location: "Oakland", + logLocation: this.displayLocation + } + } +}; + +console.log(instructor.info.data.logLocation()); // Why might we be getting an error here? +//we are trying to call a method logLocation on data but it doesnt exist in data. diff --git a/this keyowrd/index.html b/this keyowrd/index.html index b2c4ff1..229e81b 100644 --- a/this keyowrd/index.html +++ b/this keyowrd/index.html @@ -12,6 +12,7 @@ Codestin Search App - + + From 1ffb49a919eb0e98e69e95259ec0a5fceb384c34 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 4 Sep 2019 23:26:00 +0530 Subject: [PATCH 150/161] continuing working on this related exercises --- this keyowrd/exercises.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/this keyowrd/exercises.js b/this keyowrd/exercises.js index 1617265..ae601db 100644 --- a/this keyowrd/exercises.js +++ b/this keyowrd/exercises.js @@ -71,5 +71,29 @@ var instructor = { } }; -console.log(instructor.info.data.logLocation()); // Why might we be getting an error here? +// console.log(instructor.info.data.logLocation()); // Why might we be getting an error here? //we are trying to call a method logLocation on data but it doesnt exist in data. + +//Fix the following code: +var obj = { + fullName: "Harry Potter", + person: { + sayHi: function() { + return "This person's name is " + this.fullName; + } + } +}; +console.log(obj.person.sayHi.call(obj)); + +function sumEvenArguments() { + var count = 0; + for (let i = 0; i < arguments.length; i++) { + if (arguments[i] % 2 === 0) { + count += arguments[i]; + } + } + console.log(count); + return count; +} + +sumEvenArguments(1, 2); From bcf8c7005ecb0584f0e97dd7839f3d26a7b20199 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 5 Sep 2019 07:56:11 +0530 Subject: [PATCH 151/161] did exercise on converting array like objects to array suing call() --- this keyowrd/exercises.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/this keyowrd/exercises.js b/this keyowrd/exercises.js index ae601db..ea57c3b 100644 --- a/this keyowrd/exercises.js +++ b/this keyowrd/exercises.js @@ -96,4 +96,20 @@ function sumEvenArguments() { return count; } -sumEvenArguments(1, 2); +// sumEvenArguments(1, 2); + +//converting an array like objects to array using call on slice method +function arrayFrom(args) { + return [].slice.call(args); +} + +function sample() { + var arr = arrayFrom(arguments); + console.log(arr); + if (!arr.reduce) throw "This is not an array!"; + return arr.reduce(function(acc, next) { + return acc + next; + }, 0); +} + +console.log(sample(2, 3, 5)); From a7a5dce846dbf63e419d9f9e9667e74b89530ac7 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 8 Sep 2019 08:39:37 +0530 Subject: [PATCH 152/161] Done with exercise invokeMax --- this keyowrd/exercises.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/this keyowrd/exercises.js b/this keyowrd/exercises.js index ea57c3b..f8bc15d 100644 --- a/this keyowrd/exercises.js +++ b/this keyowrd/exercises.js @@ -112,4 +112,31 @@ function sample() { }, 0); } -console.log(sample(2, 3, 5)); +// console.log(sample(2, 3, 5)); + +function add(a, b) { + return a + b; +} +function multiply(a, b) { + return a * b; +} + +function invokeMax(fn, maxNum) { + let limit = maxNum; + let functionPassed = fn; + let counter = 0; + return function innerFunc(a, b) { + counter++; + if (counter > limit) { + return "Maxed out"; + } + return functionPassed(a, b); + }; +} + +var addOnlyThreeTimes = invokeMax(multiply, 4); +console.log(addOnlyThreeTimes); +console.log(addOnlyThreeTimes(1, 2)); +console.log(addOnlyThreeTimes(2, 2)); +console.log(addOnlyThreeTimes(2, 8)); +console.log(addOnlyThreeTimes(5, 6)); From aca09303244e9fb3e6395a35334e33112aa01c22 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 8 Sep 2019 09:24:37 +0530 Subject: [PATCH 153/161] Done with exercise guessing game --- this keyowrd/exercises.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/this keyowrd/exercises.js b/this keyowrd/exercises.js index f8bc15d..dcd8dd7 100644 --- a/this keyowrd/exercises.js +++ b/this keyowrd/exercises.js @@ -140,3 +140,35 @@ console.log(addOnlyThreeTimes(1, 2)); console.log(addOnlyThreeTimes(2, 2)); console.log(addOnlyThreeTimes(2, 8)); console.log(addOnlyThreeTimes(5, 6)); + +function guessingGame(amt) { + let answer = Math.floor(Math.random() * 11); + let guesses = 0; + return function innerFunc(guess) { + guesses++; + if (guesses > amt) { + return "You're times up!"; + } + if (guess === answer) { + return "You got it!"; + } else if (guess > answer) { + return "You are too high!"; + } else if (guess < answer) { + return "You are too low!"; + } + }; +} + +var game = guessingGame(5); +console.log(game(1)); +console.log(game(5)); +console.log(game(7)); +console.log(game(8)); +console.log(game(10)); +console.log(game(4)); + +var game2 = guessingGame(3); +console.log(game2(1)); +console.log(game2(5)); +console.log(game2(7)); +console.log(game2(6)); From 6fcc1d32c3d400eedbd056dcf5519872ca10bc09 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 11 Sep 2019 07:52:06 +0530 Subject: [PATCH 154/161] Started with OOP notes and exercises --- OOP/Exercises/README.md | 3 +++ OOP/Exercises/index.html | 18 ++++++++++++++++++ OOP/Exercises/script.js | 12 ++++++++++++ OOP/Exercises/style.css | 26 ++++++++++++++++++++++++++ OOP/Notes/OOP-constructors.md | 25 +++++++++++++++++++++++++ OOP/Notes/README.md | 3 +++ OOP/Notes/index.html | 17 +++++++++++++++++ OOP/Notes/script.js | 22 ++++++++++++++++++++++ OOP/Notes/style.css | 26 ++++++++++++++++++++++++++ OOP/README.md | 3 +++ 10 files changed, 155 insertions(+) create mode 100644 OOP/Exercises/README.md create mode 100644 OOP/Exercises/index.html create mode 100644 OOP/Exercises/script.js create mode 100644 OOP/Exercises/style.css create mode 100644 OOP/Notes/OOP-constructors.md create mode 100644 OOP/Notes/README.md create mode 100644 OOP/Notes/index.html create mode 100644 OOP/Notes/script.js create mode 100644 OOP/Notes/style.css create mode 100644 OOP/README.md diff --git a/OOP/Exercises/README.md b/OOP/Exercises/README.md new file mode 100644 index 0000000..8eade75 --- /dev/null +++ b/OOP/Exercises/README.md @@ -0,0 +1,3 @@ +# Object Related exercises from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/OOP/Exercises/index.html b/OOP/Exercises/index.html new file mode 100644 index 0000000..7b6486f --- /dev/null +++ b/OOP/Exercises/index.html @@ -0,0 +1,18 @@ + + + + + + + + + Codestin Search App + + + + + + diff --git a/OOP/Exercises/script.js b/OOP/Exercises/script.js new file mode 100644 index 0000000..bfe90d8 --- /dev/null +++ b/OOP/Exercises/script.js @@ -0,0 +1,12 @@ +function Dog(name, age) { + this.name = name; + this.age = age; + this.bark = function() { + console.log(this.name + " just barked"); + }; +} + +let rusty = new Dog("Rusty", 3); +let fido = new Dog("Fido", 5); + +rusty.bark(); diff --git a/OOP/Exercises/style.css b/OOP/Exercises/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/OOP/Exercises/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/OOP/Notes/OOP-constructors.md b/OOP/Notes/OOP-constructors.md new file mode 100644 index 0000000..b62bdb1 --- /dev/null +++ b/OOP/Notes/OOP-constructors.md @@ -0,0 +1,25 @@ +Constructor function + +It is written like any other function except the name of the functions first letter is capitalized so that it is clear to other programmerrs that this is a constructor function + +example of car construtor is: + +
    +
    +function Car(make,model,year){
    +      this.make = make;
    +      this.model =  model;
    +      this.year =  year;
    +}
    +
    +
    + +The new Keyword is to create these objects + +
    +var probe = new Car('Ford','Probe',1993);
    +var cmax =  new Car('Ford','C-Max', 2014);
    +
    +probe.make;
    +cmax.year;
    +
    diff --git a/OOP/Notes/README.md b/OOP/Notes/README.md new file mode 100644 index 0000000..5915a1e --- /dev/null +++ b/OOP/Notes/README.md @@ -0,0 +1,3 @@ +# Notes Related to Objects in JS from + +https://www.rithmschool.com/courses/javascript/javascript-arrays-array-methods diff --git a/OOP/Notes/index.html b/OOP/Notes/index.html new file mode 100644 index 0000000..d5d2133 --- /dev/null +++ b/OOP/Notes/index.html @@ -0,0 +1,17 @@ + + + + + + + + + Codestin Search App + + + + + diff --git a/OOP/Notes/script.js b/OOP/Notes/script.js new file mode 100644 index 0000000..8ec2ecc --- /dev/null +++ b/OOP/Notes/script.js @@ -0,0 +1,22 @@ +//for in iteration + +var instructor = { + name: "Sam", + knowsJs: true, + knowAngular: true +}; + +for (var info in instructor) { + console.log(instructor[info]); +} + +//Check if key exists using if..in + +var sampleObj = { + name: "sam", + favBook: "Animal Farm" +}; + +if ("favBook" in sampleObj) { + console.log("Fav book exists"); +} diff --git a/OOP/Notes/style.css b/OOP/Notes/style.css new file mode 100644 index 0000000..337e85a --- /dev/null +++ b/OOP/Notes/style.css @@ -0,0 +1,26 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: "Roboto", sans-serif; +} + +.blocks-container { + display: flex; + flex-wrap: wrap; +} +.blocks { + padding: 10px; + background: #eee; + margin-right: 10px; + flex: 1 1 0; +} +.blocks:last-child { + margin-right: 0; +} diff --git a/OOP/README.md b/OOP/README.md new file mode 100644 index 0000000..a3f9f4d --- /dev/null +++ b/OOP/README.md @@ -0,0 +1,3 @@ +# Rithm School-Javascript-Exercises + +Notes about Object oriented programming From a06d47d02abb19480d9b9cce471e413fb7ec5e53 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Thu, 12 Sep 2019 21:35:21 +0530 Subject: [PATCH 155/161] OOP exercises --- OOP/Exercises/index.html | 32 +++++++++++++++++++++++++++++++- OOP/Exercises/script.js | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/OOP/Exercises/index.html b/OOP/Exercises/index.html index 7b6486f..449e30c 100644 --- a/OOP/Exercises/index.html +++ b/OOP/Exercises/index.html @@ -12,7 +12,37 @@ Codestin Search App +

    Exercise Questions

    + 1.What is the purpose of constructor function? + +
    + Sol: Purpose of constructor function is to act like a blueprint from which + we can create new instances of an object using the properties used in the + constructor function. eg: +
    +          function Vehicle(make,type,year){
    +            this.make=make;
    +            this.type=type;
    +            this.year=year;
    +          }
    +        //Creating new object out this constructor
    +        let polo = new Vehicle("Volkswagon","Sedan",2000);
    +      
    +
    + What does the new keyword do? + +
    + Sol:the new keyword does 4 things:
    + 1:create a new object out the constructor;
    + 2:sets the context of this keyword to the newly created + object.
    + 3:sets return this to the constructor function, so we dont + have to write return in constructor function.
    + 4:internal link is created between the object and the + .prototype property on the constructor function. This link + can be accessed by using __prototype__ +
    + - diff --git a/OOP/Exercises/script.js b/OOP/Exercises/script.js index bfe90d8..21119da 100644 --- a/OOP/Exercises/script.js +++ b/OOP/Exercises/script.js @@ -10,3 +10,21 @@ let rusty = new Dog("Rusty", 3); let fido = new Dog("Fido", 5); rusty.bark(); + +//using the call function to borrow properties from another constructor + +function Car(model, type, year) { + this.model = model; + this.type = type; + this.year = year; + this.numberOfwheels = 4; +} + +function MotorCycle(model, type, year, make) { + Car.call(this, model, type, year); + this.numberOfwheels = 2; + this.make = make; +} + +ducatti = new MotorCycle("Ducatti", "Superbike", 2000, "Ducattis"); +console.table(ducatti); From ea023eef50903933bb247021aa56a180a42061c6 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Fri, 13 Sep 2019 06:59:06 +0530 Subject: [PATCH 156/161] completed exercises with constructor --- OOP/Exercises/script.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/OOP/Exercises/script.js b/OOP/Exercises/script.js index 21119da..a8803c1 100644 --- a/OOP/Exercises/script.js +++ b/OOP/Exercises/script.js @@ -28,3 +28,36 @@ function MotorCycle(model, type, year, make) { ducatti = new MotorCycle("Ducatti", "Superbike", 2000, "Ducattis"); console.table(ducatti); + +//Create a constructor function for a Person, each person should have a firstName, lastName, favoriteColor and favoriteNumber. + +function Person(firstName, lastName, favoriteColor, favoriteNumber) { + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteNumber = favoriteNumber; + this.multiplyFavoriteNumber = function(number) { + console.log(this.favoriteNumber * number); + }; +} + +let personOne = new Person("Sam", "Cherian", "blue", 5); +personOne.multiplyFavoriteNumber(2); + +//Refactor the following code so that there is no duplication inside the Child function. + +function Parent(firstName, lastName, favoriteColor, favoriteFood) { + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteFood = favoriteFood; +} + +function Child(firstName, lastName, favoriteColor, favoriteFood) { + Parent.call(this, firstName, lastName, favoriteColor, favoriteFood); +} + +let parent = new Parent("Cherian", "K S", "Red", "Rice"); +let child = new Child("Sam", "Cherian", "Blue", "Rice"); + +console.log(parent, child); From 3b90d71b0621b6938d2f5c33c44158033ea4de58 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sat, 14 Sep 2019 22:01:01 +0530 Subject: [PATCH 157/161] OOP protype and shareing of functions --- OOP/Exercises/index.html | 17 +++++++++++++++++ OOP/Exercises/script.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/OOP/Exercises/index.html b/OOP/Exercises/index.html index 449e30c..6c75e4a 100644 --- a/OOP/Exercises/index.html +++ b/OOP/Exercises/index.html @@ -43,6 +43,23 @@

    Exercise Questions

    can be accessed by using __prototype__ +

    Best practices for using constructor function

    +
      +
    • + Properties and methods which we dont want to be shared can go to + constructor +
    • +
    • + Properties and methods which we want to share can go to constructor. It + is best to share methods in prototype. +
    • +
    +

    + Unless you have a good reason not to, always put function definitions on + the constructor function's prototype. +

    + check script file for code + diff --git a/OOP/Exercises/script.js b/OOP/Exercises/script.js index a8803c1..b8749d0 100644 --- a/OOP/Exercises/script.js +++ b/OOP/Exercises/script.js @@ -60,4 +60,34 @@ function Child(firstName, lastName, favoriteColor, favoriteFood) { let parent = new Parent("Cherian", "K S", "Red", "Rice"); let child = new Child("Sam", "Cherian", "Blue", "Rice"); -console.log(parent, child); +console.log(parent.firstName, child.firstName); + +console.log(Parent.prototype); + +//Best practices for using prototype + +function Books(author) { + this.author = author; + this.booksByAuthor = []; +} +//created two new instances from Books constructor +var orwell = new Books("George Orwell"); +orwell.booksByAuthor.push("1984", "Animal Farm"); + +var wodehouse = new Books("P G Wodehouse"); +wodehouse.booksByAuthor.push( + "Something Fresh", + "Pigs have wings", + "Bachelor Party", + "Psmith" +); + +//adding method to books prototype + +Books.prototype.listAllBooks = function() { + console.log(`Books from ${this.author} are ${this.booksByAuthor}`); +}; + +//since listAllBooks() method was added to the constructor prototype is available to all the instances created from the constructor +orwell.listAllBooks(); +wodehouse.listAllBooks(); From a5c120f22227485fc6e83a18a362fecc978c67dd Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 15 Sep 2019 07:15:01 +0530 Subject: [PATCH 158/161] Learned sharing functions using prototypes. Did one exercise based on that. --- OOP/Exercises/script.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/OOP/Exercises/script.js b/OOP/Exercises/script.js index b8749d0..02767c9 100644 --- a/OOP/Exercises/script.js +++ b/OOP/Exercises/script.js @@ -82,7 +82,7 @@ wodehouse.booksByAuthor.push( "Psmith" ); -//adding method to books prototype +//adding method to books prototype, which will be shared by new instances created from the Books prototype Books.prototype.listAllBooks = function() { console.log(`Books from ${this.author} are ${this.booksByAuthor}`); @@ -91,3 +91,32 @@ Books.prototype.listAllBooks = function() { //since listAllBooks() method was added to the constructor prototype is available to all the instances created from the constructor orwell.listAllBooks(); wodehouse.listAllBooks(); + +//First big exercise on constructor function + +function Vehicle(make, model, year) { + this.make = make; + this.model = model; + this.year = year; + this.isRunning = false; +} + +Vehicle.prototype.turnOn = function() { + console.log((this.isRunning = true)); +}; + +Vehicle.prototype.turnOff = function() { + console.log((this.isRunning = false)); +}; + +Vehicle.prototype.honk = function() { + if (this.isRunning) { + console.log("beep"); + } +}; + +let volkswagon = new Vehicle("polo", "poloSports", 2000); +volkswagon.turnOn(); +volkswagon.honk(); +volkswagon.turnOff(); +volkswagon.honk(); From c2332db9954053bc46ec66885bb894089544f281 Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Sun, 15 Sep 2019 10:56:53 +0530 Subject: [PATCH 159/161] Prototype Part one exercises completed --- OOP/Exercises/index.html | 3 +- OOP/Exercises/prototype-exercises.js | 44 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 OOP/Exercises/prototype-exercises.js diff --git a/OOP/Exercises/index.html b/OOP/Exercises/index.html index 6c75e4a..78e4866 100644 --- a/OOP/Exercises/index.html +++ b/OOP/Exercises/index.html @@ -60,6 +60,7 @@

    check script file for code - + + diff --git a/OOP/Exercises/prototype-exercises.js b/OOP/Exercises/prototype-exercises.js new file mode 100644 index 0000000..9d95c29 --- /dev/null +++ b/OOP/Exercises/prototype-exercises.js @@ -0,0 +1,44 @@ +//Prototypes Exercise + +//Part One: + +// Create a constructor function for a Person. Each person should have a firstName, lastName, favoriteColor, favoriteNumber and favoriteFoods (which should be an array). +function Person(firstName, lastName, favoriteColor, favoriteNumber) { + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteNumber = favoriteNumber; + this.favoriteFoods = []; + this.family = []; +} + +Person.prototype.fullName = function() { + console.log(`${this.firstName} ${this.lastName}`); +}; + +Person.prototype.toString = function() { + console.log( + `${this.firstName} ${this.lastName}, Favorite Color:${this.favoriteColor}, Favorite Number:${this.favoriteNumber}` + ); +}; + +Person.prototype.addToFamily = function(passedVal) { + if (passedVal instanceof Object) { + if (this.family.indexOf(passedVal) === -1) { + console.log(this.family.push(passedVal)); + } + } +}; + +let firstPerson = new Person("Sam", "Cherian", "Red", 10); +let secondPerson = new Person("Joe", "S", "Blue", 11); +console.log(typeof firstPerson); +firstPerson.fullName(); +firstPerson.toString(); + +firstPerson.addToFamily(firstPerson); +firstPerson.addToFamily(firstPerson); +firstPerson.addToFamily(firstPerson); +secondPerson.addToFamily(secondPerson); +console.log(firstPerson.family); +console.log(secondPerson.family); From c62bfe3793f90e623bb4e74075159cc151b6b94d Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Tue, 17 Sep 2019 07:56:49 +0530 Subject: [PATCH 160/161] Started to implement my own version of Array.prototype.map --- OOP/Exercises/prototype-exercises.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/OOP/Exercises/prototype-exercises.js b/OOP/Exercises/prototype-exercises.js index 9d95c29..4a795a5 100644 --- a/OOP/Exercises/prototype-exercises.js +++ b/OOP/Exercises/prototype-exercises.js @@ -32,13 +32,31 @@ Person.prototype.addToFamily = function(passedVal) { let firstPerson = new Person("Sam", "Cherian", "Red", 10); let secondPerson = new Person("Joe", "S", "Blue", 11); -console.log(typeof firstPerson); + firstPerson.fullName(); firstPerson.toString(); -firstPerson.addToFamily(firstPerson); firstPerson.addToFamily(firstPerson); firstPerson.addToFamily(firstPerson); secondPerson.addToFamily(secondPerson); console.log(firstPerson.family); console.log(secondPerson.family); + +//implement my own version of Array.prototype.map + +function FakeArrCreator(newArray) { + this.newArray = newArray; + // console.log(typeof this.newArray); + // try { + // typeof (this.newArray === "array"); + // } catch (e) { + // console.log("error not array", e); + // } +} + +FakeArrCreator.prototype.map = function(fn) { + console.log(this.newArray); +}; + +let multiply = new FakeArrCreator([1, 2, 3]); +console.log(multiply.map()); From 72f6015c299d99b672fea3e9665f527ef49997eb Mon Sep 17 00:00:00 2001 From: Sam Cherian Date: Wed, 18 Sep 2019 07:43:13 +0530 Subject: [PATCH 161/161] created a fake array.protype.map --- OOP/Exercises/prototype-exercises.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/OOP/Exercises/prototype-exercises.js b/OOP/Exercises/prototype-exercises.js index 4a795a5..039a2c8 100644 --- a/OOP/Exercises/prototype-exercises.js +++ b/OOP/Exercises/prototype-exercises.js @@ -55,8 +55,17 @@ function FakeArrCreator(newArray) { } FakeArrCreator.prototype.map = function(fn) { - console.log(this.newArray); + console.log(this); + let newArrOutput = []; + for (item of this.newArray) { + newArrOutput.push(fn(item)); + } + return newArrOutput; }; -let multiply = new FakeArrCreator([1, 2, 3]); -console.log(multiply.map()); +let multiply = new FakeArrCreator([1, 2, 5]); +console.log( + multiply.map(function(p) { + return p * 2; + }) +);