Last active
July 10, 2020 15:45
-
-
Save mattfwood/60e0ac0e6ef52da2e4ce9dfcfc33f658 to your computer and use it in GitHub Desktop.
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
| // You need to do two validation steps for params that are passed to a function | |
| // Option #1 | |
| function createUser({ name, email, password }) { | |
| if (name && email) { | |
| if (validateEmail(email)) { | |
| return createUser({ name, email}) | |
| } else { | |
| throw new Error('Email not valid'); | |
| } | |
| } else if (!name || !email) { | |
| throw new Error('Name is requried'); | |
| } | |
| } | |
| // Option #2 | |
| function createUser({ name, email }) { | |
| if (!name || !email) { | |
| throw new Error('Name and email are required'); | |
| } | |
| if (!validateEmail(email)) { | |
| throw new Error('Email not valid'); | |
| } | |
| return createUser({ name, email }); | |
| } | |
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
| // Show a different icon depending on different keys | |
| // Option #1 | |
| function getIcon(name) { | |
| switch (name) { | |
| case home: | |
| return 'house'; | |
| case 'event': | |
| return 'calendar'; | |
| case 'rental': | |
| return 'bike'; | |
| default: | |
| console.warn(`Icon ${name} not found`); | |
| return 'not-found'; | |
| } | |
| } | |
| // Option #2 | |
| const listingIcons = { | |
| home: 'house', | |
| event: 'calendar', | |
| rental: 'bike', | |
| } | |
| function getIcon(name) { | |
| if (!listingIcons[name]) { | |
| console.warn(`Icon ${name} not found`); | |
| return 'not-found'; | |
| } | |
| return listingIcons[name]; | |
| } |
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
| // You need to remove an item from an array | |
| const list = ['apple', 'orange', 'banana']; | |
| // Option #1 | |
| function removeItem(removedItem) { | |
| return list.filter(item => item !== removedItem); | |
| } | |
| // Option #2 | |
| functon removeItem(removeItemIndex) { | |
| return list.splice(1, removeItemIndex); | |
| } | |
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
| // You need to show a part of the UI only on mobile devices | |
| // Option #1 | |
| if (window.innerWidth < 768){ | |
| showMobileMenu(); | |
| } | |
| // Option #2 | |
| const isMobile = window.innerWidth < 768; | |
| if (isMobile) { | |
| showMobileMenu(); | |
| } |
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
| // You have to search an array of items against a text input box value | |
| // this value is coming from a text input | |
| const search = 'groceries'; | |
| const completeTasks = ['stay indoors']; | |
| const allTasks = ['get groceries', 'stay indoors', 'buy a boat']; | |
| // Option #1 | |
| const results = allTasks.filter( | |
| (task) => | |
| // only show results if something has been entered | |
| search.length !== 0 && | |
| !completeTasks.some( | |
| (completeTask) => completeTask === task | |
| ) && | |
| item.toLowerCase().includes(search.toLowerCase()) | |
| ); | |
| // Option #2 | |
| const searchValid = search.length > 0; | |
| const incompleteTasks = allTasks.filter(task => !completeTasks.includes(task)); | |
| const results = allTasks.filter((task) => { | |
| const queryMatch = task.toLowerCase().includes(search.toLowercase()); | |
| const validTask = searchValid && incompleteTasks.includes(task) && queryMatch; | |
| return validTask; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment