ES2015
Babel
'Cuase why not :)
/** * @param {Number} approximation - number of decimal places you're loojing for. * Shouldn't exceed 6 as Zu Chongzhi ratio are only valid until first 6 digits. * Is 2 by default. */ const pi = (approximation = 2) => /** Let's use [this guy's](https://en.wikipedia.org/wiki/Zu_Chongzhi) ratio to calc Pi */ (355 / 113).toFixed(approximation > 2 && approximation <= 6 ? approximation : 2)
const pi = () => {}pi()- /**
- * @param {Number} approximation - number of decimal places you're loojing for.
- * Shouldn't exceed 6 as Zu Chongzhi ratio are only valid until first 6 digits.
- * Is 2 by default.
- */
- const pi = (approximation = 2) =>
- /** Let's use [this guy's](https://en.wikipedia.org/wiki/Zu_Chongzhi) ratio to calc Pi */
- (355 / 113).toFixed(approximation > 2 && approximation <= 6 ? approximation : 2)
const assert = require('chai').assert describe("pi()", function () { it('Should equal 3.14 when called with no args', function () { const res = pi() console.log(`Call with no args:\n${res}`) assert.equal(res, 3.14, 'Call with no args') }) it('Should equal 3.1416 if 4 was passed', function () { const res = pi(4) console.log(`Call with approximation = 4:\n${res}`) assert.equal(res, 3.1416, 'Call with approximation = 4') }) it('Should equal 3.141593 if 6 was passed', function () { const res = pi(6) console.log(`Call with approximation = 6:\n${res}`) assert.equal(res, 3.141593, 'Call with approximation = 6') }) it('Should equal 3.14 whenever argument does not meet requirements to be 2 < arg <= 6', function () { const res = pi('abc') console.log(`Call with string passed as argument:\n${res}`) assert.equal(res, 3.14, 'Call with string passed as argument') }) });
- const assert = require('chai').assert
- describe("pi()", function () {
- it('Should equal 3.14 when called with no args', function () {
- const res = pi()
- console.log(`Call with no args:\n${res}`)
- assert.equal(res, 3.14, 'Call with no args')
- })
- it('Should equal 3.1416 if 4 was passed', function () {
- const res = pi(4)
- console.log(`Call with approximation = 4:\n${res}`)
- assert.equal(res, 3.1416, 'Call with approximation = 4')
- })
- it('Should equal 3.141593 if 6 was passed', function () {
- const res = pi(6)
- console.log(`Call with approximation = 6:\n${res}`)
- assert.equal(res, 3.141593, 'Call with approximation = 6')
- })
- it('Should equal 3.14 whenever argument does not meet requirements to be 2 < arg <= 6', function () {
- const res = pi('abc')
- console.log(`Call with string passed as argument:\n${res}`)
- assert.equal(res, 3.14, 'Call with string passed as argument')
- })
- });