Skip to content

Instantly share code, notes, and snippets.

@MrJadaml
Last active March 29, 2017 20:08
Show Gist options
  • Save MrJadaml/9d825f6f4f545c8ac55169dedad0cea1 to your computer and use it in GitHub Desktop.
Save MrJadaml/9d825f6f4f545c8ac55169dedad0cea1 to your computer and use it in GitHub Desktop.
// ----------------------------------------------------------------------------
//
//
// ___ ___ ___
// / /\ ___ / /\ /__/\
// / /::\ / /\ / /:/_ | |::\
// / /:/\:\ / /:/ / /:/ /\ | |:|:\
// / /:/~/:/ / /:/ / /:/ /:/ __|__|:|\:\
// /__/:/ /:/___ / /::\ /__/:/ /:/ /__/::::| \:\
// \ \:\/:::::/ /__/:/\:\ \ \:\/:/ \ \:\~~\__\/
// \ \::/~~~~ \__\/ \:\ \ \::/ \ \:\
// \ \:\ \ \:\ \ \:\ \ \:\
// \ \:\ \__\/ \ \:\ \ \:\
// \__\/ \__\/ \__\/
//
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
// ----------------------------------------------------------------------------
//-----------------------------# DIRECTIONS #----------------------------------
// Before you start give the MDN docs on Arrow functions a read/skim.
// Feel free to skip over anything related to "binding" or "this".
// After doing this you should have a genearal idea of how to work
// with arrow functions. You will see varirying examples of how arrow
// functions can be used which will be a useful reference as you work
// through the problems you are tasked with implementing below.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
const rp = require('request-promise');
const awesomeArray = [ 1, 23, 5, 19, 15, 13, 5, 1, 18, 18, 1, 25];
const uri = 'https://www.metaweather.com/api/location/2490383/'
const options = { uri, json: true }
// Refactor this function to use an es6 arrow function:
function printPrettyDate(timestamp) {
let date = new Date(timestamp);
let prettyDate = date.toString().split(' ').slice(0,4).join(' ');
console.log(prettyDate);
}
// ----------------------------------------------------------------------------
// Refactor the callback function to use an es6 arrow function.
// If a function only has one paramater, you don't need the enclosing parens.
// Make sure to omit the parens when refactoring the callback function.
let filteredAwesomeArray = awesomeArray.filter(function(item) {
return item % 2;
})
// ----------------------------------------------------------------------------
// es6 Arrow functions can be written as an expression
// - In a single line with an *implicit* return.
// Refactor this function into a single line expression
// - No code block/curley braces
// - No return keyword
// Arrow functions w/ multiple params require parens
let sum = awesomeArray.reduce(function(a, b) {
return a + b;
});
// ----------------------------------------------------------------------------
// Refactor the `fetchData` function declaration to use an arrow function.
// the function should only be a single line, using an *implicit* return.
function fetchData() {
return {
data: 'data'
}
}
// ----------------------------------------------------------------------------
// Promise handlers are no different.
// Refactor the promise handler to use an arrow function
rp(options)
.then(function(data) {
console.log('Sun Rise:');
printPrettyDate(data.sun_rise);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment