+ 1

Can one function change the different variables that are put into it?

So I need one function to change each variable that is put into it. This is for a Choose Your Own Adventure Game and I need it to map each individual ending. My solution is to have a different variable for each ending and have it only work once, so the counter only goes up once, regardless of how many times that ending is reached. And yes, I know I am using document.write and document.open but those are what works best for me. (I don't think those are the problem) The code looks like this: let counterfour = true let counter = 0; function isFalse(theVar) { if(theVar) { theVar = false; counter = counter + 1; //Could be counter++ } else if(theVar === false) { //Nothing } } and then: function func4() { document.open(); document.write("You make it out safely. The next day, you read a newspaper article saying the theatre just experienced a brief power outage. You continue on with your life, still attending that theatre every week."); isFalse("counterfour"); addButton("The End!", "end"); }

7th Mar 2019, 3:36 PM
Archos
Archos - avatar
6 Réponses
+ 3
The normal way to shove around values between functions are return values. function f(arg) { // modify arg return arg; } And then call the function like this: arg = f(arg); You pass arg to the function, it does stuff with it and delivers it right back to the point where you called the function. (You could also store it in another variable or use it directly.) Can you solve your problem like this?
7th Mar 2019, 4:18 PM
HonFu
HonFu - avatar
+ 4
Try changing isFase(".....") with isFalse(counterfour)
7th Mar 2019, 4:13 PM
Seniru
Seniru - avatar
+ 2
HonFu The function solution worked! Thank you so much!
7th Mar 2019, 5:40 PM
Archos
Archos - avatar
+ 1
You can use an array for that. Arrays are a reference type, which - in short - means you can use it's methods directly in the function, like push, pop, includes or whatever. Then you don't need a return value.
7th Mar 2019, 4:21 PM
HonFu
HonFu - avatar
+ 1
You can use an object to set a key, pass the object as function argument, then function change the key. var counterfour = {flag: true}; function setFalse(theVar) { theVar.flag = false; } setFalse(counterfour); console.log(counterfour.flag);
8th Mar 2019, 7:17 AM
Calviղ
Calviղ - avatar
0
I'm happy to hear that! Good luck going from here! :)
7th Mar 2019, 5:49 PM
HonFu
HonFu - avatar