Having trouble with Typescript as I encounter this error message
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
Specifically facing issues when trying to multiply values within an array. Attempting to create a function for calculating cosine similarity using the following code:
cosineSim(Q: string[], B: string[]){
//var dotproduct = 0;
let dotproduct: number= 0;
var mA = 0;
var mB = 0;
for(var i=0; i< Q.length;i++){
//The error goes here at Q[i] and B[i]
//it says: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts
dotproduct += Q[i] * B[i];
mA += (Q[i] * Q[i]);
mB += (B[i] * B[i]);
}
mA = Math.sqrt(mA);
mB = Math.sqrt(mB);
var similarity = (dotproduct) / ((mA) * (mB));
return similarity;
}
Trying to call this function in another piece of code like so:
var cos1 = this.cosineSim(eachdocTFIDF[0], eachdocTFIDF[1]);
console.log(cos1);
Here is an example of the data structure being passed:
//example value
eachdocTFIDF[0] = [0.5, 0.01123, 0, 0, 0.693215];
eachdocTFIDF[1] = [0.342131, 0.786785, 0, 0.2345, 0.00123];