Morse Decoder

I am currently doing the Morse Decoder project. I got stuck at the “default” case in the “switch” statement in the “else” part of the “for char in secretMessage” loop. I do not understand:

  1. In what situations do the “1st” and “default” cases execute? From what I understand, if “char” is a space, it should execute only one case i.e. the 2nd case. How did we end up with two more cases (1st and default)?

  2. How does the “default” case (after execution) append the value to “morseCodeArray”? What value does the “default” case append?

var englishText = "this is a secret message"

var secretMessage = ".... --- .-- -.. -.--   .--. .- .-. - -. . .-."

// Add your code below 🤫
var letterToMorse: [String: String] = [
  "a": ".-",
  "b": "-...",
  "c": "-.-.",
  "d": "-..",
  "e": ".",
  "f": "..-.",
  "g": "--.",
  "h": "....",
  "i": "..",
  "j": ".---",
  "k": "-.-",
  "l": ".-..",
  "m": "--",
  "n": "-.",
  "o": "---",
  "p": ".--.",
  "q": "--.-",
  "r": ".-.",
  "s": "...",
  "t": "-",
  "u": "..-",
  "v": "...-",
  "w": ".--",
  "x": "-..-",
  "y": "-.--",
  "z": "--.."
]
var morseText: String = ""
for element in englishText {
  if let morseChar = letterToMorse["\(element)"] {
    morseText += morseChar + " "
  } else {
    morseText += "   "
  }
}
print(morseText)

var decodedMessage: String = ""
var morseCodeArray: [String] = []
var currMorse: String = ""

for char in secretMessage {
  if char != " " {
    currMorse.append(char)
  } else {
    switch currMorse {
      case "": 
        currMorse += " "
      case " ":
        morseCodeArray.append(" ")
        currMorse = ""
      default: 
        morseCodeArray.append(currMorse)
        currMorse = ""
    }
  }
}

Welcome to the forums :slight_smile:

As it’s written, this is how it’s working:

  • In what situations do the “1st” and “default” cases execute?
// (in pseudocode)
// 1st switch case (case ""):

For every character in secret message
    if char is " " (in other words, not char != " ")
    then
       if currMorse is ""
           append " " to currMorse

// default case
For every character in secret message
    if char is not any of the listed cases (not "" or " ")
    then
        append currMorse to morseCodeArray
        currMorse is equal to ""

How does the “default” case (after execution) append the value to “morseCodeArray”?
What value does the “default” case append?

The value default case appends whatever value is currMorse at the time (that meets the requirements of the default case).

1 Like

Thank you for the explanation!
I have some more queries.

When you say

I know “” means an empty string, but where is the scope of an empty string while looping through secretMessage?
secretMessage only has Dots, Dashes, and Spaces. So in what cases do we come across empty strings “”?

And since we only have Dots, Dashes, and Spaces, they can be covered by:

and

This is what has been bugging me now. I apologize for this seemingly stupid question I am new to programming.

secretMessage only has Dots, Dashes, and Spaces. So in what cases do we come across empty strings

The switch cases test for currMorse being empty, currMorse can be thought of a tracker in this loop. We come across the cases every time a space is dealt with

The code:

  • adds character if it’s non-space and non-empty
  • if it’s a space, it depends on the tracker: currMorse
  • if currMorse is in its null state, then currMorse becomes a space
  • if currMorse is already a space, the array gets a space added
  • there’s a default added in for good measure

If you want to check to see whether the code is good, the best way is to verify it for yourself. Test it with different parameters (and see if it can work just as well stripped down). This will give you a better sense of why each step was put.

Usually, come project time, I hate to deal with code I don’t understand so I make a comment mark to review any technique I’m not familiar with… so that it doesn’t destroy my code later hahaha.

So if I’m adding some new functionality that I’m less familiar with, I try to break it first.

1 Like

This is gold! It is clear now. Thank you so much :slight_smile:

Another wonderful answer @toastedpitabread. I’m learning some stuff too. Keep it up!

1 Like

Hahaha I don’t know any swift :sweat_smile:… it’s in my to-learn pile after I finish my current projects. I really appreciate how nicely put together their documentation seems to be upon first glance.

Maybe this could someone to understand the Morse Decoder better.

have treated only this part → var secretMessage = ".... --- .-- -.. -.-- .--."

var secretMessage = ".... --- .-- -.. -.--   .--."

for char in secretMessage {
  if char != " " {
      currMorse.append(char)
  } else {
    switch (currMorse) {
      case "":
        currMorse += " "
      case " ":
        morseCodeArray.append(" ")
        currMorse = ""
      default: 
        morseCodeArray.append(currMorse)
        currMorse = ""
    }
  }
}