Peter Stark
Peter Stark

Reputation: 77

JavaScript Sorting a Function That Has multiple Numbers

I am trying to create a Random Lottery Number Generator code but I am having issues sorting the numbers from lower to greater. Here is my code of getting the numbers but I can't seem to figure out how to sort them.

function ball(){
  let ball = Math.ceil(Math.random() * 70);
  console.log(ball);
}

function whiteBalls(){
  for(let i = 1; i <= 5; i++){
    ball();
  }
}

whiteBalls();

I've tried many different ways but keep getting errors. Thank you in advance.

Upvotes: 1

Views: 44

Answers (1)

user9706
user9706

Reputation:

Store the result in an array and sort the array:

function ball() {
    return Math.ceil(Math.random() * 70);
}

function whiteBalls() {
    let result = []
    for(let i = 0; i < 5; i++) {
        result.push(ball());
    }
    return result.sort()
}
 
console.log(whiteBalls());

Upvotes: 2

Related Questions