java script game problem

hello
12 Replies
NIMA
NIMAOP9mo ago
'''js <body> <input type="number" placeholder="put" id="getter" > <button onclick="submiter()" >do it</button> <p id="shower"> </p> <p id="brande"> </p> <script> let text = ''; ar = [1,2,3,4,5,6] function submiter(){ let getter = document.getElementById('getter').value; let random = ar[(Math.floor(Math.random() * ar.length))]; if(getter === random ){ text = random; document.getElementById('brande').innerHTML = 'brande shodid '; }else{ alert('bakhti') } document.getElementById('shower').innerHTML = text; } </script>
</body> ''' why when im using === not work? i want to something when i put correct random number in input it use it
Jochem
Jochem9mo ago
=== does a strict comparison, which includes type. .value always returns a string, but your array has integers in it. So while 1 == '1'; //true, 1 === '1'; //false
NIMA
NIMAOP9mo ago
hmm i dont get it i cant use value with ===?
Jochem
Jochem9mo ago
you can, but you'd have to compare against other strings, or convert your string into an integer or just use ==, that's probably fine in this case anyway
NIMA
NIMAOP9mo ago
i use == not work ohh it work lol i was not lucky after 30 time attembd it alwse get another number tnx for help == work
Jochem
Jochem9mo ago
https://javascript.info/comparison here's more info on comparisons and strict comparisons
NIMA
NIMAOP9mo ago
Tnx
MarkBoots
MarkBoots9mo ago
btw fyi, document.getElementById('getter').valueAsNumber would return a number
glutonium
glutonium9mo ago
I didn't even know valueAsNumber is even a thing lol
Jochem
Jochem9mo ago
I always forget it exists
Tenkes
Tenkes9mo ago
same
Want results from more Discord servers?
Add your server