這是我的原始代碼,一切正常,但我需要添加第三個函式,用戶在第一個輸入(“apBox”)中輸入“a”或“p”。然后它找到矩形的周長或面積,而不是兩者兼而有之:
我哪里出錯了?
我覺得它應該是這樣的結構,但我不確定你們會怎么做?它可以作業,但不是我需要的方式,用戶可以輸入面積或周長,然后輸入長度/寬度并獲得單獨的答案。
If () {
}
else if () {
}
else () {
}
document.getElementById("message").innerHTML=(output);
function controller() {
var type = document.getElementById("apBox").value;
var length = document.getElementById("lengthBox").value;
var width = document.getElementById("widthBox").value;
var mainSolution = document.getElementById("message");
var answer = length * width;
var answer2 = length width length width;
// this is where I am getting stuck, it should be an if statement, right?
if (type == "a") {
}
if (length.length > 0 && width.length > 0) {
if (isNaN(answer) === false && isNaN(answer2) === false) {
mainSolution.innerHTML = "Area = " answer "<br>Perimeter = " answer2;
} else {
mainSolution.innerHTML = "Must Enter A Number";
}
} else {
mainSolution.innerHTML = "";
}
}
function clearText() {
document.getElementById("message").innerHTML = ("<br>");
document.getElementById("apBox").value = "";
document.getElementById("lengthBox").value = "";
document.getElementById("widthBox").value = "";
}
#main {
background-color: #455A6F;
border-radius: 3px;
padding: 10px;
}
#header {
background-color: #292A2B;
border-radius: 3px;
padding: 10px;
}
body {
text-align: center;
font-family: Montserrat;
color: #fff;
}
a {
color: rgb(27, 157, 218);
}
.btn1,
.btn2 {
background-color: rgba(52, 139, 221, 0.699);
border: #303436;
font-family: Montserrat;
font-size: 12px;
color: #fff;
letter-spacing: 2px;
padding: 5px;
font-size: 16px;
font-weight: bold;
border-radius: 5px;
line-height: 3;
text-decoration: none;
box-shadow: 0 0 4px black;
text-shadow: 0 1px 0 black;
margin-top: 1em;
margin-bottom: 1em;
margin-left: 1em;
margin-right: 1em;
}
h1,
h2,
h3,
h4,
h5,
h6 {
text-shadow: 1px;
text-transform: capitalize;
text-align: center;
}
/* This is a custom snippet I made so the footer always stays at the bottom of the page */
footer {
position: absolute;
right: 0;
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 2em;
background-color: darkslategray;
text-align: center;
border-color: #fff;
border-radius: 18px;
padding: 12px;
}
.description {
font-style: italic;
}
form {
border-radius: 25px;
padding: 5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<meta name="description" content="Area of a Rectangle">
<title>Area And Perimeter Of A Rectangle</title>
</head>
<body>
<div id="main">
<div id="header">
<h1>Program 6</h1>
<h5>Area or Perimeter</h5>
<h6>of a Rectangle</h6>
</div>
<form id="myForm">
<form id="myForm">
<label for="apBox1">Enter A for Area or P for Perimeter:</label><br><br>
<input type="text" id="apBox">
<br><br>
<label for="userInput2">Length:</label><br><br>
<input type="text" id="lengthBox"><br><br>
<label for="userInput3">Width:</label><br><br>
<input type="text" id="widthBox">
</form>
<p id="demo">Output: </p>
<p id="message"> <br> </p>
<button class="btn1" type="button" onClick="controller()">Submit</button>
<button class="btn2" type="button" onClick="clearText()">Clear</button>
</div>
</body>
</html>
uj5u.com熱心網友回復:
將計算移至適當的if分支。
您可以為要在結果欄位中顯示的標簽添加另一個變數,因為您不再計算answer和answer2。
if (type == "a") {
answer = length * width;
label = 'Area';
} else if (type == "p") {
answer = 2 * length 2 * width;
label = 'Perimiter';
} else {
mainSolution.innerText = "Enter either a or p";
return;
}
完整代碼:
顯示代碼片段
function controller() {
var type = document.getElementById("apBox").value;
var length = document.getElementById("lengthBox").value;
var width = document.getElementById("widthBox").value;
var mainSolution = document.getElementById("message");
var answer;
var label;
if (type == "a") {
answer = length * width;
label = 'Area';
} else if (type == "p") {
answer = 2 * length 2 * width;
label = 'Perimiter';
} else {
mainSolution.innerText = "Enter either a or p";
return;
}
if (length.length > 0 && width.length > 0) {
if (!isNaN(answer)) {
mainSolution.innerHTML = `${label} =  ${answer}`;
} else {
mainSolution.innerHTML = "Must Enter A Number";
}
} else {
mainSolution.innerHTML = "";
}
}
function clearText() {
document.getElementById("message").innerHTML = ("<br>");
document.getElementById("apBox").value = "";
document.getElementById("lengthBox").value = "";
document.getElementById("widthBox").value = "";
}
#main {
background-color: #455A6F;
border-radius: 3px;
padding: 10px;
}
#header {
background-color: #292A2B;
border-radius: 3px;
padding: 10px;
}
body {
text-align: center;
font-family: Montserrat;
color: #fff;
}
a {
color: rgb(27, 157, 218);
}
.btn1,
.btn2 {
background-color: rgba(52, 139, 221, 0.699);
border: #303436;
font-family: Montserrat;
font-size: 12px;
color: #fff;
letter-spacing: 2px;
padding: 5px;
font-size: 16px;
font-weight: bold;
border-radius: 5px;
line-height: 3;
text-decoration: none;
box-shadow: 0 0 4px black;
text-shadow: 0 1px 0 black;
margin-top: 1em;
margin-bottom: 1em;
margin-left: 1em;
margin-right: 1em;
}
h1,
h2,
h3,
h4,
h5,
h6 {
text-shadow: 1px;
text-transform: capitalize;
text-align: center;
}
/* This is a custom snippet I made so the footer always stays at the bottom of the page */
footer {
position: absolute;
right: 0;
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 2em;
background-color: darkslategray;
text-align: center;
border-color: #fff;
border-radius: 18px;
padding: 12px;
}
.description {
font-style: italic;
}
form {
border-radius: 25px;
padding: 5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<meta name="description" content="Area of a Rectangle">
<title>Area And Perimeter Of A Rectangle</title>
</head>
<body>
<div id="main">
<div id="header">
<h1>Program 6</h1>
<h5>Area or Perimeter</h5>
<h6>of a Rectangle</h6>
</div>
<form id="myForm">
<form id="myForm">
<label for="apBox1">Enter A for Area or P for Perimeter:</label><br><br>
<input type="text" id="apBox">
<br><br>
<label for="userInput2">Length:</label><br><br>
<input type="text" id="lengthBox"><br><br>
<label for="userInput3">Width:</label><br><br>
<input type="text" id="widthBox">
</form>
<p id="demo">Output: </p>
<p id="message"> <br> </p>
<button class="btn1" type="button" onClick="controller()">Submit</button>
<button class="btn2" type="button" onClick="clearText()">Clear</button>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314891.html
標籤:javascript html 数学
下一篇:代碼排條件陳述句問題集
