admin管理员组

文章数量:1384630

Complete JS newbie here!

I made an Array with a few numbers in it. I added a function that will show me the lowest number. My question can I show the index of the lowest number?(even If I would change the numbers)
Here is my code in case you need it:

function func()
{
  var low= 0;
  var numbersArr=[29, 26, 44, 80, 12, 15, 40];
  for(var i = 0; i <= numbersArr.length;i++)
  {
     if(numbersArr[i]< numbersArr[i-1])
     {
       low = numbersArr[i];
     }
  }
  console.log(low);
} 
func();

Complete JS newbie here!

I made an Array with a few numbers in it. I added a function that will show me the lowest number. My question can I show the index of the lowest number?(even If I would change the numbers)
Here is my code in case you need it:

function func()
{
  var low= 0;
  var numbersArr=[29, 26, 44, 80, 12, 15, 40];
  for(var i = 0; i <= numbersArr.length;i++)
  {
     if(numbersArr[i]< numbersArr[i-1])
     {
       low = numbersArr[i];
     }
  }
  console.log(low);
} 
func();

Share Improve this question edited Nov 29, 2017 at 9:54 Mamun 69k9 gold badges51 silver badges62 bronze badges asked Nov 29, 2017 at 9:50 mimimimi 416 bronze badges 4
  • you can with indexOf function , check this – Neji Soltani Commented Nov 29, 2017 at 9:52
  • Use numbersArr.indexOf(low). – Imran Ahmad Commented Nov 29, 2017 at 9:54
  • To get the lowest number, the following solution may be more elegant: const lowestNumber = numbers.map(n => n).sort()[0];. As sort mutates, I first create a new array so that the original one is not mutated. – sp00m Commented Nov 29, 2017 at 9:55
  • You can simply use Math.min.apply to find min value. No need to write function. and then use indexOf to find the index. Check the below answer. – Bhuwan Commented Nov 29, 2017 at 10:36
Add a ment  | 

6 Answers 6

Reset to default 3

You can also store value of i in one variable. See below code.

function func()
{
	var numbersArr=[29, 26, 11, 44, 80, 12, 15, 40,10];
  var low = numbersArr[0];
  var indexOfLow;
    for(var i = 0; i <= numbersArr.length;i++)
{
    if(numbersArr[i]< low)
	{
		low = numbersArr[i];
    indexOfLow = i;
	}
}
console.log("Lowest number is : "+low);
console.log("Index of lowest number is : "+indexOfLow);
} 
func();

My question can I show the index of the lowest number?

You can do

numbersArr.indexOf(low)

Edit

That said, your logic of finding the lowest number isn't correct as you are only paring the consecutive values of the array, try the updated logic in demo below.

Demo

function func() {
  var numbersArr = [29, 26, 11, 44, 80, 12, 15, 40];
  var low = numbersArr[0];
  for (var i = 1; i <= numbersArr.length; i++) {
    if (numbersArr[i] < low ) {
      low = numbersArr[i];
    }
  }
  console.log(low);
  console.log(numbersArr.indexOf(low));
}
func();

You function lack the problem of keeping the lowest value, because you pare only the actual element and the element before.

function getLowestValue(array) {
    var low = 0,
        i;

    for (i = 0; i < array.length; i++) { // just loop i < array.length!
        if (array[i] < array[i - 1]) {
        //             ^^^^^^^^^^^^ this is the problem, you need to check against low
            low = array[i];
        }
    }
    return low;
} 

console.log(getLowestValue([29, 26, 11, 80, 12, 15, 40])); // 12 instead of 11

You could store the value of the lowest element and pare with this value.

function getLowestValue(array) {
    var low = array[0],                   // take the first element at index 0
        i;

    for (i = 1; i < array.length; i++) { // start with index 1, because you need to
                                         // check against the last known smallest value
        if(array[i] < low) {
            low = array[i];
        }
    }
    return low;
}

console.log(getLowestValue([29, 26, 11, 80, 12, 15, 40])); // 11 the right value

For getting the lowest index, you could just store the index instead of th value and take it for parison.

function getLowestIndex(array) {
    var lowIndex = 0,
        i;

    for (i = 1; i < array.length; i++) {
        if (array[i] < array[lowIndex]) {
            lowIndex = i;
        }
    }
   return lowIndex;
}

console.log(getLowestIndex([29, 26, 11, 80, 12, 15, 40])); // 2

No need to make a function to find minimum value. You can use simply Math.min.apply to find minimum value from array and then indexOf to find index of that value

var numbersArr = [29, 26, 44, 80, 12, 15, 40];
var minValue = Math.min.apply(null, numbersArr);
console.log("Minimum Value:"+ minValue, "Index is:" + numbersArr.indexOf(minValue));

Solution:You will declair a variable which will hold the index of low number and assign it in the if statement as shown below:

if(numbersArr[i]< numbersArr[i-1])
{
    low = numbersArr[i];
            IndexLow=i;
}

I found a super easy way to do that, in your way. Just little change in your code. Please take a look.

function func()
    {
      var numbersArr=[29, 31, 26, 44, 80, 123, 151, 40,15];
      var low= numbersArr[0];
      var index = 0;
      for(var i = 1; i < numbersArr.length;i++)
      {
        if(low >= numbersArr[i])
        {
          low = numbersArr[i];
          index = i;
        }
      }
      console.log(index);
      console.log(low);
    } 
    func();

Hope you found your problem. Happy coding :)

本文标签: Javascript Arrayshowing the index of the lowest numberStack Overflow