diff --git a/Retos/ejercicio-recursividad.js b/Retos/ejercicio-recursividad.js new file mode 100644 index 0000000..cf13687 --- /dev/null +++ b/Retos/ejercicio-recursividad.js @@ -0,0 +1,47 @@ +/** + * @author Dimacros + * + * Problema: + * Las parejas de números que sumen 8; sin usar un loop. + * + * Ejemplo: [3, 6, 2, -6, 10, -2, 0, 8, 4, 4] + * + * return [ [ 6, 2 ], [ 10, -2 ], [ 0, 8 ], [4, 4] ] + */ + +const numbers = [3, 6, 2, -6, 10, -2, 0, 8, 4, 4]; + +function sumPairsEquals(total, numbers) +{ + const maxLenghtAllowed = 2; + + if (numbers.length < maxLenghtAllowed) { + return accumulator(); + } + + [a, b, ...etc] = numbers; + + if (a + b === total) { + accumulator().push([a, b]); + } + + return sumPairsEquals(total, reduceLenght(numbers, 1)); +} + +function reduceLenght(numbers, length) +{ + return numbers.slice(length); +} + +function accumulator() +{ + if( typeof accumulator.add === 'undefined' ) { + accumulator.add = []; + } + + return accumulator.add; +} + +console.log( + sumPairsEquals(8, numbers) +); diff --git a/Retos/sesion3-reto1/app/Album.js b/Retos/sesion3-reto1/app/Album.js new file mode 100644 index 0000000..74d8cf8 --- /dev/null +++ b/Retos/sesion3-reto1/app/Album.js @@ -0,0 +1,12 @@ +class Album +{ + /** + * @param {String} title + */ + constructor(title) + { + this.title = title; + } +} + +module.exports = Album; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/Artist.js b/Retos/sesion3-reto1/app/Artist.js new file mode 100644 index 0000000..7948437 --- /dev/null +++ b/Retos/sesion3-reto1/app/Artist.js @@ -0,0 +1,12 @@ +class Artist +{ + /** + * @param {String} name + */ + constructor(name) + { + this.name = name; + } +} + +module.exports = Artist; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/Factories/SongFactory.js b/Retos/sesion3-reto1/app/Factories/SongFactory.js new file mode 100644 index 0000000..2cb9855 --- /dev/null +++ b/Retos/sesion3-reto1/app/Factories/SongFactory.js @@ -0,0 +1,16 @@ +const Artist = require("../Artist"); +const Album = require("../Album"); +const Song = require("../Song"); + +class SongFactory +{ + create({artist: { name: artistName}, album: { title: albumTitle}, title, durationInSeconds}) + { + const artist = new Artist(artistName); + const album = new Album(albumTitle); + + return new Song(artist, album, title, durationInSeconds); + } +} + +module.exports = SongFactory; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/Factories/UserFactory.js b/Retos/sesion3-reto1/app/Factories/UserFactory.js new file mode 100644 index 0000000..1146300 --- /dev/null +++ b/Retos/sesion3-reto1/app/Factories/UserFactory.js @@ -0,0 +1,11 @@ +const User = require('../User'); + +class UserFactory +{ + create(username) + { + return new User(username); + } +} + +module.exports = UserFactory; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/PlayList.js b/Retos/sesion3-reto1/app/PlayList.js new file mode 100644 index 0000000..8b9b699 --- /dev/null +++ b/Retos/sesion3-reto1/app/PlayList.js @@ -0,0 +1,22 @@ +class PlayList +{ + /** + * @param {String} title + */ + constructor(title) + { + this.title = title; + this.songs = new Array; + } + + /** + * + * @param {Song} song + */ + addSong(song) + { + this.songs.push(song); + } +} + +module.exports = PlayList; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/Song.js b/Retos/sesion3-reto1/app/Song.js new file mode 100644 index 0000000..e3d03d9 --- /dev/null +++ b/Retos/sesion3-reto1/app/Song.js @@ -0,0 +1,19 @@ +class Song +{ + /** + * + * @param {Artist} artist + * @param {Album} album + * @param {String} title + * @param {Number} durationInSeconds + */ + constructor(artist, album, title, durationInSeconds) + { + this.artist = artist; + this.album = album; + this.title = title; + this.durationInSeconds = durationInSeconds; + } +} + +module.exports = Song; \ No newline at end of file diff --git a/Retos/sesion3-reto1/app/User.js b/Retos/sesion3-reto1/app/User.js new file mode 100644 index 0000000..2b05ec8 --- /dev/null +++ b/Retos/sesion3-reto1/app/User.js @@ -0,0 +1,36 @@ +const PlayList = require("./PlayList"); + +class User +{ + /** + * @param {String} username + */ + constructor(username) + { + this.username = username; + this.playLists = new Array; + } + + /** + * @param {String} title + */ + addPlayList(title) + { + this.playLists.push(new PlayList(title)); + } + + /** + * @param {String} title + */ + myPlayList(title) + { + return this.playLists.find(playList => playList.title === title); + } + + myPlayLists() + { + return this.playLists; + } +} + +module.exports = User; \ No newline at end of file diff --git a/Retos/sesion3-reto1/bootstrap/app.js b/Retos/sesion3-reto1/bootstrap/app.js new file mode 100644 index 0000000..728740b --- /dev/null +++ b/Retos/sesion3-reto1/bootstrap/app.js @@ -0,0 +1,49 @@ +const SongFactory = require("../app/Factories/SongFactory"); +const UserFactory = require("../app/Factories/UserFactory"); + +class Spotify +{ + /** + * @param {SongFactory} songFactory + * @param {UserFactory} UserFactory + */ + constructor(songFactory, userFactory) + { + this.songFactory = songFactory; + this.userFactory = userFactory; + this.songs = new Array; + this.users = new Array; + } + + createSong(song) + { + this.songs.push(this.songFactory.create(song)); + } + + createUser(username) + { + this.users.push(this.userFactory.create(username)); + } + + findUserByUsername(username) + { + return this.users.find(user => user.username === username); + } + + findSongByTitle(title) + { + return this.songs.find(song => song.title === title); + } + + allSongs() + { + return this.songs; + } + + allUsers() + { + return this.users; + } +} + +module.exports = new Spotify(new SongFactory, new UserFactory); \ No newline at end of file diff --git a/Retos/sesion3-reto1/data.json b/Retos/sesion3-reto1/data.json new file mode 100644 index 0000000..201bf72 --- /dev/null +++ b/Retos/sesion3-reto1/data.json @@ -0,0 +1,52 @@ +[ + { + "artist": { + "name": "PXNDX" + }, + "album": { + "title": "-" + }, + "title": "Los Malaventurados lo lloran", + "durationInSeconds": 300 + }, + { + "artist": { + "name": "Tragokorto" + }, + "album": { + "title": "¿A dónde vas?" + }, + "title": "Borracho en el bar", + "durationInSeconds": 300 + }, + { + "artist": { + "name": "Los Violadores" + }, + "album": { + "title": "-" + }, + "title": "Sentimiento Fatal", + "durationInSeconds": 300 + }, + { + "artist": { + "name": "" + }, + "album": { + "title": "" + }, + "title": "", + "durationInSeconds": 300 + }, + { + "artist": { + "name": "" + }, + "album": { + "title": "" + }, + "title": "", + "durationInSeconds": 300 + } +] \ No newline at end of file diff --git a/Retos/sesion3-reto1/instrucciones.js b/Retos/sesion3-reto1/instrucciones.js index 3e2793e..d74cc2d 100644 --- a/Retos/sesion3-reto1/instrucciones.js +++ b/Retos/sesion3-reto1/instrucciones.js @@ -10,50 +10,35 @@ * * La cantidad y complejidad de las estructuras de datos es libre. * - * */ + */ -// Codigo Referencia +const spotify = require('./bootstrap/app'); +const data = require('./data.json'); -class Autor { - constructor(nombre, pais) { - this.nombre = nombre; - this.pais = pais; - } -} +spotify.createUser('dimacros'); -class Cancion { - constructor(titulo, duracion, autor) { - this.titulo = titulo; - this.duracion = duracion; - this.autor = new Autor(autor.nombre, autor.pais); - } -} +data.forEach(song => { + spotify.createSong(song); +}); -class db { - constructor(canciones) { - this.canciones = canciones; - } -} +const dimacros = spotify.findUserByUsername('dimacros'); -let SpotifyDB; +dimacros.addPlayList('Rock'); +dimacros.addPlayList('Electro'); +dimacros.addPlayList('Salsa'); -function Spotify () { - return { - init: function () { - SpotifyDB = new db([]); - }, - agregarCancion: function (titulo, duracion, autor) { - SpotifyDB.canciones.push(new Cancion(titulo, duracion, autor)); - } - } -} +dimacros.myPlayList('Rock').addSong( + spotify.findSongByTitle('Los Malaventurados lo lloran') +); -let spotifyInstance = Spotify(); +dimacros.myPlayList('Rock').addSong( + spotify.findSongByTitle('Borracho en el bar') +); -spotifyInstance.init(); +dimacros.myPlayList('Rock').addSong( + spotify.findSongByTitle('Sentimiento Fatal') +); -spotifyInstance.agregarCancion('cancion uno', 4.3, {nombre: 'tanto', pais: 'tanto'}); -spotifyInstance.agregarCancion('cancion dos', 4.3, {nombre: 'tanto', pais: 'tanto'}); - - -console.log(SpotifyDB); \ No newline at end of file +console.log( + dimacros.myPlayList('Rock') +); \ No newline at end of file