Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
/**
* Returns a deep copy of the array or object passed to it.
* Useful for copying objects and arrays with nested complex types.
*
* @param {array|object} o The value to deep copy, could be array or object.
* @returns {array|object} Returns a deep copy of the supplied array or object.
*/
function deepClone(o) {
// Construct the copy `output` array or object.
// Use `Array.isArray()` to check if `o` is an array.
function isPalindrome(word) {
return (
!!(word && Object.prototype.toString.call(word) === '[object String]')
&& (
word.length <= 1
|| RegExp(word[0], 'i').test(word.slice(-1))
&& isPalindrome(word.slice(1, -1))
)
)
}
// Create a new Express app and set the port
const app = require('express')();
const PORT = process.env.PORT || 5000;
// The timeout in seconds for API responses
const TIMEOUT_SECONDS = 5;
// Define a new route on the Express app: GET /random
app.get('/random', (req, res) => {
// An object that will contain the current temperatures of the cities
// The keys are the city names, while the values are their current temperatures (in °C)
let TEMPS = null;
// The maximum number of retries for failed temperature fetches
const MAX_TEMP_FETCH_RETRIES = 5;
/**
* Fetches the current temperatures of multiple cities (in °C) and update the `TEMPS` object.
* Also schedule retries for failed temperature fetches.
const fetchTempForCity = city => {
return fetch(`${API_URL}&q=${encodeURIComponent(city)}`)
.then(response => response.json())
.then(data => [ city, data.main.temp || null ])
// Attach a `.catch()` handler for graceful rejection handling
.catch(() => [ city, null ]);
}
// Use your OpenWeatherMap API KEY
// Set the current weather data API URL
const API_KEY = 'YOUR_API_KEY_HERE';
const API_URL = `https://api.openweathermap.org/data/2.5/weather?appid=${API_KEY}&units=metric`;
// Set the list of cities
const CITIES = [
'London', 'Tokyo', 'Melbourne', 'Vancouver',
'Lagos', 'Berlin', 'Paris', 'Johannesburg',
'Chicago', 'Mumbai', 'Cairo', 'Beijing'
const exchangeRate = currency => {
const symbol = `${BASE_CURRENCY}_${currency}`;
return fetch(`${API_URL}&q=${symbol}`)
.then(response => response.json())
.then(data => [ currency, data[symbol] || null ])
// Attach a `.catch()` handler for graceful rejection handling
.catch(() => [ currency, null ]);
}
// Use your CurrencyConverter API KEY
// Set the currency conversion API URL
const API_KEY = 'YOUR_API_KEY_HERE';
const API_URL = `https://free.currencyconverterapi.com/api/v6/convert?apiKey=${API_KEY}&compact=ultra`;
// Set the base currency (US Dollar in this case)
const BASE_CURRENCY = 'USD';
// Set the list of available currencies
const loadPhoto = photo_url =>
new Promise(resolve => {
const img = new Image();
img.addEventListener('load', () => {
resolve(img);
}, false);
img.addEventListener('error', () => {
const rejection = Promise.reject(new Error('Image could not be loaded.'));
/*******************************************************
Avoid the following style definitions:
- they are ignored by screen readers
- they impact keyboard accessibility
- they affect the visual flow of the page
*******************************************************/
.hidden {
display: none;
}