Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 11 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
# 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!

So it is my second Weekend Task For Cyber4s. In this task I make simple player of music. The player have some methods and also many functions that help to user to add or remove or edit playlists or songs. In my work a tried to work effictly and avoid code duplications and improve work of the player. I use only JavaScript in this work also many syntax of ES6 in my project

## 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)
Expand All @@ -33,7 +19,15 @@ A playlist object has:
- `name`: a name
- `songs`: an array of song IDs

You are asked to implement the following functions:
## Functions
It is function that I have:
-`toCorrectDuration` - Gets a duration of the song in the seconds format. Transform seconds format in something more redble for people mm:ss format.
- `songById` - Gets a song ID. Check if Exist song with this ID Throw Erroo if not. Create a object of needed song.
- `playlistById` - Gets a playlist ID. Check if Exist playlist with this ID Throw Erroo if not. Create a object of needed playlist.
- `songIndex` - Gets a song object. Return index of this song in Array of songs of the player.
- `playlistIndex` - Gets a playlist object. Return index of this playlist in Array of plaulists of the player.
- `newId` - Gets a array of objects. Check all objects in the array and search the biggest id number. After that return maxId+1 that take me new id number that don`t used.
- `durationToSeconds` - Gets a duration in format mm:ss. And transform format to more useful for computer seconds format.
- `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.
Expand All @@ -49,43 +43,5 @@ You are asked to implement the following functions:
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.

## Link to master repository and task https://github.com/suvelocity/Mp3PlayerTask

## 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.
153 changes: 140 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,175 @@ 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+" | "+toCorrectDuration(song.duration)+".")
},
}

function toCorrectDuration(seconds){ //transform duration for people
let mm;
if(Math.floor(seconds/60)<10) mm=`0${Math.floor(seconds/60)}`;
else mm=`${Math.floor(seconds/60)}`;
let ss;
if(Math.floor(seconds%60)<10) ss=`0${Math.floor(seconds%60)}`;
else ss=`${Math.floor(seconds%60)}`;
return mm+":"+ss;
}

function songById(id){
let songObj=player.songs.find(x=> x.id===id);
if (songObj===undefined){
throw "Not a Valid ID"
}
return songObj;
}

function playlistById(id){
let playlistObj=player.playlists.find(x=> x.id===id);
if (playlistObj===undefined){
throw "Not a Valid ID"
}
return playlistObj;
}

function songIndex(song){
let index=player.songs.indexOf(song);
return index
}

function playlistIndex(playlist){
let index=player.playlists.indexOf(playlist);
return index;
}

function newId(obj){ //I take the next max id
let maxId=0;
for (let i of obj){
if(i.id>maxId) maxId=i.id;
}
return maxId+1;
}

function durationToSeconds(str){
let duration=(parseInt(str.slice(0,2))*60)+parseInt(str.slice((str.length-2),(str.length)));
return duration;
}

function playSong(id) {
// your code here
let songObj=songById(id);
player.playSong(songObj);
}

function removeSong(id) {
// your code here
let songId=songIndex(songById(id));
player.songs.splice(songId,1)
for(let list of player.playlists){
list.songs.splice(list.songs.indexOf(id),1)
}
}

function addSong(title, album, artist, duration, id) {
// your code here
function addSong(title, album, artist, duration, id=newId(player.songs)) {
if(player.songs.find(x=> x.id===id)){
throw "ID is taken"
}
let obj={
id: id,
title: title,
album: album,
artist: artist,
duration: durationToSeconds(duration) ,
}
player.songs.push(obj)
return id;
}

function removePlaylist(id) {
// your code here
let playlistId=playlistIndex(playlistById(id));
player.playlists.splice(playlistId,1)
}

function createPlaylist(name, id) {
// your code here
function createPlaylist(name, id=newId(player.playlists)) {
if(player.playlists.find(x=> x.id===id)){
throw "ID is taken"
}
let obj={
id: id,
name: name,
songs: []
}
player.playlists.push(obj)
return id;
}

function playPlaylist(id) {
// your code here
let playlist=playlistById(id);
for(let id of playlist.songs){
playSong(id)
}
}

function editPlaylist(playlistId, songId) {
// your code here
songById(songId);
let playlist=playlistById(playlistId);
let index=playlistIndex(playlist);
for(let i=0;i<playlist.songs.length;i++){
if (playlist.songs[i]===songId){
player.playlists[index].songs.splice(i,1)
if(player.playlists[index].songs.length===0){
removePlaylist(playlistId);
}
} else{
player.playlists[index].songs.push(songId)
}
}
}

function playlistDuration(id) {
// your code here
let sumDuration=0;
let playlist=playlistById(id);
for(let i=0;i<playlist.songs.length;i++){
sumDuration+=player.songs[songIndex(songById(playlist.songs[i]))].duration;
}
return sumDuration;
}

function searchByQuery(query) {
// your code here
const result={songs:[],playlists:[]}
let queryLowercase=query.toLowerCase();
for(let song of player.songs){
if(song.album.toLowerCase().includes(queryLowercase)||song.artist.toLowerCase().includes(queryLowercase) || song.title.toLowerCase().includes(queryLowercase))
{
result.songs.push(song);
}
}
result.songs.sort((a,b)=> {if(a.title.toLowerCase()<b.title.toLowerCase()) return -1;});
for(let playlist of player.playlists)
{
if((playlist.name.toLowerCase()).includes(queryLowercase))
{
result.playlists.push(playlist);
}
}
result.playlists.sort((a,b)=> {if(a.name.toLowerCase()<b.name.toLowerCase()) return -1;});
return result;
}

function searchByDuration(duration) {
// your code here
let durationDifference=10000; //first value
let durationInSeconds=durationToSeconds(duration);
let result={};
for(let i in player.songs){
if(Math.abs(player.songs[i].duration-durationInSeconds)<durationDifference){
durationDifference=Math.abs(player.songs[i].duration-durationInSeconds);
result=player.songs[i] //I check module of differnce between duration of song and arguments duration
}
}
for(let j in player.playlists){
if(Math.abs(playlistDuration(player.playlists[j].id)-durationInSeconds)<durationDifference){
durationDifference=Math.abs(playlistDuration(player.playlists[j].id)-durationInSeconds);
result=player.playlists[j] //I check module of differnce between duration of playlist(using function) and arguments duration
}
}
return result;
}

module.exports = {
Expand Down
Loading