Question

In: Computer Science

Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...

Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 

let words = ["gin", "zen", "gig", "msg"]

longestMorseCodeWords(words)

The Morse Code equivalent of the words above is ["--...-.", "--...-.", "--...--.", "--...--."]. Ordering by length, it's [7, 7, 8, 8]. When we alphabetize based off of length in descending order, the output should be # ['gig', 'msg', 'gin', 'zen'].

Solutions

Expert Solution

JavaScript Code


function longestMorseCodeWords(words) {
    // Inititalized Morse Code in an array
    let morsecode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
        ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
        "..-", "...-", ".--", "-..-", "-.--", "--.."];
    let alphabets = "abcdefghijklmnopqrstuvwxyz";
    let morseCodeArray = []; // Has Morse Code equivalent of the words
    let morseCodeString = "";
    for (let i = 0; i < words.length; i++) {
        for (let j = 0; j < words[i].length; j++) {
            morseCodeString += morsecode[alphabets.indexOf(words[i][j])];
        }
        morseCodeArray[i] = morseCodeString;
        morseCodeString = "";
    }
    let morseCodeArrayLength = []; // Has Length of the morse code
    for (let k = 0; k < morseCodeArray.length; k++) {
        morseCodeArrayLength[k] = morseCodeArray[k].length;
    }
    let finalArray = []; // Has the final output;
    let tempArray = [];
    for (let m = Math.max.apply(null, morseCodeArrayLength); m >= Math.min.apply(null, morseCodeArrayLength); m--) {
        while (morseCodeArrayLength.indexOf(m) != -1) { // Runs till there is not element of the same length
            tempArray.push(words[morseCodeArrayLength.indexOf(m)]);
            words.splice(morseCodeArrayLength.indexOf(m), 1);
            morseCodeArrayLength.splice(morseCodeArrayLength.indexOf(m), 1);
        }
        tempArray.sort(); // Sorts Alphabetically
        for (let l = 0; l < tempArray.length; l++) {
            finalArray.push(tempArray[l]);
        }
        tempArray = [];
    }
    return finalArray; // Returns the final sorted array of words in the descending order of length
}
let words = ["zen","gig", "gin", "msg"]; // Input Words array
console.log(longestMorseCodeWords(words));

OUTPUT:


Related Solutions

JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT