从阵列到parsefloat?计算(部分2)



info

第一部分

当前的在线版本链接到我的网站

示例输入

Copper Box v3;
S/N:25304;FW:1.07.12;
;a-b;a-GND;b-GND;
U=;0.74 V;3.23 V;0.48 V;
U~;0.03 V;0.02 V;0.02 V;
C;232.5 nF;11.87 nF;30.73 nF;
ISO;2.28 MΩ;237 kΩ;2.19 MΩ;
R;- -;
ΔC;- -;
Length;  - m;

所需的输出

U=
A-B 0.74 V       //this cant be more then 5volts
A-G 3.23 V       //this cant be more then 5volts
B-G 0.48 V       //this cant be more then 5volts
U~
A-B 0.03 V       //this cant be more then 5volts
A-G 0.02 V       //this cant be more then 5volts
B-G 0.02 V       //this cant be more then 5volts
Cap
A-B 232.5 nF / 6.64 ff   //ab is always dividend by 35
A-G 11.87 nF / 0.27 ff   //ag is always dividend by 44
B-G 30.73 nF / 0.69 ff   //bg is always dividend by 44
difference
A-G en B-G =18.86 nF   //this number cant be bigger then 5
ISO
A-B 2.28 MOhm [X]   //if lower then 3Mohm thare is an error
A-G 237 KOhm [X]    //if lower then 3Mohm thare is an error
B-G 2.19 MOhm [X]   //if lower then 3Mohm thare is an error
difference
A-G en B-G =2.953 MOhm     // this number cant be bigger then 100 Mohm

当前代码

                    <table>
                        <tr>
                            <th>input</th><th>&nbsp;&nbsp;</th><th>output</th>
                        </tr>
                        <tr>
                            <td><textarea style="background-color:#CACACA" id="source" cols="30" rows="22"></textarea></td> 
                            <td>&nbsp;&nbsp;</td>
                            <b><td><textarea style="background-color:#CACACA"  id="target" cols="30" rows="22" readonly></textarea></td></b>
                        </tr>
                    </table>

                    <script type="text/javascript">
                    document.getElementById("target").style.fontWeight = "600";
                    document.getElementById("source").style.fontWeight = "600";
document.querySelector("#source").addEventListener("input", function () {
    const lines = this.value
       .split(/^;/m) //split top ^ when a line starts at ; 
       .slice(1) 
       .join("")//;
       console.log(lines);
    var newlines = lines.replace(/MΩ/g, "MOhm" ) //find Ω and replace to Ohm 
   var newnewlines = newlines.replace(/kΩ/g, "KOhm [x] ") //find kΩ and replace to error's 
        .split("n")//;
        console.log(newlines)//;
        console.log(newnewlines);
// var newnewlines = [];
newnewlines.push('item 1');
newnewlines.push('item 2');
newnewlines.push('item 3');
newnewlines.push('item 4');
newnewlines.push('item 5');
newnewlines.push('item 6');
  var ab = parseFloat(newnewlines[1]) < 5;
    if (ab < 5)  {
  ab = "[v] good";
} else {
  ab = "[x] error";
}
// log This is the place whare i break my brain
console.log(ab);
console.log(newnewlines[1]);
console.log(newnewlines[2]);
console.log(newnewlines[3]);
console.log(newnewlines[4]);
console.log(newnewlines[5]);
console.log(newnewlines[6]);

     const cols = newnewlines
       .shift()
       .toUpperCase() // makes A-B uppercase
       .split(";") //enter after;
       .filter(Boolean)
       .map(code => code.slice(0,3))//;
       console.log(cols);

    document.querySelector("#target").value =
        newnewlines.filter(line => /;d/.test(line))
            .map(line => line.split(";").filter(Boolean))
                .map(
                    ([s, ...v]) => s + "n" + 
                    v.map((value, i) => cols[i] + " " + value )
                    .join("n")
                )
                 .join("n");
});

                    </script><br><br>
  Test Text <br>
 <textarea style="background-color:#CACACA" cols="30" rows="22">
Device:ARGUS153;
S/N:55565;FW:1.80.00;
Copper Box v3;
S/N:25304;FW:1.07.12;
;a-b;a-GND;b-GND;
U=;0.74 V;3.23 V;0.48 V;
U~;0.03 V;0.02 V;0.02 V;
C;232.5 nF;11.87 nF;30.73 nF;
ISO;2.28 MΩ;237 kΩ;2.19 MΩ;
R;- -;
ΔC;- -;
Length;  - m;
</textarea>

问题

已经有一段时间了,我还没有完成我仍在学习如何做的难题

我在控制台中有很多信息,但我仍然没有成功地计算它我尝试从数组中获取数字,然后将条件放入其中

我不是在寻找为我做所有事情的人,我正在寻找我自己能最好地学习这个我自己什么都不了解阵列,因为Java脚本对我来说是新的

  • 如果添加代码,您将非常友好地解释您所做的事情,以便我可以学习此

我建议的方法与您提到的Q&amp; a的第一部分中使用的方法不同。这是因为您的计算正在发生(差异)和检查(生产[X]),这意味着您对源的确切所包含以及顺序有更高的期望。

代码可以将实际输出格式推迟到模板文字。然后,代码只需要从输入中提取数字(和单位)。包含公式(减法)和条件包含[X]的模板将执行其余的。

虽然有一件棘手的事情:解决方案必须意识到措施单位之间的差异,因此即使以kΩ表示一个数字,也可以正确地执行减法,而另一个数字则在mΩ中。在这方面,我建议创建一个对象,该对象应转换为统一单元,以及哪个因素。该解决方案仅定义了一种这样的转换,但是您可以在需要的情况下将其扩展。因此,输出将使用统一的度量单位,因此237kΩ在输出中以0.237MΩ出现。

下面的代码假定兴趣的数量始终将随后是空白,然后是一个度量单位(字符序列,除了半柱外),然后是半彩色。输入中应至少有12个此类序列。因此,实际上,您可以从输入中删除大量"噪声"并仍然获得正确的输出。

// List of units that should be converted
const divisor = {
    kΩ: 1000 // Convert kΩ to MΩ
};
function refresh() {
    const u = (document.querySelector("#source").value
        // extract the numbers with their units:
        .match(/[d.]+s+[^;]+/g) || []) 
        // convert numbers to number type in uniform unit of measure:
        .map(m => +(parseFloat(m) / (divisor[m.split(/s+/)[1]] || 1)).toString()); 
    
    document.querySelector("#target").innerHTML = 
        u.length < 12 ? "(not enough values)" :
`U=
A-B ${u[0]} V ${u[0]>5?"[X]":""}
A-G ${u[1]} V ${u[1]>5?"[X]":""}
B-G ${u[2]} V ${u[2]>5?"[X]":""}
U~
A-B ${u[3]} V ${u[3]>5?"[X]":""}
A-G ${u[4]} V ${u[4]>5?"[X]":""}
B-G ${u[5]} V ${u[5]>5?"[X]":""}
Cap
A-B ${u[6]} nF / ${(u[6]/35).toFixed(2)} ff
A-G ${u[7]} nF / ${(u[7]/44).toFixed(2)} ff
B-G ${u[8]} nF / ${(u[8]/44).toFixed(2)} ff
difference
A-G en B-G = ${u[8]-u[7]} nF ${u[8]-u[7]>5?"[X]":""}
ISO
A-B ${u[9]} MOhm ${u[9]<3?"[X]":""}
A-G ${u[10]} MOhm ${u[10]<3?"[X]":""}
B-G ${u[11]} MOhm ${u[11]<3?"[X]":""}
difference
A-G en B-G = ${(u[11]-u[10]).toFixed(3)} MOhm`;
}
document.querySelector("#source").addEventListener("input", refresh);
document.querySelector("button").addEventListener("click", () => {
    document.querySelector("#source").innerHTML = `Copper Box v3;
S/N:25304;FW:1.07.12;
;a-b;a-GND;b-GND;
U=;0.74 V;3.23 V;0.48 V;
U~;0.03 V;0.02 V;0.02 V;
C;232.5 nF;11.87 nF;30.73 nF;
ISO;2.28 MΩ;237 kΩ;2.19 MΩ;
R;- -;
ΔC;- -;
Length;  - m;`;
    refresh();
});
<table>
    <tr>
        <th>Paste output here: <button>Sample</button></th><th>Beautified:</th>
    </tr>
    <tr>
        <td><textarea id="source" cols="30" rows="21"></textarea>
        </td>
        <td><textarea id="target" cols="30" rows="21" readonly></textarea></td>
    </tr>
</table>

最新更新