How to fix my function trying to find min value in javascript

javascript find min value in array of objects
javascript min array
math.min javascript
write a function in javascript that returns the minimum value of an array.
math.sum javascript
javascript reduce
javascript find object with highest value
minimum value in array in javascript

sorry for the noob question probably, but I can't get my function to work. For me it looks very similar to the resolutions found on the web, but somehow it doesn't work and I can't tell where is the problem. Would be grateful for any help

function findvalue() {
  var i = 0;
  var array = [];
  var min = array[0];
  for (i = 0; i < array.length; i++) {
    if (min > array[i]) {
      min = array[i];
    }
  }
  return min;
}
console.log(findvalue(11, 12, 13, 21, 22, 23, 97, 98, 99))

;

You could use arguments, an array like object of the function.

function findvalue() {
    var i = 0,
        min = arguments[0];

    for (i = 1; i < arguments.length; i++) {
        if (min > arguments[i]) {
            min = arguments[i];
        }
    }
    return min;
}
console.log(findvalue(11, 12, 13, 21, 22, 23, 97, 98, 99));

Math.min(), The static function Math.min() returns the lowest-valued Zero or more numbers among which the lowest value will The smallest of the given numbers. Get the latest and greatest from MDN delivered straight to your inbox. We create a variable named sum and set it equal to the returned value of our reduce() method. The reduce() method works from left to right in our array. It adds each num (number) in the array to the total value. The 0 at the end of the callback indicates the initial value for total.

Your function definition is incorrect, as well as how you are calling your function.

You are looking to iterate over an array, but you are calling your function with a bunch of numbers as the arguments. You instead need 1 parameter (argument) to call your function, which should be an array .

You have to instead call it this way:

findvalue([11, 12, 13, 21, 22, 23, 97, 98, 99])

Your function definition needs to be:

function findvalue(array) {
  var i = 0;
  var min = array[0];
  for (i = 1; i < array.length; i++) {
    if (min > array[i]) {
      min = array[i];
    }
  }
  return min;
}

As noted in the comments, you could modify your function definition to retain your initial way of calling the function. This is done by using rest parameters

The MDN docs describe rest parameters as:

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

Call the function as you did: findvalue(11, 12, 13, 21, 22, 23, 97, 98, 99)

Your function definition would be:

function findvalue(...array) {
      var i = 0;
      var min = array[0];
      for (i = 1; i < array.length; i++) {
        if (min > array[i]) {
          min = array[i];
        }
      }
      return min;
    }

JavaScript Arrays, Finding the Minimum value in an Array. The problem with Math.min() is it expects each parameter to be input on arrow function syntax, check out my previous article: JavaScript  Because MIN_VALUE is a static property of Number, you always use it as Number.MIN_VALUE, rather than as a property of a Number object you created. Examples Using MIN_VALUE. The following code divides two numeric values. If the result is greater than or equal to MIN_VALUE, the func1 function is called; otherwise, the func2 function is called.

You can use Math.min

function findMin() {
   // arguments is an Array-like object corresponding to the arguments passed to a function. 
   return Math.min.apply(Math, arguments)
}

console.log(findMin(2,4,1,0,9,-2));

JavaScript: Finding Minimum and Maximum values in an Array of , The Problem. Your minimum and maximum Y values can be anywhere. reduce() is a handy Array method that applies a function against an accumulator and each element in the  I am trying to create a page that lets the user enter three numbers, and have the max and min values printed below from the input. I've used both the Math.max/min functions and also tried an if statement but When I click submit nothing shows up. Some insight would be greatly appreciated, thanks!

The missing thing in your function is the array must be a parameter of your function.

As you wrote it, the function is always trying to find the minimum in an empty array.

It is currently completely ignoring the example values you passed when you called your function.

So, instead of writing var array = [] in the body of you function, you have several possibilities :


1st possibility : take the array as parameter : declare your function as function(array) and change your call to actually pass an array of values : findValues([11, 12, 13, 21 ...]), and remove the var array = [] inside the body of your function.


2nd possiblity (my favorite): just replace var array = [] by var array = [...arguments]

Documention on the arguments object here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

(and also, please note that let is now best practice than var)

See Nina 's answer for full snippets examples with arguments

JavaScript Tip: Finding the Minimum or Maximum Value in an Array , Return the number with the lowest value: Math.min(5, 10);. Try it Yourself ». More "Try it Yourself" examples below. How to fix this error: Ensure the function name is correct. With this error, the line number will usually point at the correct location. With this error, the line number will usually point at the correct location.

try like this

function findvalue() {
	var order = Array.from(arguments).sort(function(a,b){ return a - b; });
	var min = order[0];
	//var max = order[order.length-1];
	return min;
}
// MIN value
console.log(findvalue(23, 97, 98, 99, 11, 12, 13, 21, 22));

JavaScript min() Method, If you pass too few, the missing parameters get assigned the value undefined . The downside of this is that it is possible—  Using bad browser sniffing code. All browsers have a user-agent string, which identifies what the browser is (version, name, OS, etc.) In the bad only days when pretty much everyone used Netscape or Internet Explorer, developers used to use so-called browser sniffing code to detect which browser the user was using, and give them appropriate code to work on that browser.

Functions :: Eloquent JavaScript, lastIndexOf - Find the last occurrence of a value within the array. And finally iteration methods that apply functions to  When a function is called from a global scope, arguments.callee.caller evaluates caller to null. So arguments.callee.caller.name is like trying to access null's name property ( null.name ) , but null doesn't have a property called name , since it's not an object and can not have any property at all.

d3/d3-array: Array manipulation, ordering, searching , Underscore provides over 100 functions that support both your favorite workaday functional Try Underscore-contrib. var even = _.find([1, 2, 3, 4, 5, 6], function(​num){ return num % 2 == 0; }); => 2 Returns the minimum value in list. Definition and Usage. The toFixed() method converts a number into a string, keeping a specified number of decimals. Note: if the desired number of decimals are higher than the actual number, nulls are added to create the desired decimal length.

Underscore.js, JavaScript offers several ways to find the smallest and The built-in Math functions Math.min() and Math.max() do so compiling your JavaScript code will help with arrays The problem statement is as follows: Given a… On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.

Comments
  • First thing I can see is, you're calling your function with parameters, but in the definition of it, they're not there
  • console.log(Math.min([11, 12, 13, 21, 22, 23, 97, 98, 99]);
  • You are correctly finding the minimum value in var array = [];. Not among the function arguments.
  • Would love to know why this was down-voted
  • It wasn't me, but this won't produce the right result. You're still shadowing your array parameter with the line var array = [].
  • Oops, copy paste error there. Updated now, thanks for that
  • Looks good now, but two additional notes: you can use a rest parameter findvalue(...array) so that OP can keep his original calling syntax (pass each number as individual argument). Also, since you've pulled the first item from the array already, you may as well start the loop at index 1.
  • Added a snippet using the rest parameters approach
  • Creative approach!
  • Using a sort to find the minimum is doing too much work. It may not affect you right away, but is a problem with longer arrays.
  • That's not valid syntax