-2

How can I sort array.length numerically? In this image my numbers generator should be ordered in descending order.

I tried creating a new array with the push function. My code is as follows:

var newArray = [];
newArray.push(myArray.length);

Unfortunately this doesn't work. I'm a beginner in javascript and I haven't been able to find another solution.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<script src="hack.js" defer></script>
	</head>
<body>
	<div id="demo" style="width: 500px;border: 1px solid black;"></div>
  <script>
    var myNumbers = '';
for (var i = 10; i <= 40; i++) {
	var myArray = [];
	for (var j = 2; j < i; j++) {
		if (i % j == 0) {
			myArray.push(j);	
		}
	}
	myNumbers += "<p>" + i + " number of generators = " + myArray.length + '</p>';

}

document.getElementById("demo").innerHTML = myNumbers;
  </script>
</body>
</html>

2
  • You can make a multi-dimensional array with each index being [number, generator] and then just use array.sort(function(a,b) { return a[1] - b[1] }) then print out your result. Commented Aug 17, 2017 at 21:21
  • Possible duplicate of JavaScript array cloning Commented Aug 17, 2017 at 21:42

2 Answers 2

1
var myNumbers = [];
for (var i = 10; i <= 40; i++) {
    var myArray = [];
    for (var j = 2; j < i; j++) {
        if (i % j == 0) {
            myArray.push(j);    
        }
    }
    var value = { int: i, length: myArray.length, html:"<p>" + i + " number of generators = " + myArray.length + '</p>' }
    myNumbers.push(value);

}
myNumbers.sort(function(a, b){
  return b.length - a.length;
});

document.getElementById("demo").innerHTML = myNumbers.map(function(d) { return d['html']; }).join('')
Sign up to request clarification or add additional context in comments.

2 Comments

this is not the thing i want ... in the right side of the pic , i write the way i want
ahh. DIdn't see the pic. You can upload those into the question instead of an external link.
0

first you can store mappings of numbers and their generator count in an array of objects. Then you can sort this array using a comparator function. The iterate over this array add elements based on this sorted array.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<script src="hack.js" defer></script>
	</head>
<body>
	<div id="demo" style="width: 500px;border: 1px solid black;"></div>
  <script>
    var myNumbers = '', myArray = [];
for (var i = 10; i <= 40; i++) {
    var count = 0;
	for (var j = 2; j < i; j++) {
		if (i % j == 0) {
			count++;
		}
	}
    myArray.push({number: i, generators: count});
}
myArray.sort(function(a,b){
    return b.generators - a.generators;
});
for(var i=0; i<myArray.length; i++)
    myNumbers += "<p>" + myArray[i].number + " number of generators = " + myArray[i].generators + '</p>';

document.getElementById("demo").innerHTML = myNumbers;
  </script>
</body>
</html>

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.