-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
62 lines (43 loc) · 1.37 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Tweeter=function(){
let _Posts=[]
let postIdCounter=1
let commentIdCounter=0
const addPost=function(text){
let postId="p"+postIdCounter
_Posts.push({ id: postId, text, comments: [] });
postIdCounter++
return true
}
const getPosts=function(){
return _Posts
}
const removePost=function(postId ){
const postIndex = _Posts.findIndex(post => post.id === postId);
_Posts.splice(postIndex, 1);
console.log(JSON.stringify(_Posts))
return true
}
const addComment=function( textcom , postid ){
const commentId = 'c' + commentIdCounter++;
for(let i in _Posts){
if(_Posts[i].id===postid){
_Posts[i].comments.push({ id: commentId, text: textcom})
}
}
return true
}
const removeComment=function(postID, commenid ){
let indexPost = _Posts.findIndex(x => x.id === postID)
let indexComment = _Posts[indexPost].comments.findIndex(x => x.id === commenid)
_Posts[indexPost].comments.splice(indexComment, 1);
commentIdCounter--
}
return{
addPost:addPost,
getPosts:getPosts,
removePost:removePost,
addComment:addComment,
removeComment:removeComment,
postIdCounter:postIdCounter
}
}