Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:08:15+00:00 2026-06-02T10:08:15+00:00

I’m trying to create a raffle ticket picking script using JS and jQuery. So

  • 0

I’m trying to create a raffle ticket picking script using JS and jQuery.

So far so good – my script works. Now however I’d like to make it much more visual so that we can run it in assembly.

I have a JS Object which takes the following format:

var TempObj = { 'Student_ID' : i, 'Student_Name' : data.users[i].Student_Name };
RewardPurchases.PurchasesArray[Count] = TempObj;

I’m then using this code to randomly select one of the students:

$('button#random').click( function() {
    // "Total" is just the array length
    var Num = Math.floor(Math.random() * Total+1);
    Num--;

    // prove that the system has picked a random number out of the list
    alert("Random number out of " + Total + " is..." + Num);

    // find the array entry where the key is our random "Num"
    for (var i in RewardPurchases.PurchasesArray) {
        if (i == Num) {
            // we've found our entry, now we need more information about the corresponding student
            var TutorGroup = '';

            Frog.API.get('timetable.getClasses',
            {
                'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
                'onSuccess': function(data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].subject.name == "Tut Period") {
                            TutorGroup = data[i].name.replace("/Tp", "");
                        }
                    }
                }
            });

            // print out the student's information - he or she is the winner!
            alert("Ticket number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")");
        }
    }
} );

I would like to – very briefly – display each student’s name until the ticket has been picked (i.e. RewardPurchases.PurchasesArray[Num] has been found), then I’d like it to stop and increase the font size (and probably say WINNER! above it or something).

Is this possible, and relatively simple, using jQuery?

EDIT

I have tried the following code, which simply displays a single name. Where am I going wrong?

$('button#random').click( function() {
    var Num = Math.floor(Math.random() * Total);

    for (var i in RewardPurchases.PurchasesArray) {

        if (typeof RewardPurchases.PurchasesArray[i] === 'object' && typeof RewardPurchases.PurchasesArray[i] !== null) {

            $('#display').queue( function() { 
                $(this).text(RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase()).show().delay(250); 
                $(this).dequeue();
            } );

        }

        if (i == Num) {
            var TutorGroup = '';

            Frog.API.get('timetable.getClasses',
            {
                'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
                'onSuccess': function(data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].subject.name == "Tut Period") {
                            TutorGroup = data[i].name.replace("/Tp", "");
                        }
                    }
                }
            });

            break;
        }
    }
} );

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:08:17+00:00Added an answer on June 2, 2026 at 10:08 am

    I’ve something working for you available on jsfiddle http://jsfiddle.net/pomeh/63hug/. I’ve removed irrelevant code for the demo, and added some improvements. Look at the code to learn more.

    var students = [
        { 'Student_ID': 0, 'Student_Name': "one" },
        { 'Student_ID': 1, 'Student_Name': "two" },
        { 'Student_ID': 2, 'Student_Name': "three" },
        { 'Student_ID': 3, 'Student_Name': "four" },
        { 'Student_ID': 4, 'Student_Name': "five" },
        { 'Student_ID': 5, 'Student_Name': "six" },
        { 'Student_ID': 6, 'Student_Name': "seven" },
    ];
    
    var $display = $("#display");
    
    $('#random').click(function(){
        var total = students.length,
            selected = Math.floor( Math.random() * total ),
            i = 0;
    
        console.log( "selected", selected );
        $display.animate( {"font-size": "12px"}, 0 );
    
        // improvement: use a for loop, instead of a for..in
        for (i=0; i<total; i++) {
    
            console.log( "for", i );
            // here is the trick, use an Immediately-Invoked Function Expression (IIFE)
            // see http://benalman.com/news/2010/11/immediately-invoked-function-expression/
            setTimeout((function(i){
                return function(){
                    // code here will be delayed
                    console.log( "timeout", i );
                    $display.text( students[i].Student_Name.toUpperCase() );
                    if( i === selected ) {
                        $display.animate( {"font-size": "30px"}, "fast" );
                    }
                };
            }(i)), i*250);
    
            // improvement: triple equal sign, always !
            if( i === selected ) {
                // code here will execute immediately
                break;
            }
        }
    
    });
    

    You could learn more about Javascript variable hoisting here http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting.

    Also, you should check the point 4 of this list of common Javascript confusions http://tobyho.com/2011/11/16/7-common-js-mistakes-or-confusions/

    Hope that’ll help 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.