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
// npm i axios | |
const axios = require('axios'); | |
function getCountryDetails(name) { | |
return axios | |
.get(`https://restcountries.eu/rest/v2/name/${name}`) | |
.then(res => res.data); | |
} |
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
// ************************************************************************************ | |
// https://www.codewars.com/kata/54f9f4d7c41722304e000bbb/javascript | |
// Find the first character that repeats in a String and return that character. | |
// firstDup('tweet') => 't' | |
// firstDup('like') => undefined | |
// This is not the same as finding the character that repeats first. In that case, | |
// an input of 'tweet' would yield 'e'. | |
// ************************************************************************************ |
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
// Sort students by best score and list best 3 as fistPlace, secondPlace, thirdPlace. | |
// Add everyone else in the array. | |
const students = [ | |
{ | |
name: 'ana', | |
score: 5.4 | |
}, | |
{ | |
name: 'ivan', |
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
// Sort students by best score and list best 3 as fistPlace, secondPlace, thirdPlace. | |
// Add everyone else in the array. | |
const students = [ | |
{ | |
name: 'ana', | |
score: 5.4 | |
}, | |
{ | |
name: 'ivan', |
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
// ************************************************ | |
// ************** Rest parameter ****************** | |
// ************************************************ | |
// Reminder: 'arguments' represents a special array-like object that contains all arguments by their index. | |
function add1() { | |
let sum = 0; | |
for (let i = 0; i < arguments.length; i++) { | |
sum += arguments[i]; |
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
// ************************************************ | |
// ************** Spread operator ***************** | |
// ************************************************ | |
// Merging arrays | |
const reptiles = ['snake', 'lizard', 'alligator']; | |
const mammals = ['puppy', 'kitten', 'bunny']; | |
// ES5 approach: |
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
// ************************************************ | |
// ******* Destructuring function parameters ***** | |
// ************************************************ | |
function getFullName(user) { | |
return `${user.firstName} ${user.lastName}`; | |
} | |
getFullName({ firstName: 'ana', lastName: 'martinez' }); // => ana martinez |
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
// ************************************************ | |
// *********** Mixed destructuring **************** | |
// ************************************************ | |
const customer = { | |
name: { | |
firstName: 'ivan', | |
lastName: 'zoro' | |
}, | |
age: 32, |
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
// 1: What would be expected outputs and why? | |
// a: | |
const [a, b] = [1]; | |
console.log(a * b); // <== NaN | |
// b => undefined | |
// 1 * undefined = NaN | |
// b: | |
const [a, b = 1] = [2]; |
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
// 1: What would be expected outputs and why? | |
// a: | |
const [a, b] = [1]; | |
console.log(a * b); // <== ??? | |
// b: | |
const [a, b = 1] = [2]; | |
console.log(a * b); // <== ??? |