-5

Problem: some numbers are positive some are not. Here is my function to convert negative numbers to positive ones and leave positive numbers untouched. Is there a better way of doing it in JavaScript?

function intAbs(integer) {

    if(isNaN(integer)) {
        throw 'NaN';
    }

    if(parseInt(integer, 10) !== integer) {
        throw 'Not an integer';
    }

    n = integer * integer;

    var x = 1;
    var e = 1;

    while(!(e < 0.1 && e > -0.1)) {
        x = (n / x + x) / 2;
        e = n - x * x;
    }

    return parseInt(x);

}

Update: I don't think this question is a duplicate. I know how to get the absolute value of an integer without Math.abs(). You can see in this question. I have already accepted the answer which states "Yes, there is a better way" and it's not ambiguous.

Update #2: This question is marked as a duplicate of Get the absolute value of a number in Javascript, but this does not address problem if(is there a better way of calculating abs than exactly this algorithm). I give up.

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
Ruslanas Balčiūnas
  • 7,310
  • 3
  • 24
  • 41
  • 5
    What's wrong with `Math.abs(whatever)`? – user2357112 May 16 '15 at 00:09
  • I will not accept it If it will be closed. – Ruslanas Balčiūnas May 16 '15 at 00:11
  • The poster wants to require integer arguments only, apparently. – kennebec May 16 '15 at 00:12
  • I feel a huge peer presure now. – Ruslanas Balčiūnas May 16 '15 at 00:12
  • 5
    And are you trying to square the number, then take the square root with the Babylonian method? That's an amazingly roundabout way to do it. – user2357112 May 16 '15 at 00:13
  • Instead of ranting on about other users, why don't you improve your post so people can understand what you're asking? See [ask] for more information. – Qantas 94 Heavy May 16 '15 at 00:17
  • It is far from roundaboutest you could do. @Qantas, thanks for advice. I will try to improve it. – Ruslanas Balčiūnas May 16 '15 at 00:20
  • Similar SO question: [How do I get the absolute value of an integer without using Math.abs?](http://stackoverflow.com/questions/30268801/how-do-i-get-the-absolute-value-of-an-integer-without-using-math-abs). There are a couple of interesting answers. – Yogi May 16 '15 at 00:30
  • 1
    Now that you changed your question, there is an even better way: `-number`. Could you please make up your mind what this question is about? *edit*: ah. – Felix Kling May 16 '15 at 00:56
  • @FelixKling thanks for your remark. I've updated problem definition. – Ruslanas Balčiūnas May 16 '15 at 00:58
  • *"but this does not address problem if(is there a better way of calculating abs than exactly this algorithm)."* I see you unaccepted the answer here, so I assume you are not satisfied with the answer that `Math.abs` is the better way? Is this actually an algorithm question, not JS specific? In this case, http://stackoverflow.com/questions/30268801/how-do-i-get-the-absolute-value-of-an-integer-without-using-math-abs seems to be the better duplicate. Or are you just changing your question for the sake of it, hoping at some point it won't be a duplicate anymore? – Felix Kling May 16 '15 at 01:24
  • And now you accepted the answer again (or I was just blind). Your behavior is really confusing and the intend of the question not clear at all. If you accept `Math.abs` as answer, then it is a duplicate of the currently linked question. – Felix Kling May 16 '15 at 01:26
  • @FelixKling I was just exploring possibilities to delete this question. This answer is acceptable. Everything is ok. No jokes here. Lesson learned. Points transferred. Nobody harmed. – Ruslanas Balčiūnas May 16 '15 at 01:29

1 Answers1

0

Yes, there is a better way: Math.abs().

niry
  • 3,238
  • 22
  • 34