www.webdeveloper.com
Results 1 to 3 of 3

Thread: Comparing two number Greater than Less than

  1. #1
    Join Date
    Oct 2003
    Posts
    30

    Comparing two number Greater than Less than

    I'm using var numbersInString = input.match(/\d+/g); to put into an array the first 2 digital numbers.

    Such as: Is 6 greater than 5?

    Note: (A person types this into an input box and the answer is displayed in a similar input box. So I must keep this in a similar format. The "if statement" below checks the input. If the information I need is not present it simply doesn't trigger and is answered with a "I Dunno" type of response later in the script.

    numbersInString[0] - will give the first number
    numbersInString[1] - will give the second number

    I then want to compare the numbers as either > < or =

    I can't seem to get any combinations that works and I don't understand why.

    I can add/substract the 2 numbers and get a correct answer. I can't seem to get a valid > < type of answer though

    This is part of a bigger script of "If like statements" so I am not looking for a complete rewrite. I'm hoping I am just missing something in the below that is causing me problems.

    Thanks





    Code:
    if ((input.search("what is greater")!= -1) && (input.search(/\d{1,10}/)!=-1) && (input.search(/\d{1,10}/)!=-1)) 
    {var numbersInString = input.match(/\d+/g); 
    var num1 = parseInt( numbersInString[0], 10 ); 
    var num2 = parseInt( numbersInString[1], 10 ); 
    if (num1 < num2) document.result.result.value = ""+num1+" is less than "+num2+""; 
    if (num1 > num2) document.result.result.value = ""+num1+" is greater than "+num2+""; 
    if (num1 = num2) document.result.result.value = "Both numbers are equal"; 
    return true;}

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356
    See if this function works for your needs...
    Code:
    <script type="text/javascript">
    function compareNums(num1,num2) {
      var result = "I don't know. ";
      var n1 = parseInt(num1);  if (isNaN(n1)) { result += num1+" is not a number. "; return result }
      var n2 = parseInt(num2);  if (isNaN(n2)) { result += num2+" is not a number. "; return result }
      if (n1 < n2) { result = n1+' is less than '+n2; }
      else {
        if (n1 > n2) { result = n1+' is greater than '+n2; }
        else { result = n1+' is equal to '+n2; }
      }
      return result;
    }
    // 3 valid entries
    alert(compareNums(1,2));
    alert(compareNums(2,2));
    alert(compareNums(3,2));
    
    // 3 goofy entries
    alert(compareNums('a',2));
    alert(compareNums(1,'b'));
    alert(compareNums('a','b'));
    </script>

  3. #3
    Join Date
    Oct 2003
    Posts
    30
    Sorry, I'm not following the code. As a mentioned the present "if statement" screens out all the unnecessary information. I don't need any bailout responses in regards to this "if statement". If 2 seperate digital numbers aren't present it just doesn't trigger nor do I want it to at this point.

    So with that said I don't know what to pull out or add to my present code. Something like this perhaps?

    if ((input.search("what is greater")!= -1) && (input.search(/\d{1,10}/)!=-1) && (input.search(/\d{1,10}/)!=-1))
    {var numbersInString = input.match(/\d+/g);
    var result = "I don't know. ";
    var n1 = parseInt(num1);
    var n2 = parseInt(num2);
    if (n1 < n2) { result = n1+' is less than '+n2; }
    else {
    if (n1 > n2) { result = n1+' is greater than '+n2; }
    else { result = n1+' is equal to '+n2; }
    }
    return result;

    Here is the demo I'm testing it on.



    Code:
    <HTML> 
    <HEAD>
    <TITLE>ChatterBot Page</TITLE>
    
    
    <script language="JavaScript">
    
    var message = new Array(); 
    
    var randomnum; 
    var flagrandom;
    
    
    function Parse() { 
    var input = new String(document.chat.input.value); 
    
    document.chat.input.value=""; 
    input=input.toLowerCase(); 
    
    word=input.split(" ");
    num_of_words=word.length;
    
    input=input.replace(/[!|?|,|.]/g,""); 
    word=input.split(" ");
    
    
    
    if (input.search(/\bu\b/)!=-1) input=input.replace(/\bu\b/,"you");
    if (input.search(/\br\b/)!=-1) input=input.replace(/\br\b/,"are");
    if (input.search(/\bk\b/)!=-1) input=input.replace(/\bk\b/,"ok");
    if (input.search(/\by\b/)!=-1) input=input.replace(/\by\b/,"why");
    
    if (input.search("ok")!= -1)     
    {document.result.result.value = "Okay, that's interesting."; 
    return true;}
    
    if (input.search("yes")!= -1)     
    {document.result.result.value = "Okay, yes it is."; 
    return true;}
    
    if (input.search("no")!= -1)     
    {document.result.result.value = "Okay, no it is."; 
    return true;}
    
    
    if (!flagrandom) {
    randomnum = [Math.floor(Math.random()*3)]
    flagrandom=true;}
    message[0] = "Sorry, you stumped me on that one.";  
    message[1] = "Sorry, a search of my data base comes up empty."; 
    message[2] = "Not sure";
    document.result.result.value = message[randomnum]; 
    randomnum++
    if (randomnum>2){randomnum=0}
    return true;}
    
    
    </script> 
    
    
    
    
    </head> 
    
    
    
    <BODY BACKGROUND="FFFFFF" TEXT="#0000cc" LINK="#FFCOOO" ALINK="#FFCC99"
    VLINK="#FFC000" marginwidth="0" leftmargin="0" topmargin="0" rightmargin="0">
    <Center>
    <font size="+3">
    ChatterBot
    </font>
    <br>
    <img src="botpix.jpg" border="0" WIDTH="200" HEIGHT="200">
    <br>
    <form name="result">
    <textarea rows=5 cols=40 input type="text" name="result">
    </textarea><br>
    </form>
    </center>
    <form name="chat" onSubmit="Parse();return false">
    &nbsp;<b>Type here:</b> 
    <input type="text" name="input" size="100">
    </form>
    
    
    
    </body> 
    </html>
    Last edited by chriscc17; 06-13-2012 at 01:55 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles