Advertisement
EntropyStarRover

02.Story

Feb 20th, 2021
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Story {
  2.     #likes=[];
  3.     constructor (title, creator){
  4.         this.title=title;
  5.         this.creator=creator;
  6.         this.comments=[];
  7.         this.currentCommentId=0;
  8.         this.likesArr=[];
  9.         }
  10.  
  11.  
  12.  
  13.         like(username){
  14.             if (this.#likes.includes(username)){
  15.                 console.log('You cant like the same story twice!');
  16.               throw new Error  (`You can't like the same story twice!`)
  17.            }
  18.            if (this.creator==username){
  19.                throw new Error (`You can't like your own story!"`)
  20.            }
  21.           let userExists=false;
  22.           for (let i=0; i<this.#likes.length;i++){
  23.              
  24.               let usr=this.#likes[i];
  25.               if (username==usr){
  26.                   userExists=true;
  27.               }
  28.           }
  29.           if (!userExists){
  30.               console.log('hello, why you not working');
  31.               console.log(this.#likes.length)
  32.               this.#likes.push(username);
  33.               console.log(this.#likes.length);
  34.               return `${username} liked ${this.title}!`
  35.           }
  36.        }
  37.  
  38.      get likes(){
  39.            if (this.#likes.length==0){
  40.                return `${this.title} has 0 likes`
  41.            } else if (this.#likes.length==1){
  42.                return `${this.#likes[0]} likes this story!`
  43.            } else if (this.#likes.length>1){
  44.                return `${this.#likes[0]} and ${this.#likes.length-1} others like this story!`
  45.            }
  46.        }
  47.  
  48.        dislike(username){
  49.            //decrease dislikes once I figure em out
  50.            if (this.#likes.includes(username)==false){
  51.                throw new Error (`You can't dislike this story!`);
  52.            } else {
  53.                this.#likes.splice(this.#likes.indexOf(username));
  54.                return `${username} disliked ${this.title}`
  55.            }
  56.        }
  57.  
  58.        comment(username, content, id){
  59.            let existingComment=this.comments.find(c=>c.id==id);
  60.            if (existingComment==undefined){
  61.                this.currentCommentId++;
  62.                let newComment=
  63.                    {id:this.currentCommentId,
  64.                         username:username, content:content,replies:[]
  65.  
  66.                        }
  67.                        this.comments.push(newComment);
  68.                        return `${username} commented on ${this.title}`
  69.                } else {
  70.                    let replyId=`${existingComment.id}.${existingComment.replies.length+1}`
  71.                    let newReply={id:replyId,username,content};
  72.                    existingComment.replies.push(newReply);
  73.                    return `You replied successfully`
  74.  
  75.                        
  76.                }
  77.              
  78.  
  79.            }
  80.  
  81.           toString(sortingType){
  82.            let res=`Title: ${this.title}\n`
  83.            res+=`Creator: ${this.creator}\n`;
  84.            res+=`Likes: ${this.#likes.length}\n`
  85.            res+=`Comments:\n`
  86.  
  87.  
  88.            let commentsStr="";
  89.            let commentsArr=[]
  90.  
  91.            if (sortingType=="asc"){
  92.                this.comments.forEach(c=>{
  93.                    commentsStr+=(`-- ${c.id}. ${c.username}: ${c.content}\n`);
  94.                    if (c.replies.length>0){
  95.                        c.replies.forEach(r=>{
  96.                            commentsStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
  97.                        })
  98.                    }
  99.                })
  100.                res+=commentsStr.trim();
  101.            }
  102.  
  103.  
  104.            if (sortingType=='desc'){
  105.            let descendComments=[];
  106.            for (let i=this.comments.length-1; i>=0; i--){
  107.                let currCom=this.comments[i];
  108.                console.log(currCom)
  109.                currCom.replies.reverse();
  110.                descendComments.push(currCom)
  111.            }
  112.            let commStr=``
  113.            descendComments.forEach(c=>{
  114.                commStr+=`-- ${c.id}. ${c.username}: ${c.content}\n`;
  115.                if (c.replies.length>0){
  116.                    c.replies.forEach(r => {
  117.                        commStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
  118.                    });
  119.                }
  120.            })
  121.            res+=commStr.trim();
  122.        }
  123.  
  124.        if (sortingType=="username"){
  125.            let usernameSorted=this.comments.sort((a,b)=>a.username.localeCompare(b.username));
  126.  
  127.            usernameSorted.forEach(u=>{
  128.                u.replies.sort((a,b)=>a.username.localeCompare(b.username))
  129.            })
  130.  
  131.            let unStr="";
  132.            usernameSorted.forEach(u=>{
  133.                unStr+=`-- ${u.id}. ${u.username}: ${u.content}\n`;
  134.                if (u.replies.length>0){
  135.                    u.replies.forEach(r => {
  136.                        unStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
  137.                    });
  138.            }
  139.            })
  140.            res+=unStr.trim();
  141.        }
  142.        
  143.  
  144. return res;
  145.           }
  146.        }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement