Ways to utilize the Math.max and Math.min functions within an Array

Attempting to use this approach on an array to retrieve both the minimum and maximum values proved unsuccessful.

var array = [3, 6, 1, 5, 0, -2, 3];
var min = Math.min( array ); 
var max = Math.max( array ); 
document.write(max);

Answer №1

Implement Function.apply

value = Math.min.apply(null, numbers);
value = Math.max.apply(null, numbers);

apply is quite similar to call(), except for the kind of arguments it can handle. It allows for the use of an array of arguments instead of specific parameter names. When using apply, an array literal can be utilized

Answer №2

Give this a shot.

function findMinMax(){
  var maxNum = Math.max.apply(Math, arguments);
  var minNum = Math.min.apply(Math, arguments);
  console.log(maxNum);  // displays 6
  console.log(minNum); // displays -2
}
findMinMax(3, 6, 1, 5, 0, -2, 3);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Javascript collapsible panel that is initially expanded

I found this code example here and decided to implement a collapsible panel using css/html/javascript: function toggleCollapsibleSectionWithAnimation() { this.classList.toggle("collapsible-active"); var buttonId = this.id; var sectionId = buttonId ...

Comparing Angular 5 with --aot and Angular 5 with --aot=false configuration settings

I am having an issue with my Angular application using Angular 5.0.0 and rxjs ^5.5.2. When I run the command ng serve --aot=false, everything works fine. However, when I use ng serve --aot, I encounter the following error: core.js:1350 ERROR Error: Uncaug ...

Extracting JavaScript OnClick button using Selenium

I'm having trouble extracting the email address from the following URL: https://www.iolproperty.co.za/view-property.jsp?PID=2000026825 that is only visible after clicking on the "Show email address" button. However, every time I attempt to click and r ...

Is there a way to incorporate a JavaScript variable as the value for CSS width?

I created a scholarship donation progress bar that dynamically adjusts its width based on the amount donated. While I used CSS to handle the basic functionality, I am now trying to implement JavaScript for more advanced features. My goal is to calculate ...

What is the method to modify an action in an Ajax function?

I am trying to modify the action in the AjaxUpload function. The issue is that the value of setnamefile in the action does not change because the page does not reload. My idea was to change the action on submit, but I have not been able to do so successfu ...

Having trouble locating HTML elements by their id when typing into a box using cy.get in Cypress with JavaScript

I am a novice in working with cypress and HTML, and I am currently attempting to enter an address in the specified address field. <input type="text" title="Street Address 1" name="billing[street][]" id="billing:street1" value="" class="input-text " ...

Transform the table into a byte array

I'm currently facing a challenge with converting a Lua Table into a C# Byte array. I had success converting it to a Double array using the following code snippet: > require 'CLRPackage' > import "System" > tbl = {11,22,33,44} > ...

Pass information from one .jsp file to a JavaScript function in another .jsp file

Here is the content of OneEmployee.jsp: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <form> <td> <c:out value="${emp.getEmpid()}"/></td> <td> <input type="text" id="fname" value="<c:out v ...

"Encountering a 400 Error While Attempting to Edit Requests in NodeJS/A

Currently, I am building an application using Ionic/Angular with a NodeJS backend. Within this project, I have created an update form that allows users to modify or delete a specific row. While the deletion function is working fine, I am facing some challe ...

Ways to implement pointer event styling within a span element

Hi, I'm having trouble with styling and I can't seem to figure out how to resolve it. The style pointer-events: none doesn't seem to be working for me. Here is an example of my code: The style snippet: .noclick { cursor: default; ...

Guide to illustrating the connections between origin and destination nations utilizing IP addresses

My goal is to create an interactive world map with a clickable marker for each country. When a user clicks on a source country's marker, I want to display interactions with other countries in an aggregated manner. While I have successfully drawn the w ...

Issue encountered while trying to retrieve element within unnamed function in ajax system

Is there a way to fetch data from a file using Ajax when mousing over an element? My code seems to be working fine, but I'm having trouble accessing a <p> element inside an anonymous function. It seems like the element loses scope within the fun ...

After swapping out "div" with "input" and then replacing "input" with

My goal is to create a function where a user can edit text within a div. The idea is to replace the current text with an editable input field, and then convert it back into a div after the user hits enter. I've encountered an issue while trying to im ...

Navigate to the next page in Angular ui-grid when the down key is pressed on the last row of the grid

Is there a way to navigate to the next page if the down key is pressed in the last row of the grid? const gridScope = angular.element(document.getElementById("MainWrap")).scope(); gridScope.gridApi.pagination.nextPage(); What is the best method to detect ...

"The function you are attempting to call is not defined within the HTMLButtonElement.onclick

How do I trigger the DeleteRow and EditRow functions when clicking on the Delete Button and Edit Button? <button style="margin-right: 5px;" type='button' onclick='return EditRow(@i)' id='@editrow' class='btn btn-prima ...

Is there a way to show the Username across multiple login pages without using PHP?

Is there a way to dynamically display the username on the Homepage by extracting it from the URL? <div id="login"> <form method="post" action=""> <h2>Login</h2> <p> <label for="username"&g ...

Failed to access element from the array using specified id

Seeking assistance with creating dynamic pages for each object in the boxArray file. I've developed a service called boxService to extract objects. While I am able to retrieve all elements successfully, encountering errors when attempting to extract i ...

Having trouble establishing a connection between socket.io client and server

I am currently attempting to establish a connection between a client and server using express and node.js. Unfortunately, I am encountering difficulties in connecting to the server. The tutorial I followed (available at https://socket.io/get-started/chat) ...

Tips for implementing try-catch with multiple promises without utilizing Promise.all methodology

I am looking to implement something similar to the following: let promise1 = getPromise1(); let promise2 = getPromise2(); let promise3 = getPromise3(); // additional code ... result1 = await promise1; // handle result1 in a specific way result2 = await ...

Choosing a particular svg path in d3.js using JSON attribute

Is it feasible to isolate just one of the SVG paths generated from a geoJSON file using D3? In my scenario, the paths represent different areas on a map. Users can pick an area from a dropdown menu. I aim to utilize the selected value from this list to pin ...