2

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))

;

3
  • First thing I can see is, you're calling your function with parameters, but in the definition of it, they're not there Commented Apr 25, 2018 at 20:35
  • console.log(Math.min([11, 12, 13, 21, 22, 23, 97, 98, 99]); Commented Apr 25, 2018 at 20:35
  • 1
    You are correctly finding the minimum value in var array = [];. Not among the function arguments. Commented Apr 25, 2018 at 20:36

6 Answers 6

3

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));

A shorter approach could be the use of rest parameters ... and spread syntax ... for the values for Math.min.

function findvalue(...args) {
    return Math.min(...args)
}
console.log(findvalue(11, 12, 13, 21, 22, 23, 97, 98, 99));

Sign up to request clarification or add additional context in comments.

Comments

2

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;
    }

6 Comments

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
Note, though, that findvalue() returns undefined rather than a numeric value. It might be better to start with -Infinity.
|
1

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));

Comments

0

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

Comments

0

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));

2 Comments

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.
0

I am sure the Arrow function will simplify your work.

    //Variable diclaration
    var numbers = [11, 12, 13, 21, 22, 23, 97, 98, 99];

    //Arrow Function to find Min Number
    var minfinder = numbers.reduce((a, b) => Math.min(a, b));

    //Consloe Output
    console.log("Min Number is: " + minfinder);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.