3

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

4
  • you can with indexOf function , check this Commented Nov 29, 2017 at 9:52
  • Use numbersArr.indexOf(low). 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. 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. Commented Nov 29, 2017 at 10:36

6 Answers 6

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

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

1 Comment

@NinaScholz I have only added the way to store index. I didn't check the way to find lowest number. I will update my code.
3

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

1 Comment

@NinaScholz Why is it funny? OP's question was never about finding the lowest number, it was about find the index of lowest number presumably already found! Anyways, I have updated the code.
1

You function lack the problem of keeping the lowest value, because you compare 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 compare 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 comparison.

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

2 Comments

" for (i = 1; i <= numbersArr.length; i++) { // start with 1" - why do I have to start with 1?
Thanks a lot! I actually understand it now. :)
1

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

Comments

0

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

Comments

0

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

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.