diff --git a/1-hoisting/README.md b/1-hoisting/README.md index d17fc15..8ac1dc8 100644 --- a/1-hoisting/README.md +++ b/1-hoisting/README.md @@ -62,7 +62,8 @@ Type out your best answers to the following questions: ```js console.log(message); - + // because the declaration of the varuble is after the console log so message of console log is undefind + //When declering a var the output is undefind var message = 'Hi there!'; ``` @@ -70,7 +71,7 @@ Type out your best answers to the following questions: ```js console.log(message); - + // because has already been declared and let value is usssed only once let message = 'Hi there!'; ``` @@ -78,7 +79,7 @@ Type out your best answers to the following questions: ```js console.log(showMessage()); - +//It will be an error because the code will be read frm the top and the consol will stop at the first line because showMessage is not a function yeet var showMessage = function(){ return 'Hi there!'; }; @@ -88,7 +89,7 @@ Type out your best answers to the following questions: ```js console.log(showMessage()); - +// because the function has allredy been declaired and it is stored function showMessage(){ return 'Hi there!'; } @@ -100,9 +101,13 @@ Restructure the following instances of code to work correctly: ```js // 1. + function getValues(values){ + var array=[] for(var i = 0; i < values.length; i++){ - console.log(values[i]); + array.push(values[i]); } + return array; +} var values = [10, 20, 30]; ``` @@ -111,7 +116,8 @@ Restructure the following instances of code to work correctly: console.log(welcome('Charlie', 'Munger')); function welcome(first, last) { - return `Welcome, ${first} ${last}! You last logged in on ${lastLogin}.` + var welc= `Welcome, ${first} ${last}! You last logged in on ${lastLogin}.`; + return welc }; var lastLogin = '1/1/1970'; diff --git a/2-higher-order-functions/README.md b/2-higher-order-functions/README.md index b6ed6b1..c853676 100644 --- a/2-higher-order-functions/README.md +++ b/2-higher-order-functions/README.md @@ -25,7 +25,9 @@ In order to complete these exercises, open [repl.it](https://repl.it/), choose J ```js function sumSquares(numbers) { var total = 0; - // ... + each(numbers,function(element){ + total += (element*element) + }) return total; } ``` @@ -34,23 +36,69 @@ In order to complete these exercises, open [repl.it](https://repl.it/), choose J ```js function sumCubes(numbers) { - var total = 0; - for (var i = 0; i < numbers.length; i++) { - total = total + cube(numbers[i]); - } - return total; - } + var total = 0; + each(numbers,function(element){ + total += (element*element*element) + }) + return total; + } ``` 3. Write a function called `product` that calculates the product of an array of numbers using a `for` loop; then, refactor it to use `each`. + + ``function product(number){ + var total = 1 + each(number,function(numbers){ + total*=numbers; + }) + return total; + } + function product2(numbers){ + var total=1 + for(var i=0 ; i 30 @@ -82,8 +141,19 @@ In order to complete these exercises, open [repl.it](https://repl.it/), choose J 3. How can you use `sumBy` to compute the sum of an array of numbers (just the plain sum)? + //loop throo the array and each tieme aplay the function used in the input at the number of the array and sum the all inthe variable declaired 4. Write a function `productBy` that works like `sumBy`, but for **products**. +``` +function productBy(numbers, f) { + var result=0; + each(numbers,function(element){ + result*=f(element) + }) + return result; + } + +``` #### Refactoring