I am writing a code for performing calculation of a formula on the basis of some constants and some values to be fetched from the CSV file. I would appreciate if anyone can help me solving the issue of fetching the values from the csv file and substituing them in the formula with simple javascript.
function loadFile(o){
window.onload = () => {
//FILE READER + HTML ELEMENTS
var reader = new FileReader(),
picker = document.getElementById("picker"),
table = document.getElementById("table");
//READ CSV ON FILE PICKER
picker.onchange = () => reader.readAsText(picker.files[0]);
//READ THE CSV FILE & GENERATE HTML
reader.onload = () => {
let csv = reader.result;
// CLEAR HTML TABLE
table.innerHTML = "";
//SPLIT INTO ROWS
let rows = csv.split("rn");
//LOOP THROUGH ROWS + SPLIT COLUMNS
for(let row of rows){
let cols = row.match(/(?:"([^"]*(?:""[^"]*)*)")|([^",]+)/g);
if (cols != null){
let tr= table.insertRow();
for (let col of cols) {
let td = tr.insertCell();
td.innerHTML = col.replace(/(^"|"$)/g, "");
}
}
else {
console.log("Error parsing CSV");
}
}
function calculateCSV()
{
var getCSVData = document.getElementById("picker").files[0];
var values = getCSVData.FileReader;
rows = getCSVData.split("n");
var html = '<table border="1">';
rows.forEach((data, index) =>
{
var value = data.split(",");
var x_1 = values[0];
var x_2 = values[1];
var x_3 = values[2];
let a,b,c,d,p1, p2, p3, p4;
const e=2.71828;
if ( x_1 && x_2 ){
a=5.8;
b=0.02;
c=(-7.9);
d=0;
p1= 1/(1+e^(-(a+b*x_1+c*x_2+d*x_3)));
document.getElementById("result1").innerHTML =p1;
}
else if( x_2 && x_3 ){
a=4.5;
b=1.0;
c=(-6.4);
d=(-6.8);
p2= 1/(1+e^(-(a+b*x_1+c*x_2+d*x_3)));
document.getElementById("result2").innerHTML =p2;
}
else if( x_1 && x_3 ){
a=0.7;
b=2.2;
c=(-0.02);
d=(-6.8);
p3= 1/(1+e^(-(a+b*x_1+c*x_2+d*x_3)));
document.getElementById("result3").innerHTML =p3;
}
else if( x_1 && x_2 && x_3 ){
a=4.6;
b=0.96;
c=0.02;
d=(-6.8);
p4= 1/(1+e^(-(a+b*x_1+c*x_2+d*x_3)));
document.getElementById("result4").innerHTML =p4;
}
html += '</table>';
html += '<span>' + p1 + '</span>';
html += '<span>' + p2 + '</span>';
html += '<span>' + p3 + '</span>';
html += '<span>' + p4 + '</span>';
document.getElementById("data").innerHTML = html;
document.getElementById("data").style.color="blue";
});
}
}
}
};
I am trying this and I want the values in th erespective columns of the csv file to be fetched and substituted by x_1,x_2,x_3 and x_4