Question

In: Computer Science

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”]

Solutions

Expert Solution

function cal_modes(list) {
    var result = [];
    var maxCount = 0;
    for (var i = 0; i < list.length; i++) {
        var count = 0;
        for (var j = 0; j < list.length; j++) {
            if (list[i] === list[j]) {
                count++;
            }
        }
        if (count > maxCount) {
            maxCount = count;
        }
    }
    for (var i = 0; i < list.length; i++) {
         var count = 0;
        for (var j = 0; j < list.length; j++) {
            if (list[i] === list[j]) {
                count++;
            }
        }
        if (count === maxCount) {
            var found = false;
            for (var k = 0; k < result.length; k++) {
                if (result[k] === list[i]) {
                    found = true;
                } 
            }
            if (!found)
                result.push(list[i]);
        }
    }
    return result;
}

console.log(cal_modes([3,4,5,5]));
console.log(cal_modes([8.9, 1, 1]));
console.log(cal_modes([2.5, -2, -2, 2.5]));
console.log(cal_modes([3,3,4,4]));
console.log(cal_modes([3,4,5]));
console.log(cal_modes(["hi", "what", "where", "hi"]));

Related Solutions

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...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
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...
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...
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT