phpjs
Version:
84 lines (70 loc) • 2.17 kB
Markdown
layout: page
title: "JavaScript abs function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/abs:300
- /functions/view/abs
- /functions/view/300
- /functions/abs:300
- /functions/300
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's abs
{% codeblock math/abs.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/abs.js raw on github %}
function abs (mixed_number) {
// From: http://phpjs.org/functions
// + original by: Waldo Malqui Silva
// + improved by: Karol Kowalski
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// * example 1: abs(4.2);
// * returns 1: 4.2
// * example 2: abs(-4.2);
// * returns 2: 4.2
// * example 3: abs(-5);
// * returns 3: 5
// * example 4: abs('_argos');
// * returns 4: 0
return Math.abs(mixed_number) || 0;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/abs.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/abs.js)
### Example 1
This code
{% codeblock lang:js example %}
abs(4.2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
4.2
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
abs(-4.2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
4.2
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
abs(-5);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
5
{% endcodeblock %}
### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}