Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  28] [ 3]  / answers: 1 / hits: 10250  / 9 Years ago, fri, may 15, 2015, 12:00:00

How do I get the absolute value of a number without using math.abs?



This is what I have so far:



function absVal(integer) {
var abs = integer * integer;
return abs^2;
}

More From » function

 Answers
10

You can use the conditional operator and the unary negation operator:



function absVal(integer) {
return integer < 0 ? -integer : integer;
}

[#37103] Friday, May 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;