从数组到parseFloat?计算(第2部分) [英] from array to parseFloat ? calculations (part2)

查看:63
本文介绍了从数组到parseFloat?计算(第2部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信息



第1部分



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



示例输入

  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.28MΩ;237kΩ;2.19MΩ;
R; - - ;
$ b $bΔC; - - ;
长度; - m;

期望输出

  U = 
AB 0.74 V //这个不能超过5伏特
AG 3.23 V //这个不能超过5伏特b $ b BG 0.48 V //这个不能超过5伏特
U~
AB 0.03 V //这个不能超过5伏特
AG 0.02 V //这个不能超过5伏特
BG 0.02 V //这不能超过5伏特
上限
AB 232.5 nF / 6.64 ff // ab总是被分红35
AG 11.87 nF / 0.27 ff // ag总是被分红44
BG 30.73 nF / 0.69 ff // bg总是被分红44
差异
AG en BG = 18.86 nF //这个数字不能大于5
ISO
AB 2.28 MOhm [X] //如果低于3Mohm thare是一个错误
AG 237 KOhm [X] //如果低于3Mohm thare是一个错误
BG 2.19 MOhm [X] //如果低于3Mohm thare则是错误

差异
AG en BG = 2.953 MOhm //此数字不能大于100 Mohm

当前代码

 < table> 
< tr>
< th>输入< / th>< th>& nbsp;& nbsp;< / th>< th>输出< / th>
< / tr>
< tr>
< td>< textarea style =background-color:#CACACAid =sourcecols =30rows =22>< / textarea>< / td>
< td>& nbsp;& nbsp;< / td>
< b>< td>< textarea style =background-color:#CACACAid =targetcols =30rows =22readonly>< / 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 )//当一行开始时拆分顶部^;
.slice(1)
.join()//;

console.log(行);

var newlines = lines.replace(/MΩ/ g,MOhm)//找到Ω并替换为Ohm
var newnewlines = newlines.replace(/kΩ/ g,KOhm [ x])//找到kΩ并替换为错误的
.split(\ n)//;

console.log(换行符)//;
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]好;
}其他{
ab =[x]错误;
}

// log这是whare我打破脑子的地方
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()//使AB大写
.split(;)//进入之后;
.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>

测试文字< br>
< textarea style =background-color:#CACACAcols =30rows =22>

设备: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.28MΩ;237kΩ;2.19MΩ;
R; - - ;
$ b $bΔC; - - ;
长度; - m;
< / textarea>

问题



<它已经有一段时间了,我还没有完成拼图我还在学习如何做到这一点



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



我不是在找为我做一切的人我正在寻找我自己最好的学习方法我对数组不了解,因为java脚本对我来说是新的




  • 如果你添加代码,你会很善解释//你做了什么,这样我就可以学到这个


解决方案

我建议采用与你提到Q& A的第一部分不同的方法。这是因为你有计算发生(差异)和检查(产生 [X] ),这意味着你对源的确切含义和顺序有更强的期望。



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



但有一个棘手的问题:解决方案必须意识到测量单位之间的差异,因此即使一个数字以kΩ表示而另一个数字以MΩ表示,它也能正确执行减法。对于这个方面,我建议创建一个对象,指示哪些单位应该转换为统一单位,以及哪个因子。该解决方案仅定义了一个这样的转换,但您可以在需要的地方扩展它。因此,输出将使用统一的测量单位,因此输出中的237kΩ将显示为0.237MΩ。



下面的代码假设感兴趣的数量将是后面跟着一个空格,然后是一个测量单位(任何字符序列,除了分号),然后是一个分号。输入中应该至少有12个这样的序列。实际上,你可以从输入中删除很多噪音并仍然得到正确的输出。



  //应转换的单位列表除以= {kΩ:1000 //将kΩ转换为MΩ};函数refresh(){const u =(document.querySelector(#source)。value //用它们的单位提取数字:.match(/ [\\\。] + \s + [^;] + / g)|| [])//以统一的度量单位将数字转换为数字类型:。 map(m => +(parseFloat(m)/(divisor [m.split(/ \s + /)[1]] || 1))。toString()); document.querySelector(#target)。innerHTML = u.length< 12? (没有足够的值):`U = AB $ {u [0]} V $ {u [0]> 5?[X]:}} AG $ {u [1]} V $ {你[1]> 5?[X]:} BG $ {u [2]} V $ {u [2]> 5?[X]:} U~AB $ {你[3]} V $ {u [3]> 5?[X]:} AG $ {u [4]} V $ {u [4]> 5?[X]: }} BG $ {u [5]} V $ {u [5]> 5?[X]:}} CapA-B $ {u [6]} nF / $ {(你[6] /35).toFixed(2)} ffA-G $ {u [7]} nF / $ {(u [7] / 44).toFixed(2)} ffB-G $ {u [8]} nF / $ {(u [8] / 44).toFixed(2)} ffdifferenceA-G en BG = $ {u [8] -u [7]} nF $ {u [8] -u [7]> 5? [X]:} ISOA-B $ {u [9]} MOhm $ {u [9]< 3?[X]:}} AG $ {u [10]} MOhm $ {u [10]< 3?[X]:} BG $ {u [11]} MOhm $ {u [11]< 3?[X]:}}差异A-G en BG = $ {(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 ;; ab; a-GND; b -GND; U =; 0.74V; 3.23V; 0.48V; U~; 0.03V; 0.02V; 0.02V; C; 232.5nF; 11.87nF; 30.73nF; ISO;2.28MΩ;237kΩ;2.19MΩ; R ;  - ;;ΔC;  -   - ;长度;  -  m;`; re fresh();});  

 < table> < TR> < th>在此处粘贴输出:< button> Sample< / button>< / th>< th> Beautified:< / th> < / TR> < TR> < td>< textarea id =sourcecols =30rows =21>< / textarea> < / TD> < td>< textarea id =targetcols =30rows =21readonly>< / textarea>< / td> < / tr>< / table>  


Info

1st part

Current online version Link to my website

Example input

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;

Desired output

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

Current code

                    <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>

Question

it has been a while and I have not finished with the puzzle I'm still learning how I can do it

I have a lot of information in the console but I still have not succeeded in calculating with it I try to get the number from the array and then put the conditions into it

I'm not looking for someone who does everything for me I'm looking for how I can best learn this myself I do not understand anything about arrays since java script is new to me

  • if you add code you would be so kind to explain // what you have done so that I can learn this

解决方案

I would suggest a different approach than used in the first part you referred to Q&A. This is because you have calculations happening (differences) and checks (producing [X]), which means you have a stronger expectancy of what exactly the source would contain and in which order.

The code may defer the actual output format to a template literal. The code then only needs to extract the numbers (and units) from the input. The template literal, containing the formulas (subtraction) and conditional inclusion of [X], will do the rest.

There is one tricky thing though: the solution must be aware of differences between unit of measures, so it performs the subtraction correctly even when one number is expressed in kΩ and another in MΩ. For this aspect I propose to create an object that indicates which units should be converted to a uniform unit, and with which factor. The solution defines just one such conversion, but you could extend it where needed. By consequence, the output will use the unified unit of measure, so 237 kΩ will appear as 0.237 MΩ in the output.

The code below assumes that the numbers of interest will always be followed by white space followed by a unit of measure (any sequence of characters, except semi-colon), followed by a semi-colon. There should be at least 12 of such sequences in the input. So actually, you can remove a lot of "noise" from the input and still get a correct output.

// 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>

这篇关于从数组到parseFloat?计算(第2部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆