Monday, October 17, 2022

[FIXED] How do I get the absolute value of an integer without using Math.abs?

Issue

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;
}

Solution

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

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


Answered By - Guffa
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.