0

I have a node's server embedded in the Raspberry Pi as well as an external board attached with an A/D and a D/A converter. When all parameters are completed and a button is pressed in the website, It is generated several voltage to an external circuit as well as sensed voltages are stored in a vector like ANI0. The main idea is to measure the current at each voltage using a Mathematical expression like Mathexp. Whereby I need to evaluate these operation for each value comprised in ANIO. The expression is in string cause is introduced by the user in the web page. I tried using the command eval to evaluate it but it did not work. Is it even possible to do this procedure using some commands??

var ANI0 = [0.0,0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0];
var Mathexp; //operation inserted by the user in the webpage in a input text

Mathexp = '(ANIO[i]-0.01)/20'; //Example

for (i=0; I<ANIO.length; i++){

singleObj = {};

singleObj['voltage'] = eval(xScale);        
singleObj['current'] = eval(Mathexp);       
listOfObjects.push(singleObj);

}

//generate a jsonFILE in order to graph results with metrics graphics 

myJSON = JSON.stringify(listOfObjects);
fs.writeFile("public/data/file.json", myJSON, function(err){
    if(err){console.log(err);} else {console.log("archivo guardado..");}
});

socket.emit('graphEvent',{message: ':)'});

enter code here
Margarita Gonzalez
  • 1,127
  • 7
  • 20
  • 39
  • There's quite a few things wrong with this code, but the answer to your question is "yes". If you would've liked a different answer, perhaps you should invest some more time into writing the question in a way that we can actually help you. – Stephan Bijzitter May 03 '17 at 07:30

1 Answers1

0

There's no need for eval, you're just doing a math operation over an array, is not clear from your code what you want to do next, but in general it will be one of two:

  1. Reduce the array values to one value, for example applying the formula you added there to every item and then sum them all together.
  2. Apply the formula to each element in the array and return the array updated.

In case of 1, you do something like this (note I'm using ES6):

var result = ANIO.reduce( (acc, curr) => curr + ((curr-0.01)/20));

In case of 2, you can do something like this:

var result = ANIO.map( val => (curr-0.01)/20 );

Hope it helps.

Community
  • 1
  • 1
Franco Risso
  • 1,572
  • 2
  • 13
  • 18