Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2004, 07:37 PM
Authorized User
 
Join Date: Aug 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Numbers/Math Method???

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
 
Old September 29th, 2004, 07:52 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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 ;)
 
Old September 30th, 2004, 08:36 AM
Authorized User
 
Join Date: Aug 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old October 8th, 2004, 10:51 AM
Authorized User
 
Join Date: Aug 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Math question dparsons ASP.NET 1.0 and 1.1 Professional 5 June 14th, 2006 02:31 PM
Math Namespace transtar C# 3 February 12th, 2006 09:50 AM
Math functions-Please help! jroxit ASP.NET 2.0 Basics 5 December 28th, 2005 12:21 PM
Math problem... help please! Varg_88 Classic ASP Basics 2 September 30th, 2004 05:17 AM
Math on a Form kelcontech Classic ASP Basics 4 May 12th, 2004 05:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.