如何修复函数在javascript中寻找最小值

时间:2022-08-08 22:52:16

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

很抱歉,可能是noob的问题,但是我不能让我的功能发挥作用。对我来说,它看起来和网上的解决方案非常相似,但不知何故,它不起作用,我也不知道问题出在哪里。如果有任何帮助,我会很感激的

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

;

6 个解决方案

#1


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.

更短的方法是使用rest参数……和传播的语法……对于Math.min的值。

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

#2


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

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

正如注释中所提到的,可以修改函数定义以保留调用函数的初始方式。这是通过使用rest参数完成的

The MDN docs describe rest parameters as:

MDN文档将rest参数描述为:

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

rest参数语法允许我们将不定数量的参数表示为数组。

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

像刚才那样调用函数: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;
    }

#3


1  

You can use Math.min

您可以使用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));

#4


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 :

因此,与其在函数体中写入var array =[],还不如这样做:


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.

第一种可能性:将数组作为参数:将函数声明为函数(数组),并将调用改为实际传递一个值数组:findValues([11, 12, 13, 21…]),并删除函数体中的var数组=[]。


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

第二个可能性(我最喜欢的):用var数组=[]替换var数组=[…参数]

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

关于参数对象的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments。

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

(另外,请注意,let现在是最佳实践,而不是var)

See Nina 's answer for full snippets examples with arguments

查看Nina的答案,获得带参数的完整片段示例

#5


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

#6


-2  

Well you're passing parameters to a function that does not use them Try something like

你把参数传递给一个不使用它们的函数试试

function min(foo[ ]){
    //Rest of the function
}

And call it like

并调用它

min([1,2,3,4]);

#1


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.

更短的方法是使用rest参数……和传播的语法……对于Math.min的值。

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

#2


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

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

正如注释中所提到的,可以修改函数定义以保留调用函数的初始方式。这是通过使用rest参数完成的

The MDN docs describe rest parameters as:

MDN文档将rest参数描述为:

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

rest参数语法允许我们将不定数量的参数表示为数组。

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

像刚才那样调用函数: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;
    }

#3


1  

You can use Math.min

您可以使用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));

#4


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 :

因此,与其在函数体中写入var array =[],还不如这样做:


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.

第一种可能性:将数组作为参数:将函数声明为函数(数组),并将调用改为实际传递一个值数组:findValues([11, 12, 13, 21…]),并删除函数体中的var数组=[]。


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

第二个可能性(我最喜欢的):用var数组=[]替换var数组=[…参数]

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

关于参数对象的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments。

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

(另外,请注意,let现在是最佳实践,而不是var)

See Nina 's answer for full snippets examples with arguments

查看Nina的答案,获得带参数的完整片段示例

#5


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

#6


-2  

Well you're passing parameters to a function that does not use them Try something like

你把参数传递给一个不使用它们的函数试试

function min(foo[ ]){
    //Rest of the function
}

And call it like

并调用它

min([1,2,3,4]);