1

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.

1
  • 1
    Use an array and then sort it. Then, loop through and print it afterwards. Commented Dec 26, 2020 at 5:05

1 Answer 1

2

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your quick response/answer!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.