Map, Filter, Arrows-fn's || weak2.6
// map, filter, arrowss fns.
/* ---------------------> Syntax
// normal function syntax
// In js
function sum(a, b) {
return a+b;
}
// In express
app.get("/", function (req, res) {
})
// arrow function syntax
// In js
const sum = (a, b) => {
return a + b;
}
// In express
app.get("/", (req, res) => {
})
const ans = sum (1,2);
console.log(ans);
<-------------------------*/
/* ---------------------> Porblem Statement1.
// given an array, me back a new array in which every value multiplied by 2
// [1,2,3,4,5]
// [2,4,6,8,10]
// Ugnly Way To Write Code
const input = [1,2,3,4,5];
// solution
const newArray =[];
for (let i = 0; i < input.length; i++) {
newArray.push(input[i]*2);
}
console.log(newArray);
// Good Way To Right Code
// map filter
const input = [1,2,3,4,5];
function transform(i) {
return i*3;
}
const ans = input.map(transform)
console.log(ans);
// OR
const input = [1,2,3,4,5];
const ans = input.map(function (i) {
return i*3;
})
console.log(ans);
<-------------------- */
/* --------------------> Problem statement2.
// filtering
// Ugly Way To Write Code
// what if i tell you, gien an input array, me back all the even values from it
const arr = [1,2,3,4,5]
// ans
const newArray = []
for (let i = 0; i < arr.length; i++) {
if (arr[i]%2==0) {
newArray.push(arr[i]);
}
}
console.log(newArray);
// Pretty Way To Write Code
const arr = [1, 2, 3, 4, 5]
// ans
function filterLogic(n) {
if (arr[i] % 2 == 0) {
return true;
} else {
return false;
}
}
const ans = arr.filter(filterLogic);
console.log(ans);
// OR
// Pretty Way To Write Code / mosst time use of case
const arr = [1, 2, 3, 4, 5]
// ans
const ans = arr.filter(function (n) {
if (n % 2 == 0) {
return true;
} else {
return false;
}
});
console.log(ans);
// OR
// arrow function
const arr = [1, 2, 3, 4, 5]
// ans
const ans = arr.filter((n) => {
if (n % 2 == 0) {
return true;
} else {
return false;
}
});
console.log(ans);
<---------------- */
/**
// Print name which start with 'H' string
const arr = ["harkirat","raman","prash"]
// ans
const ans = arr.filter(function (n) {
if (n.startsWith("h")) {
return true;
} else {
return false;
}
});
console.log(ans);
*/
/* ---------------------> Syntax
// normal function syntax
// In js
function sum(a, b) {
return a+b;
}
// In express
app.get("/", function (req, res) {
})
// arrow function syntax
// In js
const sum = (a, b) => {
return a + b;
}
// In express
app.get("/", (req, res) => {
})
const ans = sum (1,2);
console.log(ans);
<-------------------------*/
/* ---------------------> Porblem Statement1.
// given an array, me back a new array in which every value multiplied by 2
// [1,2,3,4,5]
// [2,4,6,8,10]
// Ugnly Way To Write Code
const input = [1,2,3,4,5];
// solution
const newArray =[];
for (let i = 0; i < input.length; i++) {
newArray.push(input[i]*2);
}
console.log(newArray);
// Good Way To Right Code
// map filter
const input = [1,2,3,4,5];
function transform(i) {
return i*3;
}
const ans = input.map(transform)
console.log(ans);
// OR
const input = [1,2,3,4,5];
const ans = input.map(function (i) {
return i*3;
})
console.log(ans);
<-------------------- */
/* --------------------> Problem statement2.
// filtering
// Ugly Way To Write Code
// what if i tell you, gien an input array, me back all the even values from it
const arr = [1,2,3,4,5]
// ans
const newArray = []
for (let i = 0; i < arr.length; i++) {
if (arr[i]%2==0) {
newArray.push(arr[i]);
}
}
console.log(newArray);
// Pretty Way To Write Code
const arr = [1, 2, 3, 4, 5]
// ans
function filterLogic(n) {
if (arr[i] % 2 == 0) {
return true;
} else {
return false;
}
}
const ans = arr.filter(filterLogic);
console.log(ans);
// OR
// Pretty Way To Write Code / mosst time use of case
const arr = [1, 2, 3, 4, 5]
// ans
const ans = arr.filter(function (n) {
if (n % 2 == 0) {
return true;
} else {
return false;
}
});
console.log(ans);
// OR
// arrow function
const arr = [1, 2, 3, 4, 5]
// ans
const ans = arr.filter((n) => {
if (n % 2 == 0) {
return true;
} else {
return false;
}
});
console.log(ans);
<---------------- */
/**
// Print name which start with 'H' string
const arr = ["harkirat","raman","prash"]
// ans
const ans = arr.filter(function (n) {
if (n.startsWith("h")) {
return true;
} else {
return false;
}
});
console.log(ans);
*/
Comments