Skip to content

Instantly share code, notes, and snippets.

@merunga
Last active May 4, 2018 05:23
Show Gist options
  • Save merunga/0f4164f46f45fb54ec2302d25107f44a to your computer and use it in GitHub Desktop.
Save merunga/0f4164f46f45fb54ec2302d25107f44a to your computer and use it in GitHub Desktop.
propuesta contenido arrays
// Extraido de
// https://github.com/wesbos/JavaScript30/blob/master/04%20-%20Array%20Cardio%20Day%201/index-FINISHED.html
/* global describe it */
// Usaremos el `assert` de node
const assert = require('assert');
// ## Unos datos que usaremos para nuestros ejemplos
// ### Inventores
const inventors = [
{
first: 'Albert',
last: 'Einstein',
year: 1879,
passed: 1955,
},
{
first: 'Isaac',
last: 'Newton',
year: 1643,
passed: 1727,
},
{
first: 'Galileo',
last: 'Galilei',
year: 1564,
passed: 1642,
},
{
first: 'Marie',
last: 'Curie',
year: 1867,
passed: 1934,
},
{
first: 'Johannes',
last: 'Kepler',
year: 1571,
passed: 1630,
},
{
first: 'Nicolaus',
last: 'Copernicus',
year: 1473,
passed: 1543,
},
{
first: 'Max',
last: 'Planck',
year: 1858,
passed: 1947,
},
{
first: 'Katherine',
last: 'Blodgett',
year: 1898,
passed: 1979,
},
{
first: 'Ada',
last: 'Lovelace',
year: 1815,
passed: 1852,
},
{
first: 'Sarah E.',
last: 'Goode',
year: 1855,
passed: 1905,
},
{
first: 'Lise',
last: 'Meitner',
year: 1878,
passed: 1968,
},
{
first: 'Hanna',
last: 'Hammarström',
year: 1829,
passed: 1909,
},
];
// ## Personas
const people = [
'Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel',
'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig',
'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul',
'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David',
'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester',
'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd',
'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano',
'Berle, Milton', 'Berlin, Irving', 'Berne, Eric',
'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle',
'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin',
'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose',
'Biko, Steve', 'Billings, Josh', 'Biondo, Frank',
'Birrell, Augustine', 'Black Elk', 'Blair, Robert',
'Blair, Tony', 'Blake, William',
];
// # Array.prototype.filter()
describe('Array.prototype.filter()', () => {
// ## 1. Buscar los inventares nacidos en el siglo XVI
it('1. Buscar los inventares nacidos en el siglo XVI', () => {
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
return assert.deepStrictEqual(
fifteen,
[
{
first: 'Galileo',
last: 'Galilei',
year: 1564,
passed: 1642,
},
{
first: 'Johannes',
last: 'Kepler',
year: 1571,
passed: 1630,
},
],
);
});
});
// # Array.prototype.map()
describe('Array.prototype.map()', () => {
// ## 2. Construir un array de Strings que contenga nombre y apellido de los inventores
it('2. Construir un array de Strings que contenga nombre y apellido de los inventores', () => {
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
return assert.deepStrictEqual(
fullNames,
[
'Albert Einstein',
'Isaac Newton',
'Galileo Galilei',
'Marie Curie',
'Johannes Kepler',
'Nicolaus Copernicus',
'Max Planck',
'Katherine Blodgett',
'Ada Lovelace',
'Sarah E. Goode',
'Lise Meitner',
'Hanna Hammarström',
],
);
});
});
// # Array.prototype.sort()
describe('Array.prototype.sort()', () => {
// ## 3. Sort the inventors by birthdate, oldest to youngest
// Este es el resultado esperado
const oldestToYoungest = [
{
first: 'Nicolaus',
last: 'Copernicus',
passed: 1543,
year: 1473,
},
{
first: 'Galileo',
last: 'Galilei',
passed: 1642,
year: 1564,
},
{
first: 'Johannes',
last: 'Kepler',
passed: 1630,
year: 1571,
},
{
first: 'Isaac',
last: 'Newton',
passed: 1727,
year: 1643,
},
{
first: 'Ada',
last: 'Lovelace',
passed: 1852,
year: 1815,
},
{
first: 'Hanna',
last: 'Hammarström',
passed: 1909,
year: 1829,
},
{
first: 'Sarah E.',
last: 'Goode',
passed: 1905,
year: 1855,
},
{
first: 'Max',
last: 'Planck',
passed: 1947,
year: 1858,
},
{
first: 'Marie',
last: 'Curie',
passed: 1934,
year: 1867,
},
{
first: 'Lise',
last: 'Meitner',
passed: 1968,
year: 1878,
},
{
first: 'Albert',
last: 'Einstein',
passed: 1955,
year: 1879,
},
{
first: 'Katherine',
last: 'Blodgett',
passed: 1979,
year: 1898,
},
];
it('3. Ordenar a los inventores del mas viejo al mas joven', () => {
const ordered = inventors.sort((a, b) => {
if (a.year > b.year) {
return 1;
}
return -1;
});
return assert.deepStrictEqual(
ordered,
oldestToYoungest,
);
});
// ### Ahora usando el *operador ternario*
it('3.a Ordenar a los inventores del mas viejo al mas joven, usando el operador ternario', () => {
const ordered = inventors.sort((a, b) => (a.year > b.year ? 1 : -1));
return assert.deepStrictEqual(
ordered,
oldestToYoungest,
);
});
// ## 4. Ordenar a los inventores, según la cantidad de años vividos
it('4. Ordenar a los inventores, según la cantidad de años vividos', () => {
const oldest = inventors.sort((a, b) => {
const lastInventor = a.passed - a.year;
const nextInventor = b.passed - b.year;
return lastInventor > nextInventor ? -1 : 1;
});
return assert.deepStrictEqual(
oldest,
[
{
first: 'Lise',
last: 'Meitner',
passed: 1968,
year: 1878,
},
{
first: 'Max',
last: 'Planck',
passed: 1947,
year: 1858,
},
{
first: 'Isaac',
last: 'Newton',
passed: 1727,
year: 1643,
},
{
first: 'Katherine',
last: 'Blodgett',
passed: 1979,
year: 1898,
},
{
first: 'Hanna',
last: 'Hammarström',
passed: 1909,
year: 1829,
},
{
first: 'Galileo',
last: 'Galilei',
passed: 1642,
year: 1564,
},
{
first: 'Albert',
last: 'Einstein',
passed: 1955,
year: 1879,
},
{
first: 'Nicolaus',
last: 'Copernicus',
passed: 1543,
year: 1473,
},
{
first: 'Marie',
last: 'Curie',
passed: 1934,
year: 1867,
},
{
first: 'Johannes',
last: 'Kepler',
passed: 1630,
year: 1571,
},
{
first: 'Sarah E.',
last: 'Goode',
passed: 1905,
year: 1855,
},
{
first: 'Ada',
last: 'Lovelace',
passed: 1852,
year: 1815,
},
],
);
});
// ## 5. Ordenar a los inventores, según la fechade nacimiento de mas viejo a mas joven
it('5. Ordenar a los inventores, según la fechade nacimiento de mas viejo a mas joven', () => {
const alpha = people.sort((lastOne, nextOne) => {
/* eslint-disable no-unused-vars */
const [aLast, aFirst] = lastOne.split(', ');
const [bLast, bFirst] = nextOne.split(', ');
/* eslint-enable no-unused-wars */
return aLast > bLast ? 1 : -1;
});
assert.deepStrictEqual(
alpha,
[
'Beck, Glenn',
'Becker, Carl',
'Beckett, Samuel',
'Beddoes, Mick',
'Beecher, Henry',
'Beethoven, Ludwig',
'Begin, Menachem',
'Belloc, Hilaire',
'Bellow, Saul',
'Ben-Gurion, David',
'Benchley, Robert',
'Benenson, Peter',
'Benjamin, Walter',
'Benn, Tony',
'Bennington, Chester',
'Benson, Leana',
'Bent, Silas',
'Bentsen, Lloyd',
'Berger, Ric',
'Bergman, Ingmar',
'Berio, Luciano',
'Berle, Milton',
'Berlin, Irving',
'Berne, Eric',
'Bernhard, Sandra',
'Berra, Yogi',
'Berry, Halle',
'Berry, Wendell',
'Bethea, Erin',
'Bevan, Aneurin',
'Bevel, Ken',
'Biden, Joseph',
'Bierce, Ambrose',
'Biko, Steve',
'Billings, Josh',
'Biondo, Frank',
'Birrell, Augustine',
'Black Elk',
'Blair, Robert',
'Blair, Tony',
'Blake, William',
],
);
});
});
// # Array.prototype.reduce()
describe('Array.prototype.reduce()', () => {
// ## 7. Calcular la cantidad de años vividos, de todos los inventores sumados
it('7. Calcular la cantidad de años vividos, de todos los inventores sumados', () => {
const totalYears = inventors
.reduce((total, inventor) => total + (inventor.passed - inventor.year), 0);
return assert.equal(totalYears, 861);
});
// ## 8. Sumar la cantidad de apariciones de cada medio de transporte
it('8. Sumar la cantidad de apariciones de cada medio de transporte', () => {
const data = [
'car', 'car', 'truck', 'truck', 'bike', 'walk',
'car', 'van', 'bike', 'walk', 'car', 'van',
'car', 'truck',
];
const transportation = data.reduce((obj, item) => {
const result = {
...obj,
};
if (!obj[item]) {
result[item] = 0;
}
result[item] += 1;
return result;
}, {});
assert.deepStrictEqual(
transportation,
{
car: 5, truck: 3, bike: 2, walk: 2, van: 2,
},
);
});
// ## 8.a ahora agregando un nuevo medio de transporte (pogostick)
it('8.a ahora agregando un nuevo medio de transporte (pogostick)', () => {
const data = [
'car', 'car', 'truck', 'truck', 'bike', 'walk',
'car', 'van', 'bike', 'walk', 'car', 'van',
'car', 'truck', 'pogostick',
];
const transportation = data.reduce((obj, item) => {
const result = {
...obj,
};
if (!obj[item]) {
result[item] = 0;
}
result[item] += 1;
return result;
}, {});
assert.deepStrictEqual(
transportation,
{
car: 5, truck: 3, bike: 2, walk: 2, van: 2, pogostick: 1,
},
);
});
});
{
"name": "arrays",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha *.js",
"lint": "eslint *.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.4.0",
"mocha": "^5.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment