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
62 changes: 38 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,44 @@ Explain what is happening on each of the following lines in the code.

| Line # | What's happening?
|:------:|-------------------
| 1 |
| 2 |
| 3 |
| 6 |
| 7-8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 17-19 |
| 1 | constant variable
| 2 | first part of constant assignment; hash key/value pair, value is array literal
| 3 | second key value pair in constant assignment, value is array literal
| 6 | method signature - method name + argument that it takes (words)
| 7-8 | two local variables being assigned the value 0
| 9 | takes the words passed into the method and makes them all lowercase
| 10 | takes words and splits them by space, puts them into array, then iterates over
each word in the array.
| 11 | check if word being iterated over is included in the constant variable's value of
key :happy
| 12 | increase the value of the local variable happy by 1.
| 13 | check if word being iterated over is included in the constant variable's value of
key :sad
| 14 | increase the value of the local variable sad by 1.
| 17-19 | after iterating through all words, if counter of local var happy is greater than
value of local variable sad, return the smiley face. If value of sad is greater than value of happy, return the sad face. If the values are equal to one another, return neutral face.

### Data Types
What's the Data Type of the following?

| Code | Data Type
|----------------------------|-----------
| FEELINGS |
| :sad |
| happy |
| words |
| words.split(" ") |
| FEELINGS[:sad] |
| FEELINGS[:happy].include? |
| analyze_mood(text) |
| FEELINGS | hash
| :sad | symbol
| happy | fixnum
| words | string
| words.split(" ") | array
| FEELINGS[:sad] | array literal
| FEELINGS[:happy].include? | boolean
| analyze_mood(text) | string

### Explaining the Code
| Question | Answer
|------------------------|-------
| Why do we need line 9? |
| What is the relationship between `words` and `word` (line 10)? |
| Why doesn't line 19 have an associated if/condition? |
| What is the relationship between `text[0]`, `text[1]`, and `words`? |
| Why do we need line 9? | so the data is consistent and will match the words in the constant
| What is the relationship between `words` and `word` (line 10)? | word is one element of the words array
| Why doesn't line 19 have an associated if/condition? | Because it's what is returned if the values are equal
| What is the relationship between `text[0]`, `text[1]`, and `words`? | text is analyzing each row/string of words, which is passed in to the method

### Assignment: Requirements
1. Replace lines 31 and 32 and write a loop to print out each day and the emoticon that is associated by analyzing the mood of that day.
Expand All @@ -56,6 +60,8 @@ Your result will look like:

**think**: Why does 03/13 come out as _sad_ when it should be _happy_? How could we fix this?

Because "Yay!" has an exclamation point on it, and that doesn't match the word in the constant

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

2. To make the results a little more accurate, let's write and utilize a method called `strip_punctuation` to strip out the punctuation that affects the results. Namely, remove exclamation marks (!), periods (.), commas (,), and hashtags (#).

Your method should take a string as an argument and return the string without the above mentioned punctuation.
Expand All @@ -69,6 +75,8 @@ After writing this method, our new result should be:

**think**: Where should we call `strip_punctuation`? Does it matter? Why?

The strip_punctuation method should be called before the loop, because we want to remove punctuation before we add to the values of happy or sad variables. It could technically be put anywhere before the loop though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. This is a great observation. I should have additionally asked, "Should you call strip_punctuation inside or outside of analyze_mood? Why?"

3. Write a method called `happy_days` to determine how many logged entries it takes until there have been three :-) happy days.

Your output could be something like:
Expand All @@ -78,6 +86,8 @@ It takes 5 entries for 3 happy days to occur

**think**: What are you going to do if there aren't at least 3 happy days? Where do you need to handle that case?

I fixed my loop to break when happy_faces variable has the value of 3, then modified the conditional at the end to return that there aren't enough happy days if happy_faces doesn't equal 3.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes on this below.

4. Write a method called `overall_mood` to determine the most common mood across all logged entries.

Your output could be something like:
Expand All @@ -87,4 +97,8 @@ The most common mood is :-)

**think**: Should you use an array or a hash to solve this problem? Why?

I feel like using an array is easier to push the result of analyze_mood into it, then just calculate the length of the arrays.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. This will definitely work. But it's not a very scalable solution. What if we eventually add a bunch more feelings, like "Afraid :O" or "Silly :P" ... More arrays, and more comparisons, ick...

**think**: What if we eventually want to add feelings to our analysis? Can we write this code in a way that will prevent us from having to re-write it later?

We could probably put the emotion faces into constants and use them throughout the methods.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have to rewrite this question.

65 changes: 61 additions & 4 deletions mood-analysis.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FEELINGS = {
happy: %w(yay, good, great),
sad: %w(terrible, awful, horrible)
happy: %w(yay good great),
sad: %w(terrible awful horrible)
}

def analyze_mood(words)
happy = 0
sad = 0
words.downcase!
words = strip_punctuation(words)
words.split(" ").each do |word|
if FEELINGS[:happy].include? word
happy += 1
Expand All @@ -19,6 +20,58 @@ def analyze_mood(words)
return ":-|"
end

def strip_punctuation(words)
words.gsub(/[!.#,]/, "")
end

def happy_days(array)
entries = 0
happy_faces = 0
array.each do |line|
if happy_faces < 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is unnecessary because there shouldn't be a situation where happy_faces is greater than of equal to 3 because of the break below.

emotion = analyze_mood(line)
entries += 1
if emotion == ":-)"
happy_faces += 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit-picky: tabbing

break if happy_faces == 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to do a break here and then later check for this condition again later to do the return. Instead, just return here.

end
end
end
if happy_faces == 3
return "It takes #{entries} entries for 3 happy days to occur"
else
return "Not enough happy days!"
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this method return just the fixnum of the number of days it takes, not a string with that information inserted inside it. This allows us the ability to do other things with this information instead of just meeting this specfic need.


def overall_mood(array)
happy_moods = []
sad_moods = []
neutral_moods =[]
array.each do |line|
moods = analyze_mood(line)
if moods == ":-)"
happy_moods << moods
elsif moods == ":-("
sad_moods << moods
else
neutral_moods << moods
end
end
happy_length = happy_moods.length
sad_length = sad_moods.length
neutral_length = neutral_moods.length

if happy_length > sad_length && happy_length > neutral_length
return "The most common mood is :-)"
elsif sad_length > happy_length && sad_length > neutral_length
return "The most common mood is :-("
else
return "The most common mood is :-|"
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but a hash would have made for less work and a more scalable solution.



text = [
"03/01 I'm having a terrible horrible no good day.",
"03/13 Yesterday was horrible, but today is great! Yay!",
Expand All @@ -28,5 +81,9 @@ def analyze_mood(words)
"05/11 Yay, yay, yay! I'm having a awfuly great day."
]

puts analyze_mood(text[0])
puts analyze_mood(text[1])
text.each do |line|
puts line[0..5] + analyze_mood(line)
end

puts happy_days(text)
puts overall_mood(text)