Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mikkel Storch
1,955 PointsVBScript - Random Generator
Hello, I know this maybe isn't the right forum. But i really need your help with a assignment:
Can anyone help me with making a VBScript with the following requirements?
The user needs to choose a number. Program must generate a random number between 1-100. If the user guess the right number it should say "Congrats". If the user guess the wrong number it should say if the random number is higher or lower. The random number must be generated by the "random" command. The program needs to check for errors so the program doesn't crash when a wrong number is put in by the user.
2 Answers
Roberto Alicata
Courses Plus Student 39,959 PointsYou can see this page to prompt for user Input.
then this is the code to generate a random number between 1-100
<%
Dim max,min
max=100
min=1
Randomize
response.write(Int((max-min+1)*Rnd+min))
%>
Mikkel Storch
1,955 PointsThanks! Cant really figure out the site you linked. Could you try to write it for me in code, how you would do it? Thanks ;)
Mikkel Storch
1,955 PointsCould you explain how the last line works?
Mikkel Storch
1,955 PointsThis is my code so far:
Dim max,min max = 100 min = 1
Do userInput = InputBox("Choose number")
Randomize number = WScript.Echo(Int((max-min+1)*Rnd+min))
If IsNumeric(userInput) = number Then MsgBox("Du gættede rigtigt!")
ElseIf number < userInput Then MsgBox("The number is bigger than yours")
ElseIf number > brugerInput Then MsgBox("The number is smaller than yours")
End If
Loop Until number = userInput
Roberto Alicata
Courses Plus Student 39,959 PointsOk i try to code, but I can't test it now.
first you need to write this function:
<%
Function GetUserInput( myPrompt )
' This function uses Internet Explorer to
' create a dialog and prompt for user input.
'
' Version: 2.11
' Last modified: 2013-11-07
'
' Argument: [string] prompt text, e.g. "Please enter your name:"
' Returns: [string] the user input typed in the dialog screen
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Error handling code written by Denis St-Pierre
Dim objIE
' Create an IE object
Set objIE = CreateObject( "InternetExplorer.Application" )
' Specify some of the IE window's settings
objIE.Navigate "about:blank"
objIE.Document.title = "Input required " & String( 100, "." )
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 320
objIE.Height = 180
' Center the dialog window on the screen
With objIE.Document.parentWindow.screen
objIE.Left = (.availWidth - objIE.Width ) \ 2
objIE.Top = (.availHeight - objIE.Height) \ 2
End With
' Wait till IE is ready
Do While objIE.Busy
WScript.Sleep 200
Loop
' Insert the HTML code to prompt for user input
objIE.Document.body.innerHTML = "<div align=""center""><p>" & myPrompt _
& "</p>" & vbCrLf _
& "<p><input type=""text"" size=""20"" " _
& "id=""UserInput""></p>" & vbCrLf _
& "<p><input type=""hidden"" id=""OK"" " _
& "name=""OK"" value=""0"">" _
& "<input type=""submit"" value="" OK "" " _
& "OnClick=""VBScript:OK.value=1""></p></div>"
' Hide the scrollbars
objIE.Document.body.style.overflow = "auto"
' Make the window visible
objIE.Visible = True
' Set focus on input field
objIE.Document.all.UserInput.focus
' Wait till the OK button has been clicked
On Error Resume Next
Do While objIE.Document.all.OK.value = 0
WScript.Sleep 200
' Error handling code by Denis St-Pierre
If Err Then ' user clicked red X (or alt-F4) to close IE window
IELogin = Array( "", "" )
objIE.Quit
Set objIE = Nothing
Exit Function
End if
Loop
On Error Goto 0
' Read the user input from the dialog window
GetUserInput = objIE.Document.all.UserInput.value
' Close and release the object
objIE.Quit
Set objIE = Nothing
End Function
%>
make a random number
<%
Dim max,min
max=100
min=1
Randomize
rndNum = (Int((max-min+1)*Rnd+min))
%>
then you need a while loop to prompt your user for the right number
<%
dim a
a= false
do
strUserInput = GetUserInput( "Guess the number:" )
if rndNum = CInt(strUserInput) then
document.write “You are right!!!”
a = true
else
if rndNum < CInt(strUserInput)
document.write “too high!”
else
document.write “too low!”
end if
end if
loop While a = false
%>
Roberto Alicata
Courses Plus Student 39,959 PointsRoberto Alicata
Courses Plus Student 39,959 PointsWhy VBScript? [from http://www.w3schools.com/vbscript/ ] "VBScript should be used as a server-side scripting language. It should not be used for scripting in a browser."