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>