Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
function getPokemon(type) {
let pokemon;
switch (type) {
case 'Water':
pokemon = 'Squirtle';
break;
case 'Fire':
pokemon = 'Charmander';
break;
case 'Plant':
@NyaGarcia
NyaGarcia / replace-switch-object.js
Last active September 23, 2020 12:16
Using an object literal to replace a switch statement
const pokemon = {
Water: 'Squirtle',
Fire: 'Charmander',
Plant: 'Bulbasur',
Electric: 'Pikachu'
};
function getPokemon(type) {
return pokemon[type] || 'Mew';
}
@NyaGarcia
NyaGarcia / replace-switch-map.js
Last active October 8, 2019 17:55
Using a Map to replace a switch statement
const pokemon = new Map([
['Water', 'Squirtle'],
['Fire', 'Charmander'],
['Plant', 'Bulbasur'],
['Electric', 'Pikachu']
]);
function getPokemon(type) {
return pokemon.get(type) || 'Mew';
}
@NyaGarcia
NyaGarcia / complex-conditional.js
Last active October 9, 2019 16:51
A function with a complex conditional statement
function checkGameStatus() {
if (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
) {
quitGame();
}
}
@NyaGarcia
NyaGarcia / extracting-conditional.js
Created October 9, 2019 17:03
Extracting a conditional to a function
function isGameLost() {
return (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
);
}
// Our function is now much easier to understand:
function checkGameStatus() {
@NyaGarcia
NyaGarcia / duplication.js
Last active October 10, 2019 13:12
An example of duplicated code
function getJavascriptNews() {
const allNews = getNewsFromWeb();
const news = [];
for (let i = allNews.length - 1; i >= 0; i--){
if (allNews[i].type === "javascript") {
news.push(allNews[i]);
}
}
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active October 10, 2019 15:10
An example of how to avoid code duplication by extracting logic
function getJavascriptNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'javascript');
}
function getRustNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'rust');
}
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active October 10, 2019 13:03
Further refactoring duplicating code
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 / substitute-for.js
Last active October 10, 2019 20:00
Substituting a for loop with Array.map and Array.filter
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 / complex-function.js
Last active October 10, 2019 20:01
An example of a complex function
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);