Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Random Array

Status
Not open for further replies.

Sneapy

Technical User
Nov 1, 2012
14
0
0
GB
Been struggling on creating an array, that will randomly select 1 of 6 words, and every time you click the button to activate this, it will randomly choose another word, but never the same one just after that one has shown, for example:

is cat comes up cat wont come up again until a different word comes up such as dog then cat can reappear, when the button is clicked again.

this is what i have so far

Code:
Dim Word(5)
Dim n

Sub window_onLoad()
 Randomize
 Word(0) = "Damfselfy"
 Word(1) = "Frog"
 Word(2) = "Hedgehog"
 Word(3) = "Newt"
 Word(4) = "Slowworm"
 Word(5) = "Thrush"
end sub

Sub btnQ_OnClick()
     n = Word(0 to 5)
    txt1.innerText = "What is a " & Word(n) & "?"
end sub
 
i have got it working, but now i would like to allow a sound play when they hit the button that chooses an animal, that corrisponds to the animal shown...
thanks
 
Judging by the names of your subroutines, I would guess the script is within an HTML file. If so, you can use HTML 5 audio controlls ( Or MS Technets SWcript Guy provides a solution by way of sndrec32.exe (
-Geates

 
i'm intrigued. never thought about playing a sound. adds a whole new dimension to UX!

Code:
play "C:\Windows\Media\Notify.wav"

sub play(strFile)
    set objShell = CreateObject("Wscript.Shell")
    strCommand = "wmplayer.exe " & chr(34) & strFile & chr(34)
    objShell.Run strCommand, 0, True
end sub

-Geates

 
Geates:
Code:
set sapi = CreateObject("sapi.spvoice") 
sapi.Speak "yes, the possibilities are endless"
 
If you get hold of DynaWrap.dll (from here, for example), you can make API calls from VBscript, which means that you could use the PlaySound API ...

e.g.

Code:
[blue]Option Explicit
Dim UserWrap 
dim strFile
Dim SND_FILENAME
Set UserWrap = CreateObject("DynamicWrapper")
strFile="C:\windows\Media\tada.wav"
SND_FILENAME= &h20000 
UserWrap.Register "WINMM.DLL", "PlaySoundA", "I=sll", "f=s", "r=l"
UserWrap.PlaySoundA cstr(strFile), clng(0), clng(SND_FILENAME)[/blue]
 
Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
    <title></title>
</head>

<body>

<center>

<input type="button" id="btnQ" value="?????" />
<p id="txt1"></p>
<p id="result1"></p>



<input type="button" id="btnfly" value="DamselFly" style="position:fixed; left:260px; top:360px;"  />
<input type="button" id="btnfrog" value="Frog" style="position:fixed; left:460px; top:360px;"  />
<input type="button" id="btnhedge" value="HedgeHog" style="position:fixed; left:610px; top:360px;"  />
<input type="button" id="btnnewt" value="Newt" style="position:fixed; left:810px; top:360px;"  />
<input type="button" id="btnworm" value="SlowWorm" style="position:fixed; left:960px; top:360px;"  />
<input type="button" id="btnthrush" value="Thrush" style="position:fixed; left:1150px; top:360px;"  />
<input type="button" id="btnshell" value="Tortoiseshell" style="position:fixed; left:1300px; top:360px;"  />

<input type="text" id="txtfly" value="DamselFly" />
<input type="text" id="txtfrog" value="Frog" />
<input type="text" id="txthedge" value="HedgeHog" />
<input type="text" id="txtnewt" value="Newt" />
<input type="text" id="txtworm" value="Slowworm" />
<input type="text" id="txtthrush" value="Thrush" />
<input type="text" id="txtshell" value="Tortoiseshell" />






</center>
</body>
</html>


<script language=vbscript>
Option Explicit    
Dim Nums(7)
Dim n

Sub window_onLoad()
    Randomize
    Nums(1) = "Damselfly"
    Nums(2) = "Frog"
    Nums(3) = "HedgeHog"
    Nums(4) = "Newt"
    Nums(5) = "SlowWorm"
    Nums(6) = "Thrush"
    Nums(7) = "Tortoiseshell"
  
  End Sub
  
  Sub btnQ_onClick()        
    n = 1 + Int(7 * Rnd())
    txt1.innerText = "What is a " & Nums(n) & "?"
  End Sub

    Sub btnfly_onClick()
    If Cint (txtfly.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

   Sub btnFrog_onClick()
    If (txtfrog.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n 
    End If
  End Sub

   Sub btnHedge_onClick()
    If CInt(txtAns.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

   Sub btnNewt_onClick()
    If CInt(txtAns.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

   Sub btnworm_onClick()
    If CInt(txtAns.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

   Sub btnThrush_onClick()
    If CInt(txtAns.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

   Sub btnshell_onClick()
    If CInt(txtAns.value) = n Then
      result1.innerText = "Correct!"
    Else
      result1.innerText = "Sorry, please try again." & n
    End If
  End Sub

</script>

scruggling with this piece of code i have written, i am trying to get it so when a word pops up, the user can click the corresponding button, and in "result1" it will say if they pushed the correct button or not, but i cannot get it to work.

i understand i am using a very crude method of doing it, but if you can make a slightly easier way by getting it just to respond to button isntead of responding to the text box, that be good, but im am very short on time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top