Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on git #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions 1-hoisting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,24 @@ 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!';
```

2. Why does JavaScript throw an error instead of logging `undefined` in the following code?

```js
console.log(message);

// because has already been declared and let value is usssed only once
let message = 'Hi there!';
```

3. Explain precisely what happens when the following code is executed.

```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!';
};
Expand All @@ -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!';
}
Expand All @@ -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];
```
Expand All @@ -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';
Expand Down
86 changes: 78 additions & 8 deletions 2-higher-order-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
Expand All @@ -34,30 +36,83 @@ 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<numbers.length ; i++){
total = total*numbers[i]
}
return total
}``

4. Write a function called `cubeAll` that cubes each number in an array, and
returns an array of all the numbers *cubed* using a `for` loop; then,
refactor it to use `each`.
``function cubeAll(number){
var arr = []
each(number,function(numbers){
arr.push(numbers*numbers*numbers);
})
return arr;
}
function product2(numbers){
var arr=[]
for(var i=0 ; i<numbers.length ; i++){
arr.push(numbers[i]*numbers[i]*numbers[i])
}
return arr
}``


5. Write a function called `odds` that accepts an array as a parameter and
returns an array of just the odd numbers.
`` function odds(array){
var arr=[];
each(array,function(element){
arr.push(elements%2!==0);
})
return arr;
}
function odds(array){
var newArr=[];
for(var i=0;i<array.length;i++){
if(array[i]%2!==0){
newArr.push(array[i])
}
}
return newArr;
}``

### More Practice

#### Summations

1. Write a function `sumByAllElementsMultipliedByFour` that takes an array as an
argument and returns the sum of all elements multiplied by four.
``function sumByAllElementsMultipliedByFour(array){
var result=0;
each(array,function(elements){
result+=(elements*4)
})
return result
}``

2. Observe that `sumByAllElementsMultipliedByFour` is a terrible name for a
function &#x2013; you should also notice that `sumByAllElementsMultipliedByFour`
Expand All @@ -69,7 +124,11 @@ In order to complete these exercises, open [repl.it](https://repl.it/), choose J

```js
function sumBy(numbers, f) {
// ...
var result=0;
each(numbers,function(element){
result+=f(element)
})
return result;
}
var numbers = [1, 2, 3, 4];
sumBy(numbers, square); // => 30
Expand All @@ -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

Expand Down