-2

I just created vbscript code to find the greatest numbers from 10 input numbers. It works only for numbers between 1 to 10. Why?

<html>
<head>
    <title>Enter 10 numbers to find the largest number</title>
    <meta http-equiv="x-ua-compatible" content="IE=10">
</head>

<body>
<script language = "vbscript" type = "text/vbscript">
    Dim n1 
    Dim n2
    Dim n3
    Dim n4
    Dim n5
    Dim n6
    Dim n7
    Dim n8
    Dim n9
    Dim n10
    Dim largest
    n1=inputBox("please Enter the  number1 ","number1")
    n2=inputBox("please Enter the  number2 ","number2")
    n3=inputBox("please Enter the  number3 ","number3")
    n4=inputBox("please Enter the  number4 ","number4")
    n5=inputBox("please Enter the  number5 ","number5")
    n6=inputBox("please Enter the  number6 ","number6")
    n7=inputBox("please Enter the  number7 ","number7")
    n8=inputBox("please Enter the  number8 ","number8")
    n9=inputBox("please Enter the  number9 ","number9")
    n10=inputBox("please Enter the  number10 ","number10")

    If (n1>n2 AND num1>vnum3 AND n1>n4 AND n1>n5 AND n1>n6 AND n1>n7 AND n1>n8 AND n1>n9 AND n1>n10 ) then
         document.write("this is the largest number " & n1 & ", Enjoy VBscript")
    ElseIf (n2>n1 AND n2>n3 AND n2>n4 AND n2>n5 AND n2>n6 AND n2>n7 AND n2>n8 AND n2>n9 AND n2>n10 ) Then
         document.write("this is the largest number " & n2 & ", Enjoy VBscript")
    ElseIf (n3>n1 AND n3>n2 AND n3>n4 AND n3>n5 AND n3>n6 AND n3>n7 AND n3>n8 AND n3>n9 AND n3>n10 ) Then
         document.write("this is the largest number " & n3 & ", Enjoy VBscript")
    ElseIf (n4>n1 AND n4>n3 AND n4>n2 AND n4>n5 AND n4>n6 AND n4>n7 AND n4>n8 AND n4>n9 AND n4>n10 ) Then
         document.write("this is the largest number " & n4 & ", Enjoy VBscript")
    ElseIf (n5>n1 AND n5>n3 AND n5>n4 AND n5>n2 AND n5>n6 AND n5>n7 AND n5>n8 AND n5>n9 AND n5>n10 ) Then
         document.write("this is the largest number " & n5 & ", Enjoy VBscript")
    ElseIf (n6>n1 AND n6>n3 AND n6>n4 AND n6>n5 AND n6>n2 AND n6>n7 AND n6>n8 AND n6>n9 AND n6>n10 ) Then
         document.write("this is the largest number " & n6 & ", Enjoy VBscript")
    ElseIf (n7>n1 AND n7>n3 AND n7>n4 AND n7>n5 AND n7>n6 AND n7>n2 AND n7>n8 AND n7>n9 AND n7>n10 ) Then
         document.write("this is the largest number " & n7 & ", Enjoy VBscript")
    ElseIf (n8>n1 AND n8>n3 AND n8>n4 AND n8>n5 AND n8>n6 AND n8>n7 AND n8>n2 AND n8>n9 AND n8>n10 ) Then
         document.write("this is the largest number " & n8 & ", Enjoy VBscript")
    ElseIf (n9>n1 AND n9>n3 AND n9>n4 AND n9>n5 AND n9>n6 AND n9>n7 AND n9>n8 AND n9>n2 AND n9>n10 ) Then
         document.write("this is the largest number " & n9 & ", Enjoy VBscript")
    ElseIf (n10>n1 AND n10>n3 AND n10>n4 AND n10>n5 AND n10>n6 AND n10>n7 AND n10>n8 AND n10>n9 AND n10>n2 ) Then
         document.write("this is the largest number " & n10 & ", Enjoy VBscript")
    Else
         document.write("this is the largest number is  " & number10 & ", Enjoy VBscript")
    End If
</script>
</body>
</html>
Robert
  • 7,394
  • 40
  • 45
  • 64
  • The easiest way of doing it is to only keep two numbers in memory: the previous greater value and the new read one. If the read number is larger that the previous greater value then overwrite the previous value, else discard it and ask for another number. – MC ND Feb 27 '19 at 18:56
  • 2
    Because you are comparing strings, not numbers. With string compare "5" > "10" – Geert Bellekens Feb 28 '19 at 07:39
  • Not sure about your exact requirement of asking user 10 times(inputbox), but you can take a look at [this](https://stackoverflow.com/a/268659/2784816) solution – Pankaj Jaju Feb 28 '19 at 18:56

1 Answers1

-2

This puts things into loops and uses an object that sorts.

'Getting the 10 numbers
Dim Thing(10)
For x = 0 to 9
    Thing(x) = InputBox("Something")
Next

Set rs = CreateObject("ADODB.Recordset")
With rs
    'Setting up a one field database containing a single precision field called Numbers
    .Fields.Append "Number", 4 
    .Open

    'Adding the data
    For x = 0 to 9
        .AddNew
        .Fields("Number").value = Thing(x)
        .UpDate
    Next

    'Sorting descending on number field
    .Sort = "Number DESC"

    'Now write it
    MsgBox .Fields("Number").Value
    'And the second one
    .MoveNext
    MsgBox .Fields("Number").Value

    'And all the rest
    For x = 0 to 7
        .MoveNext
        MyStr = MyStr & .Fields("Number").Value & vbcrlf
    Next
    MsgBox MyStr
End With
Noodles
  • 194
  • 1
  • 4