java script game problem

hello
12 Replies
NIMA
NIMA3mo 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
Jochem3mo 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
NIMA3mo ago
hmm i dont get it i cant use value with ===?
Jochem
Jochem3mo 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
NIMA3mo 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
Jochem3mo ago
https://javascript.info/comparison here's more info on comparisons and strict comparisons
NIMA
NIMA3mo ago
Tnx
MarkBoots
MarkBoots3mo ago
btw fyi, document.getElementById('getter').valueAsNumber would return a number
glutonium
glutonium3mo ago
I didn't even know valueAsNumber is even a thing lol
Jochem
Jochem3mo ago
I always forget it exists
Tenkes
Tenkes3mo ago
same