Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Task 2 ???
- // Your task is to translate a message in some alien language (let's call it Alienski).
- // The message could be created by following simple rules and from two known languages, English and Spanish.
- // Each word in Alienski is constructed by subtracting the letters from English and Spanish (absolute value) and that is the resulting letter.
- // 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.
- // There won't be a case with a '-' (hyphen) and a ' ' (space) at the same time.
- // If one of the words is with more letters than the other just add the letters from the longer word to the result.
- // Example:
- // Copy
- // talk
- // hablar
- // Copy
- // a b c d....
- // 0 1 2 3....
- // t - h = | 19 - 7 | = 12 = m
- // a - a = | 0 - 0 | = 0 = a
- // l - b = | 11 - 1 | = 10 = k
- // k - l = | 10 - 11 | = 1 = b
- // empty - a = a
- // empty - r = r
- // Result:
- // makbar
- // Input:
- // Read from the standard input:
- // Two lines with messages in English and Spanish
- // Each message is on new line.
- // Output:
- // Print on the standard output:
- // On the single line of the output, print the decoded message in Alienski
- // Constraints:
- // All the letters will be small letters from the Latin alphabet and the special symbols '-' (hyphen) and ' ' (space).
- // Hint
- // Use the ASCII table
- // 'a' - 'a' = 0
- // Sample tests:
- // Input
- // Copy
- // thank you
- // muchas gracias
- // Output
- // Copy
- // hncgk idacias
- // Note: There are two spaces here
- // Input:
- // Copy
- // test
- // el examen
- // Output
- // Copy
- // ph pxamen
- const englishWord = 'thank you';
- const spanishWord = 'muchas gracias';
- let result = '';
- let smallerWordLength = Math.min(englishWord.length, spanishWord.length);
- for (let i = 0; i < smallerWordLength; i++) {
- const englishLetter = englishWord[i];
- const spanishLetter = spanishWord[i];
- const englishIndex = englishLetter.charCodeAt(0);
- const spanishIndex = spanishLetter.charCodeAt(0);
- const newIndex = Math.abs(englishIndex - spanishIndex) + 97;
- const newLetter = String.fromCharCode(newIndex);
- if (englishLetter === ' ' || englishLetter === '-') {
- result += englishLetter;
- } else if (spanishLetter === ' ' || spanishLetter === '-') {
- result += spanishLetter;
- } else {
- result += newLetter;
- }
- }
- if (englishWord.length < spanishWord.length) {
- result += spanishWord.slice(smallerWordLength);
- } else if (englishWord.length > spanishWord.length) {
- result += englishWord.slice(smallerWordLength);
- }
- console.log(result);
- // hncgk idacias
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement