+ 7
How to set value of a variable on button click
like in a calculator when u tap 1 it shows 1 but the only exception is that I need only 4 digits and I need to assign each digit to each variable, like if first digit is 1(or any other no.) then its variable should be num1 and for second digit num2 , third digit num3 and lastly fourth digit num4 can anybody help?
35 Réponses
+ 3
what about this.
<button onclick="assign(1);" >num1</button>
<button onclick="assign(2);" >num2</button>
in JavaScript
var x=0;
var arrs = [];
function assign(val){
arrs[x] = val;
x++;
}
now each array is new val;
+ 7
its not that simple
+ 7
by your function I can set the variable of num1 what about num2
+ 7
won't help it either
+ 7
what if user wants to assign 2 as num1
we don't know I guess we should use if-elseif-else
here
+ 7
no no not actually a calculator
just give me that part of code rest would be done by me
+ 7
its a code cracker game
+ 7
The user has to guess the no. through a series of hints
+ 7
here is half coded version by me
https://code.sololearn.com/WW7ViFM55ft0/?ref=app
BTW I know that assign function is not working
+ 7
alright I will try it
+ 7
still not working :'(
+ 7
yeah that's the problem
+ 7
could you specify?
+ 7
so do I take it out of it?
+ 7
BTW every time I add the window. onload function my every web code doesn't work idk y?
+ 7
so where should be the window onload function?
+ 7
okay
+ 7
Thx again
+ 6
Thx it works now
+ 2
no window load is correct but you should not have function inside it.