Tuesday 1 February 2011

Week 1 Exercise 3.1 Function to return the absolute value of a number

Ex 3.1

My attempt to create a function that returns an absolute value of a number. I created an if else statement. If the argument is greater than 0, you return the value. Otherwise, multiply the number with -1 and return that value.

function absolute (a) {
  if (a > 0) {
    return a;
  }
  else {
    a = -1*a;
    return a;
  }
}

No comments:

Post a Comment