java script game problem

hello
12 Replies
NIMA
NIMAOP8mo 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
Jochem8mo 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
NIMAOP8mo ago
hmm i dont get it i cant use value with ===?
Jochem
Jochem8mo 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
NIMAOP8mo 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
Jochem8mo ago
https://javascript.info/comparison here's more info on comparisons and strict comparisons
NIMA
NIMAOP8mo ago
Tnx
MarkBoots
MarkBoots8mo ago
btw fyi, document.getElementById('getter').valueAsNumber would return a number
glutonium
glutonium8mo ago
I didn't even know valueAsNumber is even a thing lol
Jochem
Jochem8mo ago
I always forget it exists
Tenkes
Tenkes8mo ago
same
Want results from more Discord servers?
Add your server