如何将两个函数加在一起并存储在 JavaScript 中的另一个函数(第三个函数)中?
How do I add two functions together and store in another function(a third function) in JavaScript?
请问如何将我声明的这两个函数添加并存储到另一个函数(第三个函数)中,以便我能够使用第三个函数调用两个函数的结果相加?
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
switch (grade1) {
case 'a':
document.getElementById('demo').innerHTML = (5 * unit / tUnits);
break;
case 'b':
document.getElementById('demo').innerHTML = (4 * unit / tUnits);
break;
default:
document.getElementById('demo').innerHTML = 'Please input a valid grade';
}
return grade1;
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3
var tUnits = 18
switch (grade2) {
case 'a':
document.getElementById('demo').innerHTML = (10 * unit2 / tUnits);
break;
case 'b':
default:
document.getElementById('demo').innerHTML = 'Please input a valid grade';
}
}
就是上面的代码!
更改您的函数,使其 return 结果而不是将其放入 innerHTML
。然后你可以有另一个函数调用它们并显示两个结果。
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
switch (grade1) {
case 'a':
return (5 * unit / tUnits);
break;
case 'b':
return (4 * unit / tUnits);
break;
default:
return 'Please input a valid grade';
}
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3
var tUnits = 18
switch (grade2) {
case 'a':
return (10 * unit2 / tUnits);
break;
case 'b':
default:
return 'Please input a valid grade';
}
}
function combinedFunc() {
let class1 = myFunc();
let class2 = myFunc2();
document.getElementById("demo").innerText = `Class 1: ${class1}, Class 2: ${class2}`;
}
<select id="grade">
<option value="">Grade for Class 1</option>
<option value="a">A</option>
<option value="b">B</option>
</select>
<select id="grade2">
<option value="">Grade for Class 2</option>
<option value="a">A</option>
<option value="b">B</option>
</select>
<button type="button" onclick="combinedFunc()">Show Grades</button>
<p>
Grades:
<div id="demo"></div>
试试这个:
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
// declare variable output
var output;
switch (grade1) {
case 'a':
output = (5 * unit / tUnits);
break;
case 'b':
output = (4 * unit / tUnits);
break;
default:
output = 'Please input a valid grade';
}
// return output
return output;
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3;
var tUnits = 18;
// declare output
var output;
switch (grade2) {
case 'a':
output = (10 * unit2 / tUnits);
break;
case 'b':
default:
output = 'Please input a valid grade';
}
// return output
return output;
}
function myFunc3() {
// get the value returned by myFunc
var funcA = myFunc();
// get value returned by myFunc2
var funcB = myFunc2();
// if the value returned by both function is a number , add up the result
if (typeof funcA === 'number' && typeof funcB === 'number'){
document.getElementById('demo').innerHTML = funcA + funcB;
} else {
// output error message to the user
document.getElementById('demo').innerHTML = 'please input a valid grade';
}
}
请问如何将我声明的这两个函数添加并存储到另一个函数(第三个函数)中,以便我能够使用第三个函数调用两个函数的结果相加?
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
switch (grade1) {
case 'a':
document.getElementById('demo').innerHTML = (5 * unit / tUnits);
break;
case 'b':
document.getElementById('demo').innerHTML = (4 * unit / tUnits);
break;
default:
document.getElementById('demo').innerHTML = 'Please input a valid grade';
}
return grade1;
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3
var tUnits = 18
switch (grade2) {
case 'a':
document.getElementById('demo').innerHTML = (10 * unit2 / tUnits);
break;
case 'b':
default:
document.getElementById('demo').innerHTML = 'Please input a valid grade';
}
}
就是上面的代码!
更改您的函数,使其 return 结果而不是将其放入 innerHTML
。然后你可以有另一个函数调用它们并显示两个结果。
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
switch (grade1) {
case 'a':
return (5 * unit / tUnits);
break;
case 'b':
return (4 * unit / tUnits);
break;
default:
return 'Please input a valid grade';
}
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3
var tUnits = 18
switch (grade2) {
case 'a':
return (10 * unit2 / tUnits);
break;
case 'b':
default:
return 'Please input a valid grade';
}
}
function combinedFunc() {
let class1 = myFunc();
let class2 = myFunc2();
document.getElementById("demo").innerText = `Class 1: ${class1}, Class 2: ${class2}`;
}
<select id="grade">
<option value="">Grade for Class 1</option>
<option value="a">A</option>
<option value="b">B</option>
</select>
<select id="grade2">
<option value="">Grade for Class 2</option>
<option value="a">A</option>
<option value="b">B</option>
</select>
<button type="button" onclick="combinedFunc()">Show Grades</button>
<p>
Grades:
<div id="demo"></div>
试试这个:
function myFunc() {
var grade1 = document.getElementById("grade").value;
var unit = 3;
var tUnits = 18;
// declare variable output
var output;
switch (grade1) {
case 'a':
output = (5 * unit / tUnits);
break;
case 'b':
output = (4 * unit / tUnits);
break;
default:
output = 'Please input a valid grade';
}
// return output
return output;
}
function myFunc2() {
var grade2 = document.getElementById("grade2").value;
var unit2 = 3;
var tUnits = 18;
// declare output
var output;
switch (grade2) {
case 'a':
output = (10 * unit2 / tUnits);
break;
case 'b':
default:
output = 'Please input a valid grade';
}
// return output
return output;
}
function myFunc3() {
// get the value returned by myFunc
var funcA = myFunc();
// get value returned by myFunc2
var funcB = myFunc2();
// if the value returned by both function is a number , add up the result
if (typeof funcA === 'number' && typeof funcB === 'number'){
document.getElementById('demo').innerHTML = funcA + funcB;
} else {
// output error message to the user
document.getElementById('demo').innerHTML = 'please input a valid grade';
}
}