DevByPowerPoint

Convert Text Variable to Morse Code

May 23rd, 2024 (edited)
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // Define an object that maps each character to its Morse code representation
  2. const morseCode = {
  3. 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
  4. 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
  5. 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
  6. 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
  7. 'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---',
  8. '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
  9. '8': '---..', '9': '----.',
  10. '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
  11. '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...',
  12. ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-',
  13. '"': '.-..-.', '$': '...-..-', '@': '.--.-.', ' ': '/'
  14. };
  15.  
  16. // Function to translate a message to Morse code
  17. function translateToMorse(message, callback) {
  18. // Convert the message to uppercase and translate each character to Morse code
  19. let morse = message.toUpperCase().split('').map(char => morseCode[char] || '').join(' ');
  20. let index = 0;
  21.  
  22. // Recursive function to update the Morse code character by character
  23. function updateMorse() {
  24. if (index < morse.length) {
  25. let char = morse[index];
  26. callback(char);
  27. index++;
  28. setTimeout(updateMorse, 1200 / 13); // Delay between each character at 13 WPM
  29. } else {
  30. callback(' ');
  31. setEndVariable(); // Call the function to set the END variable
  32. }
  33. }
  34.  
  35. updateMorse();
  36. }
  37.  
  38. let player = GetPlayer();
  39. let MESSAGE = player.GetVar('MESSAGE');
  40. let MORSE = '';
  41. let DOT_DASH = 'BLANK';
  42. let END = false;
  43.  
  44. // Function to update the MORSE and DOT_DASH variables
  45. function updateMorseAndDotDash(morseChar) {
  46. MORSE = MORSE + morseChar; // Append the new character to the existing Morse code
  47. player.SetVar('MORSE', MORSE);
  48.  
  49. DOT_DASH = morseChar === '.' ? 'DOT' : morseChar === '-' ? 'DASH' : 'BLANK';
  50. player.SetVar('DOT_DASH', DOT_DASH);
  51. }
  52.  
  53. // Function to set the END variable to true
  54. function setEndVariable() {
  55. END = true;
  56. player.SetVar('END', END);
  57. }
  58.  
  59. // Start translating the message to Morse code when the MESSAGE variable changes
  60. // The DOT_DASH output variable is used to trigger dot and dash sound effects
  61. // as the code translates the text to Morse code
  62. translateToMorse(MESSAGE, updateMorseAndDotDash);
  63.  
  64. // List of variables needed in Storyline for this code to work correctly:
  65. // 1. MESSAGE (input): A text variable that holds the message to be translated to Morse code.
  66. // 2. MORSE (output): A text variable that stores the translated Morse code.
  67. // 3. DOT_DASH (output): A text variable that indicates whether the current Morse code character is a dot, dash, or blank.
  68. // 4. END (output): A true/false variable that indicates when the translation is complete.
Advertisement
Add Comment
Please, Sign In to add comment