Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function randomInteger(min, max) {
- let rand = min - 0.5 + Math.random() * (max - min + 1);
- return Math.round(rand);
- }
- let arrLen = randomInteger(1, 10);
- if (arrLen % 2 !== 0 ) {
- arrLen++;
- }
- let arr = [];
- let posBalance = arrLen / 2;
- let negBalance = arrLen / 2;
- for (let i = 0; i < arrLen; i++) {
- let num = randomInteger(-100, 100);
- if (num > 0) {
- if (posBalance > 0) {
- arr[i] = num;
- posBalance--;
- } else {
- arr[i] = -num;
- negBalance--;
- }
- }
- if (num < 0) {
- if (negBalance > 0) {
- arr[i] = num;
- negBalance--;
- } else {
- arr[i] = -num;
- posBalance--;
- }
- }
- }
- console.log(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement