Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandocanizo/2d3026a448a703e799346c5b9e8e6eaf to your computer and use it in GitHub Desktop.
Save fernandocanizo/2d3026a448a703e799346c5b9e8e6eaf to your computer and use it in GitHub Desktop.
fantasy animals iterator
'use strict';
const randomItem = (itemsArray) => itemsArray[Math.floor(Math.random() * itemsArray.length)];
const makeFantasyAnimal = () => {
const animales = ['perro', 'gato', 'jirafa'];
const verbos = ['escupe', 'caga', 'estornuda'];
const elementos = ['fuego', 'hielo', 'moco'];
return `${randomItem(animales)} ${randomItem(verbos)} ${randomItem(elementos)}`;
};
const buildAnimalArmy = {
[Symbol.iterator]: () => {
return {
next: () => {
const enoughAnimals = Math.random() > .8;
if (enoughAnimals) {
return { done: true };
}
return {
value: makeFantasyAnimal(),
done: false
};
}
};
}
};
for (const fantasyAnimal of buildAnimalArmy) {
console.log(fantasyAnimal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment