Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2012, 11:19 AM   PM User | #1
movieland
New Coder

 
Join Date: Feb 2011
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
movieland is an unknown quantity at this point
Javascript 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.)

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. Any Help would be appreciated.



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;}
movieland is offline   Reply With Quote
Old 06-13-2012, 04:58 PM   PM User | #2
movieland
New Coder

 
Join Date: Feb 2011
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
movieland is an unknown quantity at this point
Sorry, I don't know enough about javascript to adapt this to my needs. Basically I have a working bot that answers questions using "if statements" like this. If the input isn't there the statements never triggers and gets answered later by a simple I dunno type of response. So I'm trying to working within that frame work.
movieland is offline   Reply With Quote
Old 06-13-2012, 06:04 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Code:
if (num1 = num2) document.result.result.value = "Both numbers are equal";
this will never work. it needs to have two == to compare the values
Code:
if (num1 == num2) document.result.result.value = "Both numbers are equal";
you can save yourself some grief by using the proper Number method to convert a string to a number:
Code:
var num1 = Number(numbersInString[0]); 
var num2 = Number(numbersInString[1]);
you will probably get into trouble at some point using the same name for your form and a form element - better to use something else (even adding an "s" will keep you out of trouble). Here's what all that would look like (assuming the text input has an id of "txt"):
Code:
function compareNumbers(){
                var input=document.getElementById('txt').value;
                var numbersInString = input.match(/\d+/g); 
var num1 = Number(numbersInString[0]); 
var num2 = Number(numbersInString[1]); 
if (num1 < num2) {document.results.result.value = num1+" is less than "+num2; }
if (num1 > num2) {document.results.result.value = num1+" is greater than "+num2;} 
if (num1 == num2) {document.results.result.value = "Both numbers are equal"; }
            }
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
movieland (06-14-2012)
Old 06-13-2012, 07:53 PM   PM User | #4
movieland
New Coder

 
Join Date: Feb 2011
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
movieland is an unknown quantity at this point
Ok thanks. That looks more familar to me.

Last edited by movieland; 06-14-2012 at 06:08 PM..
movieland is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:21 PM.


Advertisement
Log in to turn off these ads.