Last active
June 12, 2021 13:31
-
-
Save pratikdevdas/5bbc0de714d74f46e7d78ddb7702dc8f to your computer and use it in GitHub Desktop.
Small snippets used in web dev testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ex.1: simple array | |
const average = (array) => { | |
const reducer = (sum, item) => { | |
return sum + item | |
} | |
return array.reduce(reducer, 0) / array.length | |
} | |
console.log(average([6,3,5,6])) | |
//output : 5 | |
//ex.2: array with objects | |
const average2 = array => { | |
const reducer = (sum, item) => { | |
return sum + item; | |
}; | |
return array.reduce(reducer, 0) / array.length; | |
}; | |
const rex = [ | |
{ content: 'hi', likes: 6 }, | |
{ content: 'hi', likes: 3 }, | |
{ content: 'hi', likes: 5 }, | |
{ content: 'hi', likes: 6 }, | |
]; | |
console.log(average2(rex.map(note => note.likes))); | |
//output : 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const palindrome = (string) => { | |
return string | |
.split('') | |
.reverse() | |
.join('') | |
} | |
console.log(palindrome('hey')) | |
//output : 'yeh' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment