repl.it
@cmarmar/

DeterminedTurboLamp

Nodejs

Prasanna sent the code! This is my first refactor for clarity.

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Below, we've written code for a simple chat program (Similar to Slack or 
IRC). Recently, our users have been asking for more features, and we've 
started expanding the code. It's getting out of hand!

Please refactor this code to make it "better" and support our feature 
requests. What "better" means is up to you. To get us started, here are some 
initial features people have wanted:

1.Multiple channels: Users want separate channels (aka separate chat rooms) 
  to talk on. Right now everyone talks in the same place.

2.More bots: Everyone wants to add their own bot. We need to be able
  to write more bots in a scalable way, and ideally turn them on and off
  on each channel.

Meta note: Feel free to lookup whatever you need and change whatever you
feel appropriate to change. Modern Javascript syntax is supported.

*/ 

// C: The first thing I did was change all vars to lets and consts. Not just because I'm more comfortable with it, but also because the var hoisting can cause some issues if you redefine the variable at any point. Best to keep things within their lexical scope. 
const _ = require('underscore')
const TestRunner = (function() {
  function TestRunner() {}

  TestRunner.prototype.run = function() {
    receive("Alicia", "What's the status of #edgehill?")
    receive("Hannah", "Not sure, we should ask Jordan.")
    receive("Alicia", "Ahh ok - thanks.")
    receive("Alicia", "/givepoint Hannah")
    receive("Alicia", "/givepoint Hannah")
    receive("Alicia", "/hangout Jordan")
    receive("Hannah", "/setaway Be back in a bit")
    receive("Alicia", "Lunch?")
    const expectedOutput = ["Alicia: What's the status of #edgehill?", "Hannah: Not sure, we should ask Jordan.", "Alicia: Ahh ok - thanks.", "Alicia: /givepoint Hannah", "Bot: Hannah now has 1 point.", "Alicia: /givepoint Hannah", "Bot: Hannah now has 2 points.", "Alicia: /hangout Jordan", "Bot: Starting Hangout...", "Hannah: /setaway Be back in a bit", "Bot: Thanks, your away message has been saved.", "Alicia: Lunch?", "Hannah (Autoreply): Be back in a bit"]
    console.log(messages)
    console.log('-------------------------------')
    if (_.isEqual(messages, expectedOutput)) {
      return console.log("Tests Pass")
    } else {
      return console.log("Tests Failed")
    }
  }

  return TestRunner

})()

let messages = []
let channels = {}
let aways = []
let points = {}

//C: I've extracted the coding blocks that handle the points and away messages into their own functions that can be called within the given scope. This "modularization" will help us out when we want to consider multiple channels and bots.
const givePoint = (message) => { 
  const person = message.slice(11) //update points
  !points[person] ? points[person] = 1 : points[person] += 1
  const pointsScore = points[person] > 1? `Bot: ${person} now has ${points[person]} points.` : `Bot: ${person} now has ${points[person]} point.` // "display" current score
  messages.push(pointsScore)
}
const setAway = (sender, message) => {
  const awayMessage = message.slice(9)
  messages.push("Bot: Thanks, your away message has been saved.")
  return aways.push({
    sender: sender,
    message: awayMessage
  })
}
const receive = (sender, message) => {
  const matches = /\/(\w)+/i.exec(message)  
  messages.push(sender + ": " + message) //Immediately "display" the message received
  aways.forEach( (x) => messages.push(`${x.sender} (Autoreply): ${x.message}`)) //Filter for AFK users and send out their respective autoreplies
  if (matches) { // Guards against cases when there's no commands ammong the messages received
    switch (matches[0]) {
      case '/hangout': 
        messages.push("Bot: Starting Hangout...")
        break
      case '/givepoint': 
        givePoint(message)
        break
      case '/setaway': 
        setAway(sender, message)
        break
    }
  }
}


(new TestRunner).run()
node v10.16.0