Q: How can I change this PHP array function to JS code?

Source: Internet
Author: User
Q: How can I change this PHP array function to JS code? Functiont_arr ($ weight, $ arr) {$ ActName = array (1.464, 3.9, 3.7, 3.7, 2.28, 1.2, 1.466, 2.0, 1.90, 2.80, 14, 7.1, 3.1, 4.25, 1.83, 1.10, 1.766, 3 Q: How can I modify this PHP array function to JS code?
Function t_arr ($ weight, $ arr)
{

$ ActName = array (1.464, 3.9, 3.7, 3.7, 2.28, 1.2, 1.466, 2.0, 1.90, 2.80, 0.8, 2.166, 7.1, 3.1, 4.25, 1.83, 1.10, 1.766, 3, 1.38, 6, 9.3, 14, 9.3, 12, 5.0, 4.38, 5.8, 12.466, 13.166, 4.2, 6.4, 7.7, 4.083, 1.70, 3.95, 10, 3.12, 5.88, 4.6, 5.88, 4.7, 7.5 );

$ Count = 0;

Foreach ($ arr as $ key => $ value ){

$ Count + = ($ ActName [$ key] * $ value );

}

}

The variable $ arr imports an array, such as, 32, 3, and 14, corresponding to $ ActName, which determines the input array and obtains the sum value based on the serial number, how to change to JS.


------ Solution --------------------
JScript code
function t_arr(weight,arr){    ActName=[1.464,3.9,3.7,3.7,2.28,1.2,1.466,2.0,1.90,2.80,0.8,2.166,14,7.1,3.1,4.25,1.83,1.10,1.766,3,1.38,6,9.3,14,9.3,12,5.0,4.38,5.8,12.466,13.166,4.2,5,5,6.4,7.7,4.083,1.70,3.95,10,3.12,5.88,17,4,4.6,5.88,9,9,4.7,20,10,15,25,7.5];    count=0;    if(arr instanceof Array){        for(key in arr) {             count+=(ActName[key]*arr[key]);        }    }   alert(count);    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.