Help with Arrays

All about the past, current and future webteases and the art of webteasing in general.
---
Post Reply
User avatar
edger477
Experimentor
Experimentor
Posts: 1114
Joined: Mon Nov 29, 2021 8:24 pm
Location: underfloor

Re: Help with Arrays

Post by edger477 »

Your while loop will enter first time because the condition is true (the done_encounters contains current value of next_encounter=0 because you pushed it in initialization), and it will be executed once where it will assign it some value between one and number of encounters, since done_encounters at that moment only has one element, [0] - the while will end since it is false now. So you will always end up with an array of 2 elements in done_encounters which will be [0, some_random_number].

If you want to have an array of encounters, each appearing only once, then you can use Fisher-Yates algorithm to randomize an order of array of n encounters. First you have to have all of them in an array (you can also exclude some based on certain condition), then shuffle them:

Code: Select all

function getRandomArray(n) {
    // Step 1: Initialize the array with numbers from 0 to n
    const arr = [];
    for (let i = 0; i <= n; i++) {
        arr.push(i);
    }

    // Step 2: Shuffle using Fisher-Yates algorithm
    for (let i = arr.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1)); // Random index
        [arr[i], arr[j]] = [arr[j], arr[i]]; // Swap elements
    }

    return arr;
}

// Example usage:
done_encounters = getRandomArray(9);
console.log(done_encounters ); // i.e. (10) [3, 6, 8, 0, 5, 1, 2, 9, 4, 7]
In this case you would have randomized encounters in "done_encounters" array, but you could save it to some like "pending_encounters" and you can do
next_encounter = pending_encounters.pop(); // this will take last element from the array and assign it to next_encounter
and you can do while (pending_encounters.length>0) since each time you do pop, the element is removed from array.
My estim creations: https://mega.nz/folder/73pxmBBQ#X6ylDzRafzTt9wanZ0dacw
And in E-Stim Index: viewtopic.php?t=27090

Try creating your own estims with my restim script generator!
Spoiler: show
You can also thank me with crypto: https://trocador.app/anonpay?ticker_to= ... e+a+coffee
User avatar
edger477
Experimentor
Experimentor
Posts: 1114
Joined: Mon Nov 29, 2021 8:24 pm
Location: underfloor

Re: Help with Arrays

Post by edger477 »

Your logic seems wrong...
To get each encounter only once, better way is to store them in random order in pending_encounters variable, that way you just take from that pile, and you don't need to do while loop until you find one that hasn't happened, that is very inefficient, imagine having 100 and after 99 you have 1% chance to get the remaining one, your while loop would spin for a while until you hit that one.

Based on what you said what you want I generated this with chatgpt (was faster than writing by hand, adjust as needed):

Code: Select all

// Constants
const number_of_encounters = 10;
let pending_encounters = [];
let done_encounters = [0];
let next_encounter = 0;

// Initialize pending_encounters with 1..10 in random order
function initializePendingEncounters() {
    pending_encounters = Array.from({ length: number_of_encounters }, (_, i) => i + 1); // this creates list with sequential items

    // Shuffle using Fisher-Yates algorithm
    for (let i = pending_encounters.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [pending_encounters[i], pending_encounters[j]] = [pending_encounters[j], pending_encounters[i]];
    }
}

// Function to get the next encounter
function get_next_encounter() {
    if (pending_encounters.length === 0) {
        console.log("No more encounters remaining!");
        return null;
    }

    const value = pending_encounters.shift(); // Take first element
    done_encounters.push(value);
    next_encounter = value;

    return value;
}

// Example usage:
initializePendingEncounters();
console.log("Pending encounters:", pending_encounters);

console.log("Next encounter:", get_next_encounter());
console.log("Updated pending:", pending_encounters);
console.log("Done encounters:", done_encounters);
console.log("Current next_encounter:", next_encounter);
Explanation:

pending_encounters starts as an empty array and gets filled with numbers 1..number_of_encounters in random order using Fisher-Yates shuffle.

done_encounters starts with [0] as requested.

get_next_encounter():
Pops the first element from pending_encounters (FIFO order).
Pushes it to done_encounters.
Updates next_encounter.
Returns the value.


You could place the code from initializePendingEncounters function directly into initialization block, so you don't need the function (since you only need it to run once).
My estim creations: https://mega.nz/folder/73pxmBBQ#X6ylDzRafzTt9wanZ0dacw
And in E-Stim Index: viewtopic.php?t=27090

Try creating your own estims with my restim script generator!
Spoiler: show
You can also thank me with crypto: https://trocador.app/anonpay?ticker_to= ... e+a+coffee
Majikthise
Explorer
Explorer
Posts: 25
Joined: Thu Feb 23, 2023 10:43 pm
Gender: Male
Sexual Orientation: Straight
I am a: Switch

Re: Help with Arrays

Post by Majikthise »

Hello,

I have no idea how to write code for that (or much of anything, really); but I do have a suggestion that may prove useful.

A tease that uses the mechanism that I think you are looking for is available here: https://milovana.com/webteases/showtease.php?id=52376

You may be able to get some ideas from the way that code is written. If you have further questions, JBK (the author) has been very responsive.

Good luck!
MarlowePierce
Curious Newbie
Curious Newbie
Posts: 1
Joined: Tue Aug 12, 2025 5:14 am

Re: Help with Arrays

Post by MarlowePierce »

Exploring new experiences often requires both patience and curiosity, which is clear from your post. Many users here experiment to find what works best for them, almost like testing features at hitnspin, where discovery is part of the enjoyment. Sharing updates on your progress will help others learn and improve their own approaches.
Last edited by MarlowePierce on Wed Aug 20, 2025 7:04 am, edited 1 time in total.
AMX4
Explorer
Explorer
Posts: 9
Joined: Sun Sep 01, 2024 5:51 am
Gender: Nonbinary
Sexual Orientation: Bisexual/Bi-Curious
I am a: Switch

Re: Help with Arrays

Post by AMX4 »

I'm a bit late, but I hope I can still help. :-)
OP's algorithm is logically sound and works if you punch it into your browser's console (F12 -> Console).
The only issue that I see is that it will go into an infinite loop if all of the options are exhausted. I suggest adding a safeguard against that case, just to be sure.
The source of OP's problem seems to be rooted outside of the posted code.
Post Reply