Skip to content

Instantly share code, notes, and snippets.

Nya NyaGarcia

Block or report user

Report or block NyaGarcia

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@NyaGarcia
NyaGarcia / if-else-if.js
Created Nov 7, 2019
Verbose if else if statement
View if-else-if.js
function attack(pokemon) {
if (pokemon.type === 'water') {
useWaterAttack();
} else if (pokemon.type === 'fire') {
useFireAttack();
} else if (pokemon.type === 'ice') {
useIceAttack();
} else if (pokemon.type === 'electric') {
useElectricAttack();
} else {
@NyaGarcia
NyaGarcia / fix-return-true-false.js
Created Nov 7, 2019
Directly returning boolean values to avoid return true return false
View fix-return-true-false.js
function isWildPokemon(pokemon) {
return pokemon.isWild;
}
function isSaved(pokemonId) {
return this.savedPokemon.some(({ id }) => id === pokemonId);
}
@NyaGarcia
NyaGarcia / return-true-false.js
Created Nov 7, 2019
Two functions that return true; return false;
View return-true-false.js
function isWildPokemon(pokemon) {
if (pokemon.isWild) {
return true;
} else {
return false;
}
}
function isSaved(pokemonId) {
if (this.savedPokemon.some(({ id }) => id === pokemonId)) {
@NyaGarcia
NyaGarcia / boolean-shorthands.js
Last active Nov 6, 2019
Conditionals with boolean shorthands
View boolean-shorthands.js
if (isWildPokemon) {
usePokeball();
}
if (isPokemon && !isSaved) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / non-boolean-shorthands.js
Last active Nov 6, 2019
Conditionals with boolean variables without using shorthands
View non-boolean-shorthands.js
if (isWildPokemon === true) {
usePokeball();
}
if (isPokemon === true && isSaved === false) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / extracting-function-logic.js
Last active Oct 10, 2019
Extracting logic from a complex function
View extracting-function-logic.js
function startProgram() {
if (!window.indexedDB) {
throw new Error("Browser doesn't support indexedDB");
}
initDatabase();
setListeners();
printEmployeeList();
}
@NyaGarcia
NyaGarcia / complex-function.js
Last active Oct 10, 2019
An example of a complex function
View complex-function.js
function startProgram() {
if (!window.indexedDB) {
window.alert("Browser not support indexeDB");
} else {
let openRequest = indexedDB.open("store", 1);
openRequest.onupgradeneeded = () => {};
openRequest.onerror = () => {
console.error("Error", openRequest.error);
@NyaGarcia
NyaGarcia / substitute-for.js
Last active Oct 10, 2019
Substituting a for loop with Array.map and Array.filter
View substitute-for.js
function getNews(type) {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, type);
}
function getNewsContent(newsList, type) {
return newsList
.filter(newsItem => newsItem.type === type)
.map(newsItem => newsItem.content);
}
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active Oct 10, 2019
Further refactoring duplicating code
View fixing-duplication.js
function getNews(type) {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, type);
}
function getNewsContent(newsList, type) {
const news = [];
for (let i = newsList.length - 1; i >= 0; i--) {
if (newsList[i].type === type) {
news.push(newsList[i].content);
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active Oct 10, 2019
An example of how to avoid code duplication by extracting logic
View fixing-duplication.js
function getJavascriptNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'javascript');
}
function getRustNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'rust');
}
You can’t perform that action at this time.