RosettaCodeData/Task/Dot-product/Jsish/dot-product.jsish
2023-07-01 13:44:08 -04:00

18 lines
530 B
Text

/* Dot product, in Jsish */
function dot_product(ary1, ary2) {
if (ary1.length != ary2.length) throw "can't find dot product: arrays have different lengths";
var dotprod = 0;
for (var i = 0; i < ary1.length; i++) dotprod += ary1[i] * ary2[i];
return dotprod;
}
;dot_product([1,3,-5],[4,-2,-1]);
;//dot_product([1,3,-5],[4,-2,-1,0]);
/*
=!EXPECTSTART!=
dot_product([1,3,-5],[4,-2,-1]) ==> 3
dot_product([1,3,-5],[4,-2,-1,0]) ==>
PASS!: err = can't find dot product: arrays have different lengths
=!EXPECTEND!=
*/