The lottery system appears to be malfunctioning as critical information is absent

I attempted to create a mini lottery game where 6 unique random numbers are chosen from 49 options. However, my code seems to have some issues. Can you please help me identify the problem and correct it?

function getNumbers(){
    var numbers = [];
    var randomnumber;
  
    while(numbers.length < 6){
            randomnumber = Math.ceil(Math.random()*49)
            if(arr.indexOf(randomnumber) > -1) continue;
            numbers[numbers.length] = randomnumber;
    }

    numbers.sort(sortNumber);
  
    document.getElementById("Ausgabe").innerHTML = numbers;
}

function sortNumber(a,b) {
     return a - b;
}

Answer №1

if(arr.indexOf(randomnumber) > -1)
needs to be changed to numbers.indexOf(randomnumber)

Therefore, arr should be replaced by numbers

function getNumbers(){
    var numbers = [];
    var randomnumber;

    while(numbers.length < 6){
            randomnumber = Math.ceil(Math.random()*49)
            if(numbers.indexOf(randomnumber) > -1) continue;
            numbers[numbers.length] = randomnumber;
    }

    numbers.sort(sortNumber);

    document.getElementById("Ausgabe").innerHTML = numbers;
}

function sortNumber(a,b) {
    return a - b;
}

getNumbers();
<div id="Ausgabe" ></div>

Answer №2

This solution has proven effective for me. Make sure to utilize the numbers array instead of the arr variable.

HTML:

<div id="output"></div>

JavaScript:

function generateNumbers(){
        var numbers = [];
        var randomNum;

    while(numbers.length < 6){
            randomNum = Math.ceil(Math.random()*49)
            if(numbers.indexOf(randomNum) > -1) continue;
            numbers[numbers.length] = randomNum;
    }

    numbers.sort(sortNumbers);

    document.getElementById("output").innerHTML = numbers;
}

function sortNumbers(a,b) {
        return a - b;
}

Answer №3

The other participants seem to have overlooked a significant issue. It appears that you neglected to invoke the function. Haha.

Another mistake is using "arr" instead of "numbers" in the if statement.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent the div from moving beyond the boundaries of its container while anim

After experimenting with a javascript image panner that I put together from various code snippets, I have decided to switch to a simpler approach. However, I need some guidance on a few things. Below is the code for the left and right buttons: <div id ...

What is the reason behind router.base not functioning properly for static sources while running 'npm run build' in Nuxt.js?

Customizing Nuxt Configuration const BASE_PATH = `/${process.env.CATEGORY.toLowerCase()}/`; export default { router : { base : BASE_PATH }, } In addition, there is a static source for an image in the component: <img src="/mockups/macbookpro_01. ...

Invoke the render function once more at the highest level while preserving the ability to make changes

Below is a test that ensures the component is pure (using the PureRenderMixin). However, I'm receiving the following warning message. Warning: setProps(...) and replaceProps(...) are deprecated. Instead, call render again at the top level. it(& ...

Ideas for utilizing jQuery to extract data from CSS files

Hey there, I'm facing an issue on my HTML page where I have jQuery included. In my CSS file, I have quite a lot of lines and I'm trying to figure out how to read all styles for a specific element from the external CSS file rather than the inline ...

Why do React JS array objects reset when being updated?

I am working with an array of objects that contain IDs and prices. Whenever the onClick event is triggered, it updates the price of a specific object. However, upon clicking the event again, I noticed that the previously updated item's price gets rese ...

Applying a class in jQuery to an element when the data attribute aligns with the current slide in Slick

I need to compare the data-group attribute of the current slide to the equivalent attribute in a navigation list. If they match, the anchor element in the navigation list should be given an active class. How can I achieve this using jQuery? Navigation Li ...

Unable to alter state from the onClick event of a dialog button

In the process of developing a Next.js app, I encountered an interesting challenge within one of the components involving a DropdownMenu that triggers a DialogAlert (both powered by Shadcn components). The issue arises when attempting to manage the dialog& ...

The calculation of Value Added Tax does not involve the use of jQuery

My form setup is as follows: <form method="post" action="" id="form-show"> <table class="table table-bordered table-striped table-hover" id='total' style="width:100%;"> ...

What methods can I use to identify my current page location and update it on my navigation bar accordingly?

My latest project involves a fixed sidebar navigation with 3 divs designed to resemble dots, each corresponding to a different section of my webpage. These sections are set to occupy the full height of the viewport. Now, I'm facing the challenge of de ...

Vue watchers capturing original value prior to any updates

When working with Vue.js, we can easily access the value after modification within watchers using the following syntax: watch: function(valueAfterModification){ // ...... } But what about getting the value before it's modified? PS: The official ...

Using Angular JS to dynamically load an HTML template from a directive upon clicking a button

When a link is clicked, I am attempting to load an HTML template. A directive has been created with the templateUrl attribute to load the HTML file. Upon clicking the link, a function is called to append a custom directive "myCustomer" to a pre-existing di ...

Merging various variables together where applicable in JavaScript

I am looking to combine different variables if they exist and return the result. However, I need to separate the values with "-" and only return the variables with a value. Here is my code: var test = ""; var test1 = ""; var test2 = ""; var test3 = ""; ...

What is the most efficient method for storing and accessing a large matrix in JavaScript?

With 10000 items, creating a matrix of 10000 rows * 10000 columns using a one-dimensional array would be massive. Additionally, setting specific values to cells (i,j) where 0 < i, j < 10000 would require a large number of iterations. I am facing cha ...

Is there a discrepancy in the JSON algorithm?

While using google chrome, I encountered the following scenario: I had an object in javascript that looked like this: var b = { text: 'hello world, you "cool"' }; I then used JSON.stringify to convert it to a string and sent it to the dat ...

I'm looking to leverage useEffect in React Native to effectively manage and store arrays - any tips on how

Redux has provided me with data called targetFarm, which contains an array of objects in targetFarm.children. targetFarm.children = [ {name: 'pizza'}, {name: 'burger'}, {name: 'lobster'}, {name: 'water'}, ] I am try ...

Experiencing difficulties with mocha and expect while using Node.js for error handling

I'm in the process of developing a straightforward login module for Node. I've decided to take a Test-Driven Development (TDD) approach, but since I'm new to it, any suggestions or recommended resources would be greatly appreciated. My issu ...

Alignment issue with Bootstrap 4 form fields within inline form group

My experience with Bootstrap 4 on certain pages has been quite challenging, specifically when it comes to aligning fields with labels to the left and maintaining an aligned appearance for input fields. Here is the snippet of my code: <div class="wrap ...

Error: The function req.logIn is not valid

I'm currently in the process of creating a dashboard for my Discord bot, but I've encountered an error that reads as follows: TypeError: req.logIn is not a function at Strategy.strategy.success (C:\Users\joasb\Desktop\Bot& ...

Retrieving information from an object using JavaScript

Hello, I am facing a challenge with extracting data from an object via a web API in ReactJS. It seems like the data returned by the API is not structured properly as a JavaScript object. To view the issue directly in your browser visit: I need assistance ...

What is the best way to verify a set of requests concurrently before proceeding with their execution or flagging an error?

Below is the code I have written for liking a post on my blog website, which involves three phases. The first phase is adding the liked post to the user's list of liked posts, the second phase is adding the like to the post itself, and the third phase ...