Can you help me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you help me?

I'm trying to run this code, but I don't know why my array don't save more than one element and my second button don't show anything, here's my code: https://code.sololearn.com/W0anxyyqIIp3/?ref=app Thanks a lot if you can tell me where is my mistake and if you can teach me :3

7th Sep 2020, 8:01 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
9 Answers
+ 3
So its similar, just remove your arguments in the mostrar function Since your array is a global variable you can access it inside the function. Function mostrar(){ Alert(arr[0]); }
7th Sep 2020, 10:04 PM
Robert Atkins
Robert Atkins - avatar
+ 3
You have two return statements in your js code, when you return, it ends the function, so everything after your return arr[control] isn't done also, not too familiar with JS, but the control that you increment inside of your function shouldnt affect the control variable outside of your function so say var control = 1; control here won't change, because you are only passing the value of control, not a reference to the variable control myfunc(var control){ control++ this control is a local variable that stops existing outside of the function. } look up passing arguments by reference in JS for more info. though changing values outside of the function from within a function is generally frowned upon https://medium.com/nodesimplified/javascript-pass-by-value-and-pass-by-reference-in-javascript-fcf10305aa9c
7th Sep 2020, 9:09 PM
Robert Atkins
Robert Atkins - avatar
+ 3
https://code.sololearn.com/W4513bo4y2LE/?ref=app So you dont need to pass control as an argument since its a global, and you can directly change it within the function
7th Sep 2020, 9:47 PM
Robert Atkins
Robert Atkins - avatar
+ 3
Jesús Emmanuel Justo González you don't need to pass any arguments or specify any parameter if you are already defining arr and control outside function in the global scope, control value is 1 now ,and at index 1 of arr there is no value
7th Sep 2020, 10:13 PM
Abhay
Abhay - avatar
+ 1
You dont need it, what i was getting at is that the way your guardar function is written will not affect the variable control that exists outside of it, give me a sec and I'll try and help out a little more
7th Sep 2020, 9:27 PM
Robert Atkins
Robert Atkins - avatar
+ 1
Oh my God, thank you so much, now I finally can make my code works, thanks a lot :3 you're amazing Robert Atkins
7th Sep 2020, 10:33 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
0
Should I declare myfunc (var control) inside or outside the function guardar?
7th Sep 2020, 9:23 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
0
Thanks bro, now just a last question, Can I use the values of the array outside the function? Because (supposedly) I was wanting to save usernames and then show all of them using the function Mostrar, but only appears "undefined"
7th Sep 2020, 9:56 PM
Jesús Emmanuel Justo González
Jesús Emmanuel Justo González - avatar
0
No problem, glad to help, probably could of done so a little better if I knew more about js :D
7th Sep 2020, 10:43 PM
Robert Atkins
Robert Atkins - avatar