The absolute value function (abs() in many languages) returns the absolute value of a number. This means that it returns a number's positive value. You can think of it as the distance away from zero.
Create your own function that recreates this functionality, using the name and parameter n from the starter code.
Examples:
abs(-12) ➞ 12 abs(-544) ➞ 544 abs(250) ➞ 250 abs(0) ➞ 0 abs(-14) ➞ 14
Note:
Do not use the absolute value function provided by Rust (it will defeat the purpose of the challenge)
Hints:
You can make use of
< operator with an if statement to check if n is negative.
You can add an
else statement to handle the case when n is positive.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
