JavaScript array_product
Returns the product of the array entries
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 | function array_product (input) { // Returns the product of the array entries // // version: 1109.2015 // discuss at: http://phpjs.org/functions/array_product // + original by: Waldo Malqui Silva // * example 1: array_product([ 2, 4, 6, 8 ]); // * returns 1: 384 var idx = 0, product = 1, il = 0; if (Object.prototype.toString.call(input) !== '[object Array]') { return null; } il = input.length; while (idx < il) { product *= (!isNaN(input[idx]) ? input[idx] : 0); idx++; } return product; } |
Examples
Running
1 | array_product([ 2, 4, 6, 8 ]); |
Should return
1 | 384 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have array_product goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>