Question on arrays (edit:PLEASE HELP PROJECT DUE TOMORROW)

Hi eyerybody thankyou for the feedback
I’m new to wix (and to coding)
My family does a present exchange every year where we put all the names in a little basket and then each person pulls a name out and that’s who they give their present to, except they can’t be from their family.
So I made an array with all the names, and then they click on a button and a name comes up and then i ask them (and i trust them) if that person is from their family. if that person is, than they do it again, if they’re not, then i delete that person from the array.
So it works fine except that when you reload the page the array goes back to what it was.
is there a way that the array is permanently affected? is there an alternative?
thanks again

here’s my code (i speak spanish: familia=family; si=yes; no=no; the names are names; textcris is the textbox where the name appears)

var familia = [“¡BABY!”,“¡RICK!”,“¡CARLOS!”,“¡MIRNA!”,“¡NORI!”,“¡ARTURO IPIENS!”,“¡MIGUE!”,“¡BEATRIZ!”,“¡TOTOY!”,“¡ANY!”,“¡GUILLERMO!”,“¡MARIA JULIA!”,“¡ALEJO!”,“¡PEPE!”,“¡BRIGIT!”,“¡HANNAH!”,“¡DIEGO!”,“¡GABY!”,“¡CARLANGAS!”,“¡MAGGIE!”,“¡CARLITITOS!”,“¡JOSE LUIS!”,“¡NANCY!”,“¡REGINA!”,“¡ALE!”,“¡PABLO!”,“¡JULIETA!”]
var rand = Math.floor(Math.random()*familia.length);
$w.onReady( function () {
}

export function button1_click(event) {
$w(‘#textcris’).text = familia[rand];
$w(‘#asignar’).hide();
$w(‘#si’).show();
$w(‘#no’).show();
$w(‘#text1’).show();
$w(‘#textcris’).show();
}
export function no_click(event) {
familia.splice(rand, 1);
$w(‘#si’).hide();
$w(‘#no’).hide();
$w(‘#text1’).hide();
//$w(‘#textcris’).hide();
$w(‘#ok’).show();

}
export function si_click(event) {
//Add your code for this event here:
rand = Math.floor(Math.random()*familia.length);
$w(‘#asignar’).show();
$w(‘#textcris’).hide();
$w(‘#si’).hide();
$w(‘#no’).hide();
$w(‘#text1’).hide();
}

Redirections on similar questions are also welcome

PLEASE HELP PROJECT DUE TOMORROW

You can save the last random value setting using storage.setItem() . Then in the onReady() function, retrieve the value using storage.getItem() , and set the array using the value you just retrieved.

1 Like

WOW thank you very much! I’ll try it and get back to you

so how does this solve my problem? am I restting the whole array each timme in the onReady() function?
thank you very much

I don’t understand how the storage thing works very well could you give an example?
thank you

thank you in advance
@yisrael-wix — I really did not understand but this is what I came up with
import {storage,local} from ‘wix-storage’;
$w.onReady( function (){
storage.setItem(local, rand);
})

storage.setItem(0,“¡BABY!”);
storage.setItem(1,“¡RICK!”);
storage.setItem(2,“¡CARLOS!”);
storage.setItem(3,“¡MIRNA!”);
storage.setItem(4,“¡NORI!”);
storage.setItem(5,“¡ARTURO IPIENS!”);
storage.setItem(7,“¡MIGUE!”);
storage.setItem(8,“¡BEATRIZ!”);
storage.setItem(9,“¡TOTOY!”);
storage.setItem(10,“¡ANY!”);
storage.setItem(11,“¡GUILLERMO!”);
storage.setItem(12,“¡MARIA JULIA!”);
storage.setItem(13,“¡ALEJO!”);
storage.setItem(14,“¡PEPE!”);
storage.setItem(15,“¡BRIGIT!”);
storage.setItem(16,“¡HANNAH!”);
storage.setItem(17,“¡DIEGO!”);
storage.setItem(18,“¡GABY!”);
storage.setItem(19,“¡CARLANGAS!”);
storage.setItem(20,“¡MAGGIE!”);
storage.setItem(21,“¡CARLITITOS!”);
storage.setItem(22,“¡JOSE LUIS!”);
storage.setItem(23,“¡NANCY!”);
storage.setItem(24,“¡REGINA!”);
storage.setItem(25,“¡ALE!”);
storage.setItem(26,“¡PABLO!”);
storage.setItem(27,“¡JULIETA!”);
export function page1_viewportEnter(event) {
//Add your code for this event here:
storage.setItem(local, rand);
}
export function asignar_click(event) {
//Add your code for this event here:
$w(‘#textcris’).text = storage.getCurrentItem();
$w(‘#asignar’).hide();
$w(‘#si’).show();
$w(‘#no’).show();
$w(‘#text1’).show();
$w(‘#textcris’).show();
}
//var familia = [“¡BABY!”,“¡RICK!”,“¡CARLOS!”,“¡MIRNA!”,“¡NORI!”,“¡ARTURO IPIENS!”,“¡MIGUE!”,“¡BEATRIZ!”,“¡TOTOY!”,“¡ANY!”,“¡GUILLERMO!”,“¡MARIA JULIA!”,“¡ALEJO!”,“¡PEPE!”,“¡BRIGIT!”,“¡HANNAH!”,“¡DIEGO!”,“¡GABY!”,“¡CARLANGAS!”,“¡MAGGIE!”,“¡CARLITITOS!”,“¡JOSE LUIS!”,“¡NANCY!”,“¡REGINA!”,“¡ALE!”,“¡PABLO!”,“¡JULIETA!”]
var rand = Math.floor(Math.random()*27);

export function no_click(event) {

storage.remove(); 
$w('#si').hide(); 
$w('#no').hide(); 
$w('#text1').hide(); 

//$w(‘#textcris’).hide();
$w(‘#ok’).show();
}

export function si_click(event) {
//Add your code for this event here:
rand = Math.floor(Math.random()*27);
storage.setItem(local, rand);
$w(‘#asignar’).show();
$w(‘#textcris’).hide();
$w(‘#si’).hide();
$w(‘#no’).hide();
$w(‘#text1’).hide();
}

can you correct me please?
otherwise can you give me some examples?

this is what the developper console says btw (on preview)
Loading the code for the HOME page. To debug this code, open c1dmp.js in Developer Tools.
There was an error in your scriptTypeError: Cannot read property ‘setItem’ of undefined(Line 6)
TypeError: Cannot read property ‘setItem’ of undefined (Line 3)

@intercambiocrissanta You are getting the error because you are trying to get a value from storage that doesn’t yet exist - you haven’t yet saved the value to storage.

You don’t need to save the entire array in storage. What you need to do is after you calculate a random number, save the random number in storage to be used later.

rand = Math.floor(Math.random()*familia.length);
storage.setItem("random number", "" + rand);    // save as a string

Then, next time you want to randomly select an item from the array, you get the random number from storage. Values are stored in storage as strings, so you will need to convert the returned string value to a number. A couple of examples using your original code:

rand = Number(storage.getItem("random number"));
$w('#textcris').text = familia[rand];
rand = Number(storage.getItem("random number"));
familia.splice(rand, 1);

I hope this helps