How to resolve the algorithm Dot product step by step in the Jsish programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Dot product step by step in the Jsish programming language
Table of Contents
Problem Statement
Create a function/use an in-built function, to compute the dot product, also known as the scalar product of two vectors. If possible, make the vectors of arbitrary length.
As an example, compute the dot product of the vectors:
If implementing the dot product of two vectors directly:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dot product step by step in the Jsish programming language
Source code in the jsish programming language
/* 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!=
*/
You may also check:How to resolve the algorithm Tau number step by step in the REXX programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the C++ programming language
You may also check:How to resolve the algorithm LU decomposition step by step in the REXX programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Sidef programming language