|
Subject:
|
Numbers/Math Method???
|
|
Posted By:
|
TSEROOGY
|
Post Date:
|
9/29/2004 7:37:45 PM
|
I want to take two numbers and find the difference of each column of digits - in other words, I want to find the difference of the digits in the far right position (1-9); find the difference of the digits in the 10 position; find the difference of the digits in the 100 position; etc. For example:
first number: 1 2 4 second number: 4 2 1 difference: -3 0 3
Although I may be wrong, I believe a method exists for this in some programming languages - i.e. VB, C++, etc. Does Javascript have such a method or maybe a variable type that allows this?
PS - I can accomplish this via other means, I'm just looking for a shorter way than looping through the entire number.
Thanks, sabertec2
|
|
Reply By:
|
Snib
|
Reply Date:
|
9/29/2004 7:52:11 PM
|
I don't think one exists. You'll have to write one that loops through the number, but I made you a simple, short one that does it for you. It should work for numbers that are the same length.
function calculateDifference(number_1,number_2) { var difference = ""; for(var i = 0; i > (number_1.length-1); i++) { difference += (number_1.charAt(i) - number_2.charAt(i)).toString(); } return difference; }
Let me know if it works, I created it off the top of my head.
Also, if you can remember the name of this concept, that would be good. I've never had to use anything like this before.
HTH
-Snib <>< http://www.snibworks.com There are only two stupid questions: the one you don't ask, and the one you ask more than once ;-)
|
|
Reply By:
|
TSEROOGY
|
Reply Date:
|
9/30/2004 8:36:45 AM
|
Thanks Snib, Having limited writing experience, your loop is certainly more precise/concise than the one I was about to create. It worked well when modified for my application.
It's been several years since I last looked at some of the programming manuals. I believe it was a VB6 tutorial that demonstrated a method or possibly a data type that performed or allowed this operation. Then again, I have MPM (Male Pattern Memory - It's about one inch long!), so, I'll dig out the old books and check.
Best regards, sabertec2
|
|
Reply By:
|
TSEROOGY
|
Reply Date:
|
10/8/2004 10:51:52 AM
|
Okay, Snib, Got back and took a look at some of my old Tutorials. The operation I'm trying to perform is not a method or number type. What I remembered is a procedure that was briefly demonstrated using Java's (not JavaScript) Bitwise and Logical Operators.
In short, using a single operand you can shift from one bit to the next. It appears that this is simply an easier and possibly quicker way of cycling through all the elements or bits in a string or number.
This comes from pages 61 through 64 of "The Java Tutorial Second Edition," by Mary Campione & Kathy Walrath.
Guess Java's next on my list of things to learn?!?
Best regards, sabertec2
|