0

I'm trying to work with some maths, and I can't get a negative number to act as a positive number. I have these numbers in different variables;

330 3106 -2776

I need 330, 3106 and 2776 so I can do this statement (obviously they will be vars though!);

if((2776 + 330) == 3106){ 

}

Is there a shorthand or quick method I can use to do this inside of my if statement? Id rather not have to make more variables to do this...

Thanks

3 Answers 3

5

Use Math.abs() function:

if((Math.abs(-2776) + 330) == 3106){ 
  //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's what I was after. Thanks Tomasz and every else who replied. I'll accept as soon as the website lets me (got to wait a few mins first!)
2

You can use Math.abs() on a number to ensure it's positive.

Math.abs(-123);  // 123

Math.abs(123);   // 123

Comments

1

You could use Math.abs() method.

Math.abs(-2276) === 2276

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.