Skip to content

submit part 4: mythical creature interactions#1

Open
hafzahIO wants to merge 1 commit into
mainfrom
creature-interactions
Open

submit part 4: mythical creature interactions#1
hafzahIO wants to merge 1 commit into
mainfrom
creature-interactions

Conversation

@hafzahIO

@hafzahIO hafzahIO commented Apr 1, 2025

Copy link
Copy Markdown
Owner

No description provided.

@hafzahIO hafzahIO requested a review from ericjenkinson April 1, 2025 05:42

@ericjenkinson ericjenkinson left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job completing this assignment.

@@ -28,6 +28,57 @@ struct Creature {
return Int(numerator / denominator)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Meets expectations!

It looks like your other branches have parts 1 through 3, and this branch has part 4. I was able to combine them, so there was no problem.

// }

// Part 4: Mythical Creature Interactions
func interactWith(creature: Creature) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function exhibits a behavior that is not discussed in the content. In Swift, if a function doesn’t explicitly return a value, it returns Void (an empty tuple, ()).

This is why you were seeing () in your output.
Astral Horn is a unicorn and has a magic power strength of 9 points which translates into 34 points of special ability. Both creatures are good and there is no point in them fighting each other. What can we do? ()

This also plays a part in the issue you found when trying to single-print a statement with String Interpolation.

return Int(numerator / denominator)
}

// // Part 4: Mythical Creature Interactions

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It is considered good style to remove code that is commented out. If you need to leave the code commented out, add a comment explaining why for the reviewer.


/* The homework is done before this line. However, the func describeCreature has 2 print functions to deliver what I was asked of. If I want to combine the content of the two print functions into one, what is printed inside the console down is done in reverse (i.e. it calls the interactWith function first then the content of the first print function). I don't know why is this and I hope you can help understand this behavior (this is why I separated the content into 2 separate print functions).
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice catch! The reason why the output from below appears first is due to how String Interpolation works.

\(creature.ability) \(creature.interactWith(creature: creatureArray.randomElement()!))

Swift evaluates every expression inside the string interpolation before constructing the final string for the print statement. The call to creature.interactWifth(creature: ) contains its own print statement or other side effects, those will execute immediately when that expression is evaluated.

In other words, as Swift processes the interpolated expressions, it calls the function, and its side effects (like printing) occur right away, which is why you see its output before the overall string is printed.

You can resolve this issue by modifying interactWith(creature: ) to return a String.

`func interactWith2(creature: Creature) -> String {
switch (self.isGood, creature.isGood) {
case (false, false):
return "Both creatures are evil; it's good that they are fighting each other."

case (false, true):
return "The evil (self.name) is fighting the good (creature.name). Do you think we can convince (self.name) to turn to the right side?"

case (true, false):
return "I guess there is no better time to fight the evil (creature.name), right?!"

default:
return "Both creatures are good and there is no point in them fighting each other. What can we do?" }
}`

This change will preserve the order of output as intended and remove the extra () that are printed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants