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

Define a JavaScript function named prepend that has two parameters. The first parameter is an array...
Define a JavaScript function named prepend that has two parameters. The first parameter is an array of Strings. The second parameter is a String. Your function should use the accumulator pattern to return a newly created array whose elements are the concatenation of the second parameter with the element at the same index in the first parameter.
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 in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
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...
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns...
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme". Code: function reverse_kvs(store) { var reverse_store = {}; for (var key in store) { if (store.hasOwnProperty(key)) { reverse_store[store[key]]...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT