Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
be27c06
function playsong
oridavidi100 Sep 8, 2021
e4c5daa
Update index.js
oridavidi100 Sep 8, 2021
fa4cbb2
playsong
oridavidi100 Sep 8, 2021
0b627cc
Update index.js
oridavidi100 Sep 8, 2021
f2fa449
function add song
oridavidi100 Sep 8, 2021
90b6640
Update index.js
oridavidi100 Sep 8, 2021
9d4dc6e
remove song
oridavidi100 Sep 8, 2021
c3986c6
add song function
oridavidi100 Sep 8, 2021
5e9ab69
add song function
oridavidi100 Sep 8, 2021
ce8ee30
add song
oridavidi100 Sep 8, 2021
c399bdb
remove playlists function
oridavidi100 Sep 8, 2021
e8974de
create playlist function
oridavidi100 Sep 8, 2021
28612a5
createPlaylist function
oridavidi100 Sep 8, 2021
42e9847
createPlaylist
oridavidi100 Sep 8, 2021
1dee476
create playlists function
oridavidi100 Sep 8, 2021
572fc1f
playplaylists function
oridavidi100 Sep 8, 2021
7592d22
edit playlists
oridavidi100 Sep 8, 2021
76a00a0
edit playlists function
oridavidi100 Sep 8, 2021
7bd267e
edit playlists function
oridavidi100 Sep 8, 2021
14d480f
edit playlist function
oridavidi100 Sep 8, 2021
ebb8ba2
edit playlists
oridavidi100 Sep 8, 2021
9b5c21f
playlist duration function
oridavidi100 Sep 8, 2021
fcb95be
playlist duration function
oridavidi100 Sep 8, 2021
f2d6f7e
playlistDuration function
oridavidi100 Sep 8, 2021
7c31388
serach by query function
oridavidi100 Sep 8, 2021
94987c7
search by query
oridavidi100 Sep 8, 2021
963a6f3
search by query function
oridavidi100 Sep 8, 2021
3599056
search by query function
oridavidi100 Sep 8, 2021
377c4e4
search by query function
oridavidi100 Sep 8, 2021
86edf4a
fixing problems
oridavidi100 Sep 8, 2021
265cfe8
search by duration function
oridavidi100 Sep 9, 2021
4b89a2e
search by duration
oridavidi100 Sep 9, 2021
b8fe3d0
search by duration
oridavidi100 Sep 9, 2021
9e55382
search by duration function
oridavidi100 Sep 9, 2021
892a376
search by duration
oridavidi100 Sep 9, 2021
1af9de2
search by query
oridavidi100 Sep 11, 2021
7cdcfaa
Update index.js
oridavidi100 Sep 11, 2021
ab2a252
Update README.md
oridavidi100 Sep 11, 2021
22d6b8b
Update index.js
oridavidi100 Sep 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 5 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,5 @@
# MP3 Player - Second Weekend Assignment
You are going to implement an MP3 player.


## Instructions
1. Fork this repo into your account.
2. Clone the forked repo to your computer.
3. Execute `npm install` in the project folder to install the [tests](#testing).
4. Create a new git branch for your work.
5. Complete the project [requirements](#requirements).
6. Remember to push your commits regularly to GitHub.
7. Submit your work (explanation [below](#submission))
8. Good luck & have fun!


## Requirements
The player itself is an object that has:
- `songs`: an array of songs
- `playlists`: an array of playlists
- `playSong`: a method that plays a song.
It receives a song object and should print the following format `"Playing {song.title} from {song.album} by {song.artist} | {song.duration}."` (replace the stuff inside the `{}` with the real values).
The song duration should be in `mm:ss` format (for example 02:40).

A song object has:
- `id`: a unique ID (a number)
- `title`: a title
- `album`: album title
- `artist`: artist name
- `duration`: duration (number, in seconds)

A playlist object has:
- `id`: a unique ID (a number)
- `name`: a name
- `songs`: an array of song IDs

You are asked to implement the following functions:
- `playSong` - Gets a song ID. Uses `player.playSong` to play the song with the given ID.
- `removeSong` - Gets a song ID. Removes the song with the given ID from the player (from songs and playlists).
- `addSong` - Gets a title, album, artist, duration & ID. Adds a new song with given properties to the player. The ID is optional, and if omitted should be automatically generated. The song duration should be in `mm:ss` format (for example 06:27). Returns the ID of the new song.
- `removePlaylist` - Gets a playlist ID. Remove the playlist with the given ID from the player (does not delete the songs inside it).
- `createPlaylist` - Gets a name & ID. Creates a new, empty playlist with the given details. The ID is optional, and if omitted should be automatically generated. Returns the ID of the new playlist.
- `playPlaylist` - Gets a playlist ID. Plays all songs in the specified playlist, in the order the appear in the playlist.
- `editPlaylist` - Gets a playlist ID & a song ID. If the song ID exists in the playlist, removes it. If it was the only song in the playlist, also deletes the playlist. If the song ID does not exist in the playlist, adds it to the end of the playlist.
- `playlistDuration` - Gets a playlist ID. Returns the total duration of the entire playlist with the given ID.
- `searchByQuery` - Gets a query string. Returns a results object, which has:
- `songs`: an array of songs in which either title or album or artist contain the query string. The songs should be sorted by their titles.
- `playlists`: an array of playlists in which the name contains the query string. The playlists should be sorted by their names.

The comparison in both cases should be case-insensitive.
- `searchByDuration` - Gets a duration in `mm:ss` format (for example 11:03). Returns the song, or playlist, with the closest duration to what was given.


## Testing
We have added some automated tests for you to use. They will help you make sure your code covers the requirements.

To run the tests, execute `npm run test` in the project folder.

__Note__: These tests might not cover everything. Don't just count on them. You should remain responsible and vigilant for other possible edge-cases.


## Grading
Your work will be graded based on the following considerations:
- The number of automatic tests you pass
- Readable and ordered code
- Spacing & indentation
- Indicative vairable/function names
- Comments (where necessary)
- Proper use of Git
- Granular commits
- Descriptive commit messages
- Extra features you might have added


## Submission
1. On GitHub, open a pull request from your branch to the main branch.
2. __Do not merge the pull request!__
3. Add the user `Cyber4sPopo` as collaborator to your repo.
4. Submit a link to the pull request in Google Classroom.


## Important Tip
Try to work in small iterations. You've got a big and complex task ahead of you. Break it down into smaller tasks. Concentrate on making small progress every time. Do it step by step.


## Remarks
- The player object must be stored in a global variable called `player`.
- The function, method & property names should be __exactly__ as described above (for the tests to function).
- __Avoid code duplication!__ You are free to add as many extra functions as you like.
- Pay attention to edge-cases! Your code should throw errors when the user tries to do something invalid (delete a song that doesn't exist, etc.).
- You can use all the material you've learned so far, including extras you've learned on your own.
- Write your code in the `index.js` file. It contains a template which you can use as the basis for your code.
this an MP3 with multi stuff u can do.
you can play or add or remove songs .
you can make new playlists with the songs you have in the player.
you can even search songs or playlits by their duration or query .
by ori davidi
221 changes: 208 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,244 @@ const player = {
{ id: 5, name: 'Israeli', songs: [4, 5] },
],
playSong(song) {
console.log(/* your code here */)
console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${durationConvert(song.duration)}.`)
},
}


// convert to mm:ss
function durationConvert(duration)
{
let min = Math.floor(duration / 60);
let sec = duration % 60;

if (min < 10){
min = "0" + String(min);
}
if (sec < 10) {
sec = "0" + String(sec);
}
return min+':'+sec
}

function playSong(id) {
// your code here
for (let song of player.songs){
if (song.id === id)
{
return player.playSong(song);
}
}
throw new Error("No such ID");
}

function removeSong(id) {
// your code here
let a=0;
for(let x=0;x<player.songs.length ;x++){
if (player.songs[x].id===id) a=1}
if (a===0)
throw ("non-existent ID,try another one");
for (let i=0;i<player.songs.length ;i++){
if (player.songs[i].id===id){
player.songs.splice(i,1)
}
}
for (let i=0;i<player.playlists.length;i++){
for (let j=0;j<player.playlists[i].songs.length;j++){
if (player.playlists[i].songs[j]===id)
player.playlists[i].songs.splice(j,1);
}
}
}

function maxID (arr)
{
let max=0;
for (let i = 0; i < arr.length; i++)
{
if (arr[i].id > max)
max = arr[i].id;
}
return max;
}

function addSong(title, album, artist, duration, id) {
// your code here
if (id===undefined)
id=maxID(player.songs)+1;
let time=0;
let a=0;
for(let x=0 ;x<player.songs.length ; x++){
if (player.songs[x].id===id) a=1}
if (a===1)
throw ("existent ID,try another one");
let arr=duration.split(":")
var c = parseInt(arr[0])
var b = parseInt(arr[1])
time =(c*60 + b)
player.songs.push({"id":id,
"title":title,
"album":album,
"artist":artist,
"duration":time
})
return player.songs[player.songs.length-1].id
}

function removePlaylist(id) {
// your code here
let a=0;
for (let i=0;i<player.playlists.length;i++){
if (player.playlists[i].id===id){
a=1
player.playlists.splice(i,1)
}
}
if (a===0) throw ("non-existent id , try another")

}

function createPlaylist(name, id) {
// your code here
function createPlaylist(name, id=10) {
let a=0;
for(let x=0;x<player.playlists.length ;x++){
if (player.playlists[x].id===id) a=1 }
if (a===1) throw ("existent ID,try another one");
player.playlists.push({
"id":id,
"name":name,
"songs":[]
})
return player.playlists[player.playlists.length-1].id
}

function playPlaylist(id) {
// your code here
let a=0;
for (let i=0;i<player.playlists.length;i++){
if (player.playlists[i].id===id){
a=1;
let arr=player.playlists[i].songs;
for (let j=0;j<arr.length;j++){
for (let x=0 ;x<player.songs.length; x++){
if (player.songs[x].id===arr[j])
console.log(player.songs[x])
}
}
}
}
if (a===0)throw ("non-existent id,try another")

}

function editPlaylist(playlistId, songId) {
// your code here
let z=0;
for (let j=0;j<player.songs.length;j++){
if (player.songs[j].id===songId){
z=1;
}
}
if (z===0)throw ("non existing songId please try another")
let a=0;
let y;
let list;
for (let x=0;x<player.playlists.length;x++){
if(player.playlists[x].id===playlistId){
list= player.playlists[x]
}
}
for (let i=0;i<list.songs.length;i++){
if (list.songs[i]===songId&&list.songs.length<2){
a=1;
player.playlists.splice(y,1)}
else if (list.songs[i]===songId&&list.songs.length>1){
a=1;
list.songs.splice(i,1)}
}
if (a===0){ list.songs.push(songId) }
}


function playlistDuration(id) {
// your code here
let sum=0;
let list;
try {for (let x=0;x<player.playlists.length;x++){
if(player.playlists[x].id===id){
list= player.playlists[x]
}
}
for (let i=0;i<list.songs.length;i++){
for (let j=0;j<player.songs.length;j++){
if (list.songs[i]===player.songs[j].id){
sum +=player.songs[j].duration;
continue;
}
}
}
return sum
}
catch{ throw ("non-existent playlist Id please try another")}
}

function searchByQuery(query) {
// your code here
query=query.toLowerCase();
let obj={
songs:[],
playlists:[]
};
for (let song of player.songs)
{
if( (song.title.toLowerCase().includes(query)) ||
(song.album.toLowerCase().includes(query)) ||
(song.artist.toLowerCase().includes(query)) )
{
obj.songs.push(song);
}
}

for (let playlist of player.playlists)
{
if (playlist.name.toLowerCase().includes(query))
{
obj.playlists.push(playlist);
}
}
obj.songs.sort(function (a, b) {
if (a.title < b.title){
return -1; }
else if (a.title > b.title){
return 1; }
return 0;
})
return obj;
}

function close(goal,counts){
var closest = counts.reduce(function(prev, curr) {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});

return (closest);
}

function searchByDuration(duration) {
// your code here
}
let arr=duration.split(":")
var c = parseInt(arr[0])
var b = parseInt(arr[1])
duration =(c*60 + b)
let durArr=[];
let min;
for (let i=0 ; i<player.songs.length ; i++){
durArr.push(player.songs[i].duration)
}
for (let x=0 ; x<player.playlists.length ; x++){
durArr.push(playlistDuration(player.playlists[x].id))}
min=close(duration,durArr)
for (let j=0 ; j<player.songs.length ; j++){
if (player.songs[j].duration===min)
return player.songs[j]
}
for (let n=0 ; n<player.playlists.length ; n++){
if (playlistDuration(player.playlists[n].id)===min)
return player.playlists[n]
}
}


module.exports = {
player,
Expand Down