Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.8 kB
2
Indexable
Never
/*
    Create Methods to support 
    stackoverflow website logic

    Post a Question
    Search Questions
    Reply to a question 
    Up Vote / Down Vote Question

*/



// data = {
//     question:{title:string,body:[{type:type,data:string}.....]},
//     tags: ["array of tags"],
//     comment:{},
//     vote:{up:Number,down:Number}
// }



// class stackoverflow{
    posts=[];
    counter=1;

    function createPost(data){
         //pre validation to come
         try{
            let result = search(data.question.body);
            console.log(result);
            if(!result.length){
                data.postid=counter;
                posts.push(data);
                counter++;
                return {msg:"created successfully",post:data,code:200};
            }
            return {msg:"Similat post available",post:result,code:301};

         }catch(e){
            console.log(e);
            return {msg:"Error in saving data",code:417};
         };
             
    }
    function search(searchKey){
        let searchparams=searchKey.split(" ");
        //elimante generic words from key
        console.log(searchparams);
        let result=[]
        for(let p=0;p<posts.length;p++){
            let element=posts[p];
            for(let k=0;k<searchparams.length;k++){
                if(element.question.body.indexOf(searchparams[k])>-1){
                    result.push(element);
                    break;
                };
            }
        };
        console.log(result);
        return result;
    }


// }





console.log(createPost({question:{title:"abc","body":"def"}}));
console.log(createPost({question:{title:"abc","body":"def"}}));
console.log(createPost({question:{title:"abc","body":"def"}}));

console.log(posts);

console.log(search("def"));
Leave a Comment