Advertisement
nikolayneykov

Untitled

Nov 24th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Task 2 ???
  2.  
  3. // Your task is to translate a message in some alien language (let's call it Alienski).
  4.  
  5. // The message could be created by following simple rules and from two known languages, English and Spanish.
  6.  
  7. // Each word in Alienski is constructed by subtracting the letters from English and Spanish (absolute value) and that is the resulting letter.
  8.  
  9. // There are two special cases. If in each of the words the symbol is '-' (hyphen) or ' ' (space) it is mandatory for it to be kept this way.
  10.  
  11. // There won't be a case with a '-' (hyphen) and a ' ' (space) at the same time.
  12.  
  13. // If one of the words is with more letters than the other just add the letters from the longer word to the result.
  14.  
  15. // Example:
  16. // Copy
  17. // talk
  18. // hablar
  19. // Copy
  20. // a b c d....
  21. // 0 1 2 3....
  22. // t - h = | 19 - 7 | = 12 = m
  23. // a - a = | 0 - 0 | = 0 = a
  24. // l - b = | 11 - 1 | = 10 = k
  25. // k - l = | 10 - 11 | = 1 = b
  26. // empty - a = a
  27. // empty - r = r
  28.  
  29. // Result:
  30.  
  31. // makbar
  32. // Input:
  33. // Read from the standard input:
  34.  
  35. // Two lines with messages in English and Spanish
  36. // Each message is on new line.
  37. // Output:
  38. // Print on the standard output:
  39.  
  40. // On the single line of the output, print the decoded message in Alienski
  41. // Constraints:
  42. // All the letters will be small letters from the Latin alphabet and the special symbols '-' (hyphen) and ' ' (space).
  43. // Hint
  44. // Use the ASCII table
  45. // 'a' - 'a' = 0
  46. // Sample tests:
  47. // Input
  48. // Copy
  49. // thank you
  50. // muchas gracias
  51. // Output
  52. // Copy
  53. // hncgk  idacias
  54. // Note: There are two spaces here
  55.  
  56. // Input:
  57. // Copy
  58. // test
  59. // el examen
  60. // Output
  61. // Copy
  62. // ph pxamen
  63.  
  64.  
  65. const englishWord = 'thank you';
  66. const spanishWord = 'muchas gracias';
  67.  
  68. let result = '';
  69. let smallerWordLength = Math.min(englishWord.length, spanishWord.length);
  70.  
  71. for (let i = 0; i < smallerWordLength; i++) {
  72.   const englishLetter = englishWord[i];
  73.   const spanishLetter = spanishWord[i];
  74.  
  75.   const englishIndex = englishLetter.charCodeAt(0);
  76.  
  77.   const spanishIndex = spanishLetter.charCodeAt(0);
  78.   const newIndex = Math.abs(englishIndex - spanishIndex) + 97;
  79.    const newLetter = String.fromCharCode(newIndex);
  80.  
  81.   if (englishLetter === ' ' || englishLetter === '-') {
  82.     result += englishLetter;
  83.   } else if (spanishLetter === ' ' || spanishLetter === '-') {
  84.     result += spanishLetter;
  85.   } else {
  86.     result += newLetter;
  87.   }
  88. }
  89.  
  90. if (englishWord.length < spanishWord.length) {
  91.   result += spanishWord.slice(smallerWordLength);
  92. } else if (englishWord.length > spanishWord.length) {
  93.   result += englishWord.slice(smallerWordLength);
  94. }
  95.  
  96. console.log(result);
  97.  
  98. // hncgk  idacias
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement