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

write a javascript function called cal_modes that will take a List of numbers or strings as...
write a javascript function called cal_modes that will take a List of numbers or strings as input and returns a List of the most frequent values. If there's only one most-frequent value, it returns a single-element List. example cal_modes([3,4,5,5]) should return [5] cal_modes([8.9, 1, 1]) should return [1] cal_modes([2.5, -2, -2, 2.5]) should return [2.5] cal_modes([3,3,4,4]) should return [3,4] cal_modes([3,4,5]) should return [3,4,5], because all occur with equal frequency cal_modes(["hi", "what", "where", "hi"]) should return ["hi”]
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...
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
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
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array values from the start to end index separated by delimiter\'s value. function([1,2,3,4],1,3,'*') // returns 2*3*4 as string 2. Write a function to change the case of all the characters in string based on their position which matches the value of the pos parameter passed to function(str, pos [even|odd]). Example: function(‘abCd’, ‘odd’) // returns Abcd 3 Write a function which receives two arguments (array, oddOrEven)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT