0

I have group of numbers. Without using methods, how to pick the index of the smallest number in JavaScript? Explain, how the code will work.

let numbers = [20, 90, 10, 324, 9, 73] //Example array of numbers.
2
  • 2
    Smallest index of the smallest number? You mean smallest number index? Please provide example input/output and what you already tried. Commented Apr 27, 2021 at 21:29
  • 1
    Welcome to Stack Overflow! This is not a website where people write code for you so that you don't have to. If you need help debugging code that you have written, you must post a Minimal, Complete, and Reproducible example and explain the specific problem with your code. Commented Apr 27, 2021 at 21:36

2 Answers 2

3

Barebones, without methods:

let numbers = [20, 90, 10, 324, 9, 73]
let smallest = 0

for (i in numbers) {
  if (numbers[smallest] > numbers[i]) smallest = i
}

console.log(smallest)

First, we make a variable "smallest" and set it to 0, which is the first index of the array. Then, we go through the entire array and see if there's a number smaller than numbers[smallest], the initial value of which is 0. If there is, each time smallest is set to the index of that smaller number. By the end of the array, we have the variable smallest set to the index of the smallest number in the array. That's the solution without methods.

Using array methods and the Math library:

let numbers = [20, 90, 10, 324, 9, 73]
let smallest = numbers.indexOf(Math.min(...numbers))

console.log(smallest)

  • Math.min() takes a list of numbers and returns the smallest.
  • ...numbers is what's called "spreading". Math.min() takes parameters, not an array, (Math.min(1, 2, 3) not Math.min([1, 2, 3]). So ...numbers turns [20, 90, 10, 324, 9, 73] into (20, 90, 10, 324, 9, 73)
  • The .indexOf() method takes a value and finds the index of that value in an array.
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of setting smallest = 0, just set smallest = numbers[0] and replace it if a lesser value if encountered. This will eliminate the negative numbers problem.
Can't see why it'd produce wrong results with negative numbers. Can you provide an example? My tests with negative numbers provided correct results.
Thanks for the feedback! I really appreciate that
No problem! If a post answers what you were looking for, don't forget to accept it as a correct answer :)
how if there are multiple minimum value in set of array. can we get the multiple index ?
0

You can get the smallest number first

let numbers = [20, 90, 10, 324, 9, 73];
let smallest = Math.min(...numbers);

Then, you can get its index easily using the indexOf function.

let index = numbers.indexOf(smallest);

1 Comment

thank toy a lot but I'm learning and i want to know how it's work without using something like indexOf i could easily use it, Thx <3

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.