# Thread: Comparing two number Greater than Less than

1. Registered User
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. 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

// 3 goofy entries
</script>```

3. Registered User
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>
<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>

<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.

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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.08721 seconds
• Memory Usage 2,865KB
• Queries Executed 15 (?)
Template Usage (33):
• (3)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (3)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (3)postbit
• (3)postbit_onlinestatus
• (3)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete