skip to Main Content

I help organise a ‘golf society’ and each week we make a draw to decide who is playing with who, by pulling numbers out of a’ hat’. I am in the process of building a website (app) and want to add a draw function.

The web app is written in NextJS and uses Supabase for auth and member / event data.

So the parameters are:

Total numbers: 4, (not 5), 6 …… up to 32

slot size: 4 or 3 (i.e. preferably 4 i.e teams of 2 x 2 players but can be 3. (How 3 players work is a golf issue not a coding issue)

Table order: The ‘3 balls’ need to be at the top of the table.

My data: Is drawn from Supabase. The query filters on a date and only picks players who have said they will play. A function then adds a random number to each of the players. The final data is an array of ‘members’. Each member being an object including {name: bill, rnd: 0.5565789}

Logic

If the leftOver (see code below) is 3, then I need 1 x 3 players in a row (each with a blank field under column 4) and the rest rows in 4 filled columns.

If the leftOver is 2, then I need 2 rows x 3 columns (each with a blank field under column 4) and the rest in 4 filled columns.

If the leftOver is 1, then I need 3 rows x 3 columns (each with a blank field under column 4) and the rest in 4 filled columns.

If the leftOver is 0, then all rows are all 4 filled columns.

Initial code below. I need to dynamically fill a table with rows of 3 or 4 filled by member.names using the random number to order them.

I am sure I can do it (in a long laborious and messy way) but I am looking for a code efficient solution and hope someone can help.

Thank you

My baseline code is:

const date = await getLatestDate()
        if (!date) { throw Error }

        const members = await getMembers(date)
        if (!members) { throw Error }

        const playingMembers: Member[] = members.filter(
            (members) => members.playing == true)

        const numberPlaying: number = playingMembers.length

        // CODE: won't work with <3 or 5

        playingMembers.forEach(element => {
            element.rnd = Math.random()
        })

        const leftOver = numberPlaying % 4
        

2

Answers


  1. Chosen as BEST ANSWER

    Thank you, Barry.

    Here is my solution. If anyone can imporve with coding best practice I would love to hear from you.

    export async function getDrawData() {
    
    try { //TOAST MOST OF THESES ERRORS
    
        const date = await getLatestDate()
        if (!date) throw Error('Could not find latest date. Please note: draw/actions.tsx')
    
        const members = await getMembers(date)
        if (!members) throw Error('Could not get list of members from database. Please note: draw/actions.tsx')
    
        const playingMembers: Member[] = members.filter(
            (members) => members.playing == true)
    
        const numberPlaying: number = playingMembers.length
    
        if (numberPlaying == 5 || numberPlaying <= 3) {
            throw Error('Could not get list of members from database. Please note: draw/actions.tsx')
            ///   TOAST ?
        }
    
        playingMembers.forEach(element => {
            element.rnd = Math.random()
        })
    
        const dummyMember: Member = {
            id: "none",
            name: 'Pick a Middleman',
            admin: false,
            active: false,
            date: "none",
            playing: true,
            dining: false,
            buggy: false,
            loggedUser: {
                loggedID: "none",
                loggedName: " ",
                loggedisAdmin: false,
            },
            rnd: 0
        }
    
        const leftOver = numberPlaying % 4
        //console.log('Left over in draw: ', leftOver)
        switch (leftOver) {
            case 1:
                //code i.e. 9, 13 etc
                // 3 groups of 3 
                playingMembers.splice(3, 0, dummyMember)
                playingMembers.splice(7, 0, dummyMember)
                playingMembers.splice(11, 0, dummyMember)
                break
            case 2:
                // code i.e 6, 10 etc
                // 2 groups of 3
                playingMembers.splice(3, 0, dummyMember)
                playingMembers.splice(7, 0, dummyMember)
                break
            case 3:
                // code i.e 7, 11 etc
                // 1 groups of 3
                playingMembers.splice(3, 0, dummyMember)
                break
            default: // i.e. 4, 8 etc 
            // no change to playingMembers
            // Do I need default ?
        }
    
        const numberCompetitors: number = playingMembers.length
        const leftOverCompetitors = numberCompetitors % 4
        if (leftOverCompetitors > 0) {  // double check that after adding blanks, competitors are divisible by 4
            console.log('Cannot make draw table.  leftOverCompetitors in draw actions.tsx =: ', leftOverCompetitors)
            throw Error('Something went wrong creating draw.   Please note: draw/actions.tsx')
        }
    
        let slots: slots = []
    
        for (let i = 0; i < (numberCompetitors / 4); i++) {
            const slot: slot = playingMembers.slice(i * 4, (i * 4) + 4)
            slots.push(slot)
        }
        return slots
    } catch {
        return null
    }
    

    }


  2. The following code will help you calculate the number of 3 and 4 in C.

    #include <stdio.h>  
    
    
    void verifyAndPrintCombination(int num) {  
        int fours = 0;  
        int threes = 0;  
        int remainder = 0;  
    
        fours = num / 4;  
        remainder = num % 4;  
          
            if (remainder == 1) {  
             
                threes=3; 
                fours=(num-threes*3)/4;
              
            } else if (remainder == 2) {  
            
                threes = 2;
                fours=(num-threes*3)/4;  
      
            } else if (remainder == 3) {  
         
                threes =1;  
            }  
            
    
        if (fours * 4 + threes * 3 == num&&fours>=0) {  
            printf("%d can be composed of %d four and %d three. n", num, fours, threes);  
        } else {  
            printf("%d cannot be composed with four and three . n", num);  
        }  
    }  
    
    int main() {  
        int number;  
    
        printf("input a number greater than 3:");  
        scanf("%d", &number);  
    
        if (number < 3) {  
            printf("input a number greater than 3:n");   
        }  
    
        verifyAndPrintCombination(number);  
    
        return 0;  
    }
    

    My English is poor, so it is hard for me to write comments.(Translation is tedious)
    The max value of number three is 3, You only need to discuss four scenarios.
    I can just help you with the core part of the requirement because i am not good in java(and i am too lazy to learn right now ,ditto), I believe you can refine the rest of the code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search