Question

In: Computer Science

Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...

Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value).

The original array must NOT be modified by this function.
Example
secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8.

I have the Python code but I am having trouble converting it to Javascript format

def secondHighest(number):

count = 0

A1 = A2 = float('-inf')

for b in number:

count += 1

if b > A2:

if b >= A1:

A1, A2 = b,A1

else:

A2 = b

return A2 if count >= 2 else None

print(secondHighest([5,3,8,6,2,7,4]))

print(secondHighest([5,3,8,6,2,7,8]))

Solutions

Expert Solution

Code:

function myFunction(numbers) {
  max1=0      //max is used to store highest element and initially 0
  max2=0    //max is used to store second highest element and initially 0
  for(i=0; i<numbers.length; i++)  //traverse till length of given array
    {
        if(numbers[i] >= max1)     //check if maximum is greater than curren element 
        {
            max2 = max1;          //then maximum is second maximum
            max1 = numbers[i];    //and maximum element is current element
        }
        else if(numbers[i] >= max2 && numbers[i] <= max1)    //if the element is greater than second max and lessthan max
        {
            max2 = numbers[i];  //then store  it in second maximum
        }
    }
    if(max1===max2)     //if max1 and max2 are same then array has 2 maximum numbers
      console.log(max1)   //so print maximum number
    else
      console.log(max2)   //otherwise print second maximum number
}
myFunction([5,3,8,6,2,7,4]);  //call myFunction function
myFunction([5,3,8,6,2,7,8]); 

Output:

.


Related Solutions

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
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
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...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an object of the type "string" and "ch" is a character value with the default argument of 'A'. The function looks for the character "ch" in the string  "s" and displays all the indices of its occurrences on the screen. For example, if the caller sends the values of "ABCDBGAB" and 'B' to the function for the parameters "s" and "ch", respectively, the function displays the...
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT