Skip to content
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
108 changes: 1 addition & 107 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,107 +1 @@
![Image description](https://i1.faceprep.in/ProGrad/face-logo-resized.png)

# ProGrad Lab | Higher Order Function

In the previous lesson, we have learnt some useful Higher Order Functions to manipulate arrays & objects. Now, let's put them to use in this lab.

## What should you do
```
Fork this repo
Clone this repo
Practice JavaScript - Higher order functions
```

## How To Submit
```
Upon completion, run the following commands:

git add .
git commit -m "ProGrad ID"
git push origin master

And finally, create a pull request so your ProGrad Mentor (PM) can review your work.
```

# Instructions
Create app.js and index.html. Write the code for the following functionalities given below and see the output in console.

### Function #1: Array Slice

Take an example array. We will be using the below array for reference.
```
const foods = [pizza, burger, fingerChips, donuts, springRoll];

```
Create a new array named as `modifiedFood` and also create a `slice()` method.The `slice()` method should return the selected elements in an array, as a new array object. This method starts slicing the array from the given start argument and ends the slicing operation one element before the given second argument.

```
Expected output: [burger, fingerShips, donuts]
```


### Function #2: Array Splice

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Take an example array. Create a new array named as `modifiedFood` and use `splice()` to get the below expected output. Elements are added to the original array.

```
const foods = [pizza, burger, fingerShips, donuts, springRoll];

Expected output: [pizza, burger, noodles, icecream, fingerShips, donuts, springRoll];
```


### Function #3: Filter

Filter is an array method. It accepts a test function as an argument and returns a new array with only the elements for which the test function returns true.

Now, create a function `isEven()` which takes `numberArray` as argument and _returns all even numbers from the given array_.

```
Take numberArray as

const numberArray = [12,324,213,4,2,3,45,4234];
```

Next, let's build on top of this. Create another functon called `isPrime()` with takes same `numberArray` as argument and _returns all prime numbers from the above given array_.


### Function #4: Reject

We can now reuse the `isPrime()` function in conjunction with reject to get all the non-prime numbers in the array. The reject function does the opposite of the filter function. It returns an array with all the elements that do not satisfy the condition. Kindly note that there is no reject method in javascript arrays like filters. So you need to define a new function and make it work like reject.

Create a function `nonPrime()` which takes `numberArray` as argument and _return all non-prime numbers from the above given array in Function #3_.


### Function #5: Lambda

When defining short functions, it's convenient to use an alternative syntax called lambda function that allows us to define anonymous functions in a more compact way: _( /*arguments*/ ) => { /*code*/ }_. If our function is only a return statement, we can even strip the curly brackets and avoid writing return: _( /*arguments*/ ) => /*value to return*/_.

Now, rewrite the function `isEven()` by using the concept of Lambda Function.


### Function #6: Map

Another very useful higher order function is map. It takes a function and applies it to all the elements of an array.
The syntax is identical to `filter`.

Now, create a function `findSquareOfNumbers()` which takes the following array called `myArray` as argument and _returns square value of all elements in the given array_.

```
const myArray = [11, 34, 20, 5, 53, 16];
```

### Function #7: Reduce

Reduce is the last higher order function we are going to discuss and it's also the most powerful one. In fact, you can implement any list transformation operation with reduce.

Reduce takes a callback function and a starting value. The callback function takes as arguments an accumulator and the value of the current element of the array and returns the accumulator to be used in the next cycle. The values returned from the last call of the callback function is the value returned by `reduce`.

Create a function `multiply()` which takes an argument of `myArray` which holds the values as `[2, 3, 5, 10]` and _returns multiplication value of all numbers in the given array_.

Let's expand our knowledge by `implementing map with reduce`.

Take the function explained in the above task of `map` and try is by implementing the same by using `reduce`.

Happy Coding ProGrad ❤️
![image](https://user-images.githubusercontent.com/66902249/168128194-70923d65-11db-4259-b4c6-d51751d9c5cb.png)
92 changes: 92 additions & 0 deletions func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
function slice() {
const foods = ['pizza','burger','fingerChips','donuts','springRoll','Sandwich','HotDog','Pancake'];
var num1 = parseInt(document.getElementById("n1").value);
var num2 = parseInt(document.getElementById("n2").value);
var modifiedFood = [];
if(num1>=0 && num1<=7 && num2>=1 && num2<=8){
for(let i=num1;i<num2;i++){
modifiedFood.push(foods[i]);
}
document.getElementById("out1").innerText = "modifiedFood = ["+modifiedFood+"]";
}
else{
document.getElementById("out1").innerText = "Value out of array index! Please enter again.";
}

return modifiedFood
}

function Addsplice() {
const foods = ['pizza','burger','fingerChips','donuts','springRoll'];
var num1 = parseInt(document.getElementById("n3").value);
var value = document.getElementById("n4").value;
var modifiedFood = [];
if(num1>=0 && num1<=4){
let i;
for(i=0;i<num1;i++){
modifiedFood.push(foods[i]);
}
modifiedFood.push(value);
for(;i<foods.length;i++){
modifiedFood.push(foods[i]);
}
document.getElementById("out2").innerText = "modifiedFood = ["+modifiedFood+"]";
}
else{
document.getElementById("out2").innerText = "Value out of array index! Please enter again.";
}
return modifiedFood;
}

function isEven(){
const numberArray = [12,324,213,4,2,3,45,4234];
function checkEven(num){
return num%2==0;
}
var res = numberArray.filter(checkEven);
document.getElementById("out3").innerText = "evennumberArray = ["+res+"]";
return res;
}

function isprime(){
const numberArray = [12,324,213,4,2,3,45,4234];
var notprime = [];
for(let i=0;i<numberArray.length;i++){
let j;
for(j=2;j<numberArray[i];j++){
if(numberArray[i]%j==0){
break;
}
}
if(j==numberArray[i]){
notprime.push(numberArray[i]);
}
}
document.getElementById("out4").innerText = "Notprime = ["+notprime+"]";
return notprime;
}

function checkEven(){
const numberArray = [12,324,213,4,2,3,45,4234];
var res = numberArray.filter(value => value%2==0);
document.getElementById("out5").innerText = "evennumberArray = ["+res+"]";
return res;
}

function findSquare(){
const myArray = [11, 34, 20, 5, 53, 16];
let output = myArray.map(function(ele){
return ele*ele;
})
document.getElementById("out6").innerText = "SquareofArray = ["+output+"]";
return output;
}

function multiply(){
const myArray = [2, 3, 5, 10];
let mul = myArray.reduce(function(mul,ele){
return mul*ele;
},1)
document.getElementById("out7").innerText = "Multiply Value = "+mul;
return mul;
}
50 changes: 50 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="func.js"></script>
<title>Higher Order Function</title>
</head>
<body>
<h1>Function 1 : Array Slice()</h1>
<p style="font-size: larger;">const foods = [pizza, burger, fingerChips, donuts, springRoll, Sandwich, HotDog, Pancake]</p>
<input type="number" id="n1" placeholder="start position">
<input type="number" id="n2" placeholder="end position">
<input type="submit" value="Submit" onclick="slice()">
<p style="font-size: larger;" id="out1"></p>

<h1>Function 2 : Array Splice()</h1>
<p style="font-size: larger;">const foods = [pizza, burger, fingerChips, donuts, springRoll]</p>
<input type="number" id="n3" placeholder="position">
<input type="text" id="n4" placeholder="element">
<input type="submit" value="Add" onclick="Addsplice()">
<p style="font-size: larger;" id="out2"></p>

<h1>Function 3 : Filter()</h1>
<p style="font-size: larger;">const numberArray = [12,324,213,4,2,3,45,4234]</p>
<input type="submit" value="Filter even number" onclick="isEven()">
<p style="font-size: larger;" id="out3"></p>

<h1>Function 4 : Reject()</h1>
<p style="font-size: larger;">const numberArray = [12,324,213,4,2,3,45,4234]</p>
<input type="submit" value="Not prime number" onclick="isprime()">
<p style="font-size: larger;" id="out4"></p>

<h1>Function 5 : Lambda()</h1>
<p style="font-size: larger;">const numberArray = [12,324,213,4,2,3,45,4234]</p>
<input type="submit" value="Even using Lambda" onclick="checkEven()">
<p style="font-size: larger;" id="out5"></p>

<h1>Function 6 : Map()</h1>
<p style="font-size: larger;">const myArray = [11, 34, 20, 5, 53, 16]</p>
<input type="submit" value="Square of number" onclick="findSquare()">
<p style="font-size: larger;" id="out6"></p>

<h1>Function 7 : Reduce()</h1>
<p style="font-size: larger;">const myArray = [2, 3, 5, 10]</p>
<input type="submit" value="multiply all" onclick="multiply()">
<p style="font-size: larger;" id="out7"></p>
</body>
</html>