1

I generated an array with 10 random numbers and I need to sort out these numbers into two new arrays, depending if the generated number is inferior or superior (or equal to 50). My solution seemed to work fine at first but then I noticed the first number generated is always missing after I sorted it. I tried different things but no luck so far... Any recommendation? Thanks!

const arr = Array(10)
  .fill()
  .map(() => Math.round(Math.random() * 100));

console.log(arr);



let arr1 = [];
let arr2 = [];

arr.sort((a) => {
  if (a < 50) {
    arr1.push(a);
  } else {
    arr2.push(a);
  }
});

document.getElementById("arrPrint").innerHTML = JSON.stringify(arr1);

document.getElementById("arrPrint2").innerHTML = JSON.stringify(arr2);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <pre id="arrPrint"></pre>
  <pre id="arrPrint2"></pre>
  <script src="./index.js"></script>
</body>

</html>
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
Hector
  • 11
  • 1
  • 1
    This is ***not*** how sorting works at all. Sorting is about getting one array and re-ordering it. You're trying to use `.sort()` to loop over the array which goes way beyond [Is performing a mapping operation without using returned value an antipattern?](https://stackoverflow.com/q/56903693) since *at the very least*, `.map()` will go through each element. `.sort()` will not if it determines the array is sorted earlier. Moreover, you're not even using both the arguments that the sorting callback expects. – VLAZ Sep 21 '22 at 07:53
  • 3
    Why use `sort`? You need `forEach` ? – Simone Rossaini Sep 21 '22 at 07:53

5 Answers5

0

You have to sort and place in arrays as two separate actions. First sort the array, then iterate through the sorted array with arr.forEach

arr = arr.sort();

arr.forEach((a) => {
  if (a < 50) {
    arr1.push(a);
  } else {
    arr2.push(a);
  }
});
0

Please try the following code. In here, sort function still in the code, then loop the array using forEach

const arr = Array(10)
  .fill()
  .map(() => Math.round(Math.random() * 100));

let arr1 = [];
let arr2 = [];

arr.sort();

arr.forEach((a) => {
  if (a < 50) {
    arr1.push(a);
  } else {
    arr2.push(a);
  }
})

console.log(arr1)
console.log(arr2)
MONSTEEEER
  • 544
  • 6
  • 23
0

lodash if you don't mind.

Please pay attention how you can fill array with random numbers.

Live Demo:

const arr = Array.from({length: 10}, () => Math.floor(Math.random() * 100));

const [arr1, arr2] = _.partition(arr, (num) => num < 50);

console.log("arr1: ", ...arr1);
console.log("arr2: ", ...arr2);
.as-console-wrapper { max-height: 100% !important; top: 0 }
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

try put the filter in variable

const arr = Array(10)
  .fill()
  .map(() => Math.round(Math.random() * 100));

console.log(arr);

arr.sort()

let arr1 = arr.filter(int=> int < 50)
let arr2 = arr.filter(int=> int >= 50)

console.log(arr1)
console.log(arr2)
Feis
  • 51
  • 3
0
const getRandomNo = max => {
    return Math.floor(Math.random() * max) + 1;
};
const randomNos = Array.from({ length: 10 }).map(el => {
    return getRandomNo(100);
});

//sort the numbers
const nosGreaterThan50 = randomNos.filter(no => {
    return no > 50;
});

//get the number less than 50
const nosLessThan50 = randomNos.filter(no => {
    return no < 50;
});

console.log(nosGreaterThan50);

console.log(nosLessThan50);