Here is my code
var x = [];
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
I simply wanna remove all the elements in the array then add new elements in it.
But when I try to do it with the code above, undefined
is also adding to the array.
How can I prevent it?