Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:57+00:00 2026-05-26T14:11:57+00:00

Is it possible to sort arrays in ascending/descending order using the for loop JavaScript?

  • 0

Is it possible to sort arrays in ascending/descending order using the for loop JavaScript?

I’ve been learning JS going through a few practice questions in a textbook before a class test.

Any pointers would be appreciated!

a = Number(prompt("a:"));
b = Number(prompt("b:"));
c = Number(prompt("c:"));
d = Number(prompt("d:"));
e = Number(prompt("e:"));

// find largest element
var test = [a, b, c, d, e];
var biggest = -Infinity;
var biggest_index = -1; {
    for (i = 0; i < test.length; i++) {
        if (test[i] > biggest) {
            biggest = test[i];
            biggest_index = i;
        }
        else;
    }
    alert("The biggest element is " + biggest + " at index " + biggest_index);
}

// move largest element of array to the last index
test[test.length] = biggest;

// get rid of copy
test[biggest_index] = 0;
alert("Unsorted: " + test);

// shuffle ??
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:11:58+00:00Added an answer on May 26, 2026 at 2:11 pm

    If you want to get the max/min number in an array, why not use Math.max/Math.min?

    If you want to sort the array, you can use sort method:

    var sorted = [3, 1, 6, 2].sort(); // sort ascending  
    var sorted = [3, 1, 6, 2].sort(function(a, b){
        return b - a;
    }); // sort descending 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sort arrays of primitive types in descending order Java : How to
I have a number of very long arrays. No run-time sort is possible. It
Is it possible to sort a TreeView collection using IComparer<T> and not IComparer ?
Possible Duplicate: How to sort arrays inside an array? Sort an array by a
Is it possible to sort an array of values using a specific collation in
Possible Duplicate: Javascript Array.sort implementation? I want to know how .sort() works in JavaScript.
So I have been working on a few projects using audio and images from
Possible Duplicate: Using Perl, how can I sort an array using the value of
Possible Duplicate: How can I sort arrays in php by a custom alphabet? For
Possible Duplicate: How to sort arrays inside an array? Probably this is easily accomplished

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.