Playground - Crea un stack para una playlist
Clase 90 de 99 • 30 días de JavaScript
Contenido del curso
Clase 90 de 99 • 30 días de JavaScript
Contenido del curso
Abril Darynka Tapia Sosa
Rubén Hernández Hernández
Angel Javier Sanchez Tenjo
Nicolás Felipe Pinto Vega
Carina Payleman
Victor Ortiz
Elias Rayas Gonzalez
Alejandro Anaya
Gabriel Luna
JAIME EDUARDO DIAZ TOBON
Victor Hernandez
David Esteban Giraldo Duque
Andres Eduardo Maneiro Antunez
Harold Zurita Simon
Alexis Corrales
Raul Carrillo Garrido
Agustin Choque Veliz
Fabrizio López
Israel Blas
David Ochoa
Hola, les dejo mi solución . . . . . . . . . .
. . . .
export class Node { constructor(value) { // Tu código aquí 👈🏻 this.value = value; this.next = null; } } export class Playlist { constructor() { // Tu código aquí 👈🏻 this.top = null; this.bottom = null; this.length = 0; } addSong(song) { // Tu código aquí 👈🏻 const newNode = new Node(song); if (!this.top) { this.top = newNode; this.bottom = newNode; } else { newNode.next = this.top; this.top = newNode; } this.length++; } playSong() { // Tu código aquí 👈🏻 if (!this.top) { throw new Error("No hay canciones en la playlist") } const currentSong = this.top.value; if (this.top === this.bottom) { this.bottom = null; } this.top = this.top.next; this.length--; return currentSong; } getPlaylist() { // Tu código aquí 👈🏻 let currentNode = this.top; let currentNodeArray = []; for (let i = 0; i < this.length; i++) { currentNodeArray.push(currentNode.value) currentNode = currentNode.next; } return currentNodeArray; } }
💚Mi Solución💚
🛡️Escudo Anti-spoilers🛡️
!catmusic
👾Código
import { Node } from "./node"; export class Playlist { constructor() { this.top = null this.bottom = null this.length = 0 } addSong(song) { const newNode = new Node(song) if (this.length == 0) { this.top = newNode this.bottom = newNode } else { newNode.next = this.top this.top = newNode } this.length++ return this } playSong() { if (this.length == 0) throw new Error("No hay canciones en la lista") const song = this.top.value if (this.length != 1) { this.top = this.top.next } else { this.top = null this.bottom = null } this.length-- return song } getPlaylist() { let playlist = [] let actualSong = this.top while (actualSong != null) { playlist.push(actualSong.value) actualSong = actualSong.next } return playlist } }
Hola mi aporte:
✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
node.js
export class Node { constructor(value) { this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (this.length === 0) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; } playSong() { if (this.length === 0) { throw new Error("No hay canciones en la playlist"); } const actualSong = this.top.value; if (this.top === this.bottom) { this.bottom = null; } this.top = this.top.next; this.length--; return actualSong; } getPlaylist() { const songs = []; let current = this.top; while (current) { songs.push(current.value); current = current.next; } return songs; } }
Aqui esta mi solución: . . . . . . . . . . . . . . .
//node.js
export class Node { constructor(value) { // Tu código aquí 👈🏻 this.value = value; this.next = null; } }
//exercise.js
import { Node } from "./node"; export class Playlist { constructor() { // Tu código aquí 👈🏻 this.top = null; this.bottom = null; this.length = 0; } addSong(song) { // Tu código aquí 👈🏻 const newSong = new Node(song); if (!this.top) { this.top = newSong; this.bottom = newSong } else { newSong.next = this.top; this.top = newSong; } this.length++; } playSong() { // Tu código aquí 👈🏻 if (!this.top) throw new Error('No hay canciones en la lista'); if (this.top === this.bottom) this.bottom = null; const playedSong = this.top; this.top = this.top.next; this.length--; return playedSong.value; } getPlaylist() { // Tu código aquí 👈🏻 const playList = new Array(); let currentSong = this.top; while (currentSong) { playList.push(currentSong.value); currentSong = currentSong.next; } return playList; } }
node.js
export class Node { constructor(value) { this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (!this.top) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; } playSong() { if (!this.top) { throw new Error("No hay canciones en la lista."); } if (this.top === this.bottom) { this.bottom = null; } let song = this.top.value; this.top = this.top.next; this.length--; return song; } getPlaylist() { let currentSong = this.top; const arrayPlaylist = []; while (currentSong) { arrayPlaylist.push(currentSong.value); currentSong = currentSong.next; } return arrayPlaylist; } }
Este si estuvo mas fácil, aunque siempre tiene que ser realizado bajo las condiciones ya programadas, solo por una variable que cambie de nombre no me pasaba, pero en fin mi solución . . . . . . . . . . . . . . . . .
node.js
export class Node { constructor(value) { // Tu código aquí 👈🏻 this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { // Tu código aquí 👈🏻 this.top = null; this.bottom = null; this.length = 0; } addSong(song) { // Tu código aquí 👈🏻 const newSong = new Node(song); if (!this.top) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; return this; } playSong() { // Tu código aquí 👈🏻 if (!this.top) throw new Error('No hay caciones en la lista'); if (this.top === this.bottom) this.bottom = null; let song = this.top.value; this.top = this.top.next; this.length--; return song; } getPlaylist() { // Tu código aquí 👈🏻 let lSong = this.top; const listSongs = []; while (lSong) { listSongs.push(lSong.value); lSong = lSong.next; } return listSongs; } }
Solución
class Node { constructor(value) { this.value = value; this.next = null; } } class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (this.length === 0) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; return this; } playSong() { if (this.length === 0) return null; const deletedSong = this.top.value; if (this.length === 1) { this.top = null; this.bottom = null; } else { this.top = this.top.next; } this.length--; console.log(deletedSong); return this; } getPlaylist() { let currentNode = this.top; const array = []; while (currentNode) { array.push(currentNode.value); currentNode = currentNode.next; } console.log(array); return array; } }
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
import { Node } from "./node"; export class Playlist { constructor() { this.top = null this.bottom = null this.length = 0 } addSong(song) { let newNode = new Node(song) if (this.length === 0) { this.top = newNode this.bottom = newNode } else { newNode.next = this.top this.top = newNode } this.length++ return this } playSong() { if (this.length <= 0) throw new Error("No hay canciones en la lista") if (this.top === this.bottom) { this.bottom = null; } let play = this.top.value this.top = this.top.next this.length-- return play } getPlaylist() { let playlistArray = [] let currentNode = this.top while (currentNode) { playlistArray.push(currentNode.value) currentNode = currentNode.next } return playlistArray } }
Aquí mi solución: . . . . . . . . . . . . node.js
export class Node { constructor(value) { this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null this.bottom = null this.length = 0 } addSong(song) { const newNode = new Node(song); if (this.length === 0) { this.top = newNode; this.bottom = newNode; } else { newNode.next = this.top; this.top = newNode; } this.length++; } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist"); } if (this.top === this.bottom) { this.bottom = null; } let ssong = this.top.value; this.top = this.top.next; this.length--; return ssong; } getPlaylist() { let playList = []; if (this.top) { let currentNode = this.top; while (currentNode) { playList.push(currentNode.value); currentNode = currentNode.next; } } return playList; } }
++MI APORTE++ 💪 . . . . . . . . . . . . . . . . . . . . . . . . . . class Node
export class Node { constructor(value) { this.value = value; this.next = null; } }
class Playlist
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newNode = new Node(song); if (this.length === 0) { this.top = newNode; this.bottom = newNode; } else { newNode.next = this.top; this.top = newNode; } this.length++; } playSong() { if (!this.top) throw new Error("No hay canciones en la lista"); if (this.top === this.bottom) { this.bottom = null; } const song = this.top.value; this.top = this.top.next; this.length--; return song; } getPlaylist() { const songs = []; let currentNode = this.top; for (let i = 0; i < this.length; i++) { songs.push(currentNode.value); currentNode = currentNode.next; } return songs; } }
node.js
export class Node { constructor(value) { this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newNode = new Node(song); if (this.length === 0) { this.top = newNode; this.bottom = newNode; } else { const pointer = this.top; this.top = newNode; this.top.next = pointer; } this.length++; return this; } playSong() { if (this.length === 0) { throw new Error("No hay canciones en la lista"); } if (this.top === this.bottom) { this.bottom = null; } const value = this.top.value; this.top = this.top.next; this.length--; return value; } getPlaylist() { if (this.length === 0) { return []; } const array = []; let node = this.top; array.push( this.top.value) while (node.next) { array.push(node.next.value); node = node.next; } return array; } }
Mi solución: . . . . . . . .
node.js
export class Node { constructor(value) { this.value = value; this.next = null; } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null this.bottom = null this.length = 0 } addSong(song) { const newSong = new Node(song) if (this.length === 0) { this.top = newSong this.bottom = newSong } else { newSong.next = this.top this.top = newSong } this.length++ } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist") } const currentSong = this.top if (currentSong === this.bottom) { this.bottom = null } this.top = this.top.next this.length-- return currentSong.value } getPlaylist() { let array = [] let currentSong = this.top while (currentSong) { array.push(currentSong.value) currentSong = currentSong.next } return array } }
Les comparte mi humilde solucion . . . . . . . . . .. . . . . . ..
export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (this.length === 0) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist"); } const current = this.top.value if (this.top === this.bottom) { this.bottom = null } this.top = this.top.next this.length-- return current } getPlaylist() { const playlist = [] let current = this.top while (current) { playlist.push(current.value) current = current.next } return playlist } }
Solución… 😄 . . . .
export class Node { constructor(value) { this.value = value; this.next = null; } }
. exercise.js:
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { let newSong = new Node(song); if (!this.top) { this.bottom = newSong; this.top = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; return this; } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist"); } if (this.top === this.bottom) { this.bottom = null; } let actualSong = this.top.value; this.top = this.top.next; this.length--; return actualSong; } getPlaylist() { let list = []; let actual = this.top; while (actual) { list.push(actual.value); actual = actual.next; } return list; } }
export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newNode = new Node(song); if (this.length === 0) { this.top = newNode; this.bottom = newNode; } else { newNode.next = this.top; this.top = newNode; } this.length++; } playSong() { if (this.length === 0) { throw new Error("No hay canciones en la playlist"); } const song = this.top.value; if (this.top === this.bottom) { this.bottom = null; } this.top = this.top.next; this.length--; return song; } getPlaylist() { const playlist = []; let currentNode = this.top; while (currentNode) { playlist.push(currentNode.value); currentNode = currentNode.next; } return playlist; } }
El error de "Eliminar el siguiente al reproducir" es demasiado criptico, me ha costado un rato y estar apunto de tirar la toalle el darme cuenta de que lo que queria decir en realidad es "si solo queda un elemento en la lista, poner botttom a null. . . . . . . . . . . . .
export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newNode = { value: song, next: null } if (!this.bottom) { this.bottom = newNode; this.top = newNode; } else { const prevTop = this.top; this.top = newNode; this.top.next = prevTop; } this.length++; } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist"); } if (this.top === this.bottom) { this.bottom = null; } const currentTop = this.top.value; this.top = this.top.next; this.length--; return currentTop; } getPlaylist() { const playlistArr = []; let currentNode = this.top; while (currentNode) { playlistArr.push(currentNode.value); currentNode = currentNode.next; } return playlistArr; } }
Mi solución: . . . . . . . . . . código node.js:
export class Node { constructor(value) { this.value = value; this.next = null; } }
código exercise.js:
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newNode = new Node(song); if (this.length === 0) { this.top = newNode; this.bottom = newNode; } else { newNode.next = this.top; this.top = newNode; } this.length++; return this } playSong() { if (this.length == 0) { throw new Error("No hay canciones en la playlist"); } const songTop = this.top.value; if (this.top === this.bottom) { this.bottom = null; } this.top = this.top.next; this.length--; return songTop; } getPlaylist() { let playList = []; let currentNode = this.top; while (currentNode) { playList.push(currentNode.value); currentNode = currentNode.next; } return playList; } }
Acá mi solución, muy parecida a la del resto. Ojo que el error que se debe mostrar no es "No hay canciones en la lista", sino "No hay canciones en la playlist". Ahora si la solución: . . . . . . . . . .
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (!this.top) { this.top = newSong; this.bottom = newSong; } else { newSong.next = this.top; this.top = newSong; } this.length++; return this; } playSong() { if (!this.top) throw new Error("No hay canciones en la playlist"); const lastSong = this.top; this.top = this.top.next; if (this.length === 1) this.bottom = null; this.length--; return lastSong.value; } getPlaylist() { if (!this.top) return []; let currentSong = this.top; const playList = []; playList.push(this.top.value); while (currentSong.next) { playList.push(currentSong.next.value); currentSong = currentSong.next; } return playList; } }
Resolución del ejercicio jejej
. . . . . . . . . . . .
import { Node } from "./node"; export class Playlist { constructor() { this.top = null this.bottom = null this.length = 0 } addSong(song) { const newSong = new Node(song) if (this.length === 0) { this.top = newSong this.bottom = newSong } else { newSong.next = this.top this.top = newSong } this.length++ return this } playSong() { if (this.length === 0) throw new Error("No hay canciones en la playlist") const removeSong = this.top this.top = this.top.next if (this.length === 1) this.bottom = null this.length-- return removeSong.value } getPlaylist() { let count = this.length const arraySongs = [] let tempSong = this.top while (count > 0) { arraySongs.push(tempSong.value) tempSong = tempSong.next count-- } return arraySongs } }
🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️
Crea un stack para una playlist
!Spoiler Shield
Nunca pares de aprender 🦾
Node.js
export class Node { constructor(value) { this.value = value this.next = null } }
exercise.js
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (!this.top) { this.top = newSong this.bottom = newSong console.log(this); } else { newSong.next = this.top this.top = newSong } this.length++; return this } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist") } if (this.top == this.bottom) { this.bottom = null } const playedSong = this.top.value this.top = this.top.next this.length--; return playedSong } getPlaylist() { const playList = [] if (!this.top) { return [] } if (this.top) { playList.push(this.top.value) console.log(this.top.value) let currentSong = this.top while (currentSong.next) { playList.push(currentSong.next.value) currentSong = currentSong.next } } return playList } }