我在Javascript中写了以下内容,但是我没有让变量正确使用calculate函数。我不认为代码要求回忆正确的变量/函数,然后不起作用。任何人都可以确定需要更改的位置,或使用更简单的计算方法吗?
提前致谢
<!DOCTYPE html>
<html>
<head>
<title>Parallel RLC Circuit Parameters Calculator</title>
</head>
<body>
<form onreset="myfunction()" align="center">
<font face="Garamond">
<h1 style="color:#ff336f">Parallel RLC Circuit Parameters Calculator </h1>
<p style="color:#92E367"> Please Enter the Voltage Supply:
<input id="Supply Voltage" value=0> V</input><br>
</p>
<p style="color:#92E367">
Please Enter the Frequency:
<input id="Frequency" value=0> Hz</input><br>
</p>
<p style="color:#92E367">
Please Enter the Resistor Value:
<input id="Resistance" value=0> Ohms</input><br>
</p>
<p style="color:#92E367">
Please Enter the Inductor Value:
<input id="Inductance" value=0> H</input><br>
</p>
<p style="color:#92E367">
Please Enter the Capacitor Value:
<input id="Capacitance" value=0> F</input><br>
</p>
<button id="Submit" type="button" onclick="calculate()">Calculate</button>
<button id="Reset" type="button" onclick="reset()">Reset</button>
<p style="color: #808080" id="message">The values will appear here after clicking Calculate</p>
</form>
<script>
function calculate() {
document.getElementById("message").innerHTML = "<br>";
var Vs = document.getElementById('Supply Voltage').value;
var f = document.getElementById('Frequency').value;
var R = document.getElementById('Resistance').value;
var L = document.getElementById('Inductance').value;
var C = document.getElementById('Capacitance').value;
const pi = 3.14159265;
document.getElementById("message").innerHTML = "" + "<br>";
if ((Vs||f||R||L||C)==0) {
document.getElementById("message").innerHTML = "Zero values are not permitted. Please reset the form.";
break;
}
var Xl = calcXl(f,L,pi);
document.getElementById("message").innerHTML ="Xl = " + Xl +" Ohms <br>";
var Xc = calcXc(f,C,pi);
document.getElementById("message").innerHTML ="Xc = " + Xc +" Ohms <br>";
var Z = calcZ(Xl,Xc,R);
document.getElementById("message").innerHTML ="Z = " + Z +" Ohms <br>";
var Ir = calcIr(Vs,R);
document.getElementById("message").innerHTML ="Ir = " + Ir +" Amps <br>";
var Il = calcIl(Vs,Xl);
document.getElementById("message").innerHTML ="Il = " + Il +" Amps <br>";
var Ic = calcIc(Vs,Xc);
document.getElementById("message").innerHTML ="Ic = " + Ic +" Amps <br>";
var Is = calcIs(Ir,Il,Ic);
document.getElementById("message").innerHTML ="Is = " + Is +" Amps <br>";
var G = calcG(R);
document.getElementById("message").innerHTML ="G = " + G +" Seconds <br>";
var Bl = calcBl(Xl);
document.getElementById("message").innerHTML ="Bl = " + Bl +" Seconds <br>";
var Bc = calcBc(Xc);
document.getElementById("message").innerHTML ="Bc = " + Bc +" Seconds <br>";
var Y = calcY(Z);
document.getElementById("message").innerHTML ="Y = " + Y +" Seconds <br>";
var phi = calc_phi(G,Y);
document.getElementById("message").innerHTML ="phi = " + phi +" Degrees <br>";
var f_res = calc_f_res(pi,L,C);
document.getElementById("message").innerHTML ="f_res = " + f_res +" Hz <br>";
var f_res = calc_f_res(pi,L,C);
document.getElementById("message").innerHTML ="f_res = " + f_res +" Hz <br>";
var Q = calcQ(R,C,L);
document.getElementById("message").innerHTML ="Q = " + Q +"<br>";
var Bw = calcBw(f_res,Q);
document.getElementById("message").innerHTML ="Bw = " + Bw +"<br>";
}
function calcXl(f,L,pi){
return 2*pi*f*L;
}
function calcXc(f,C,pi){
return 1/(2*pi*f*C);
}
function calcZ(Xl,Xc,R){
return 1/(Math.sqrt(Math.pow((1/R),2)+Math.pow((1/Xc)-(1/Xl),2));
}
function calcIr(Vs,R){
return Vs/R;
}
function calcIl(Vs,Xl){
return Vs/Xl;
}
function calcIc(Vs,Xc){
return Vs/Xc;
}
function calcIs(Ir,Il,Ic){
return Math.sqrt(Math.pow(Ir,2)+Math.pow((Il-Ic),2));
}
function calcG(R){
return 1/R;
}
function calcBl(Xl){
return 1/Xl;
}
function calcBc(Xc){
return 1/Xc;
}
function calcY(Z){
return 1/Z;
}
function calc_phi(G,Y) {
return Math.acos(G/Y);
}
function calc_f_res(pi,L,C) {
return 1/(2*pi*(Math.sqrt(L*C)));
}
function calcQ(R,C,L) {
return R*(Math.sqrt(C/L));
}
function calcBw(f_res,Q) {
return f_res/Q;
}
function myfunction() {
document.getElementById("message").innerHTML = "The value of the resistor will appear here after clicking calculate"
}
</script>
</body>
</html>
答案 0 :(得分:0)
使用return,而不是break语句。
if ((Vs||f||R||L||C)==0) {
document.getElementById("message").innerHTML = "Zero values are not permitted. Please reset the form.";
return;
}
在calcZ中你错过了一个paren
function calcZ(Xl,Xc,R){
return 1/(Math.sqrt(Math.pow((1/R),2)+Math.pow((1/Xc)-(1/Xl),2))); //extra paren added at the end.
}
让它为我工作。
以前我用来测试的编码器:https://codepen.io/anon/pen/YLYRNZ?editors=1010