This file contains 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
// ... | |
function updateUserName(user, { firstName, lastName }) { | |
const { name } = user; | |
const newName = { | |
firstName: firstName || name.firstName, | |
lastName: lastName || name.lastName, | |
}; | |
return { ...user, name: newName }; | |
} |
This file contains 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
// ... | |
function updateUserName(user, { firstName, lastName }) { | |
const { name } = user; | |
if (firstName) name.firstName = firstName; | |
if (lastName) name.lastName = lastName; | |
return { ...user, name }; | |
} | |
function saveUser(users, updatedUser) { |
This file contains 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
function userByMatcher({ key, value }) { | |
return user => user[key] === value; | |
} | |
function updateUserName(user, { firstName, lastName }) { | |
// <~ #1 mutating argument, bad idea | |
if (firstName) user.name.firstName = firstName; | |
if (lastName) user.name.lastName = lastName; | |
return user; | |
} |
This file contains 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
// Tail call optimization | |
// this function call itself n times | |
function f(x) { | |
// with every new call, new stack frame is created | |
// there is a limit, this limit varies from engine to another, but there is a limit | |
// this simple function breaks on chrome at f(100000) | |
if (x < 1) { | |
return 0 | |
} |