Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created June 22, 2022 03:44
Show Gist options
  • Save natafaye/68b31befb98ee139d2e5ea75db6031ad to your computer and use it in GitHub Desktop.
Save natafaye/68b31befb98ee139d2e5ea75db6031ad to your computer and use it in GitHub Desktop.
/***** Converting Between Arrow and Normal Functions *****/
// Normal Function
function isJose(user) {
return user.username === "jose"
}
// Arrow Function
const isJose = (user) => {
return user.username === "jose"
}
// Get rid of the parenthesis around one parameter (if you want)
const isJose = user => {
return user.username === "jose"
}
// Get rid of the parenthesis around one parameter (if you want)
const isJose = user => user.username === "jose"
function isNotEmpty(paper) {
return paper !== "";
}
const isNotEmpty = (paper) => {
return paper !== "";
}
const isNotEmpty = paper => {
return paper !== "";
}
const isNotEmpty = paper => paper !== ""
const isNotEmpty = p => p !== ""
name => "Hello " + name
name => {
return "Hello " + name
}
(name) => {
return "Hello " + name
}
function makeGreeting(name) {
return "Hello " + name
}
/***** Array Methods *****/
const emailList = [
{
id: 234,
author: "Natalie",
to: "Calvin",
message: "Heyyyy",
read: false,
},
{
id: 1654,
author: "Natalie",
to: "Calvin",
message: "What's up?",
read: true,
},
{
id: 474,
author: "Dylan",
to: "Calvin",
message: "Good day",
read: false,
}
]
alert( emailList.map( email => email.author + " - " + email.message ).join("\n") );
alert( emailList.map( email => email.author + ":\n" + email.message ).join("\n\n") );
alert( emailList.map( function(email) {
return email.author + " - " + email.message
}) );
const unreadEmails = emailList.filter( email => !email.read ).map( email => email.author + ":\n" + email.message ).join("\n\n")
alert(unreadEmails);
// We CAN do map then filter, we just don't tend to
const emailStringsThatStartWithN = emailList.map( email => email.author + ":\n" + email.message ).filter( stringOfEmail => stringOfEmail[0] === "N" ).join("\n\n")
alert( emailList.find( message => message.id === 1654 ).author )
const idToFind = 1654
const emailWithId = emailList.find( message => message.id === idToFind )
console.log( emailWithId.author )
// Challenge: make a function that takes an id as a parameter and returns the author of the email with that id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment