對於我的Comp Sci課程而言,教授要求我們將函數實現爲一個主函數,用於已經用do-while和for循環,switch語句,和基本的條件編程。但是,他並沒有指導我們如何完成這一任務。基本Javascript菜單程序中的函數問題
這就是原始賦值的代碼看起來像沒有函數的樣子。
<html>
<head>
</head>
<body>
<script>
var menu_choice;
do {
menu_choice = 1 * prompt("-----Menu------\n\n" +
"1. Addition\n" +
"2. Multiplication\n" +
"3. Expenentiation\n" +
"4. String Concatenation\n" +
"5. Logical AND (&&)\n" +
"6. Logical OR (||)\n" +
"7. Quit\n\n" +
"Enter your choice (1-7):");
switch (menu_choice) {
case 1:
var iterations = 1*prompt("Enter number of numbers to add");
var sum = 1*prompt("Enter a number:");
for(var i = 1;i < iterations; i++){
sum += 1*prompt("Enter a number:");}
alert("The sum is " + sum);
document.write("The sum is " + sum + "<br><br>");
break;
case 2:
var iterations = 1*prompt("Enter number of numbers to multiply");
var product = 1;
for(var i = 1;i <= iterations; i++){
product *= 1*prompt("Enter a number:");}
alert("The product is " + product);
document.write("The product is " + product + "<br><br>");
break;
case 3:
var iterations = 1*prompt("Enter number of numbers to exponentiate");
var result = 1*prompt("Enter a number");
for(var i = 1;i < iterations; i++){
result = Math.pow(result, 1*prompt("Enter a number"));}
alert("The exponentiation is " + result);
document.write("The exponentiation is " + result + "<br><br>");
break;
case 4:
var iterations = 1*prompt("Enter the number of strings to concatenate:");
var result = prompt("Enter a string");
for(var i = 1;i < iterations; i++){
result += prompt("Enter a string");}
alert("The concatenated string is " + result);
document.write("The concatenated string is " + result + "<br><br>");
break;
case 5:
var iterations = 1*prompt("Enter the number of booleans to AND:");
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var result = string;
if (result === "true")
var result = true;
else
var result = false;
for (var i = 1; i < iterations; i++){
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var newbool = string;
if (newbool === "true")
var newbool = true;
else
var newbool = false;
var result = (result && newbool)
}
alert("The ANDing of the booleans is: " + result);
document.write("The ANDing of the booleans is: " + result + "<br><br>");
break;
case 6:
var iterations = 1*prompt("Enter the number of booleans to OR:");
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var result = string;
if (result === "true")
var result = true;
else
var result = false;
for (var i = 1; i < iterations; i++){
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var newbool = string;
if (newbool === "true")
var newbool = true;
else
var newbool = false;
var result = (result || newbool)
}
alert("The ORing of the booleans is: " + result);
document.write("The ORing of the booleans is: " + result + "<br><br>");
break;
case 7:
document.write("Quitting the program...");
break;
default:
document.write("Error: invalid input (must be 1-7)<br><br>");
}
}
while (menu_choice !== 7)
</script>
</body>
</html>
現在我只是想處理案例1,但我想我正在爲'+ ='操作符寫一個函數。在測試中(使用JSrunner),我只能使用兩個數字來工作。
下面是我一直在測試的情況下,1
var number_of_numbers, final_result;
var iterations = function (op)
{
return 1*prompt("Enter the number of numbers to " + op + ":");
};
var input = function()
{
return 1*prompt("Enter a number:");
};
var sum_plus = function()
{
var result = input();
return result += 1*prompt("Enter a number:");
};
number_of_numbers = iterations("add");
for(var i = 1;i < number_of_numbers; i++){
final_result = sum_plus();};
alert("The sum is " + final_result);
是否有可能把for循環sum_plus函數裏面的代碼?或者更好地調用for循環內的函數?
任何幫助將不勝感激。
這正是我期待的事情。我試圖根據你告訴我的想法來想出一個解決方案,但不幸的是我無法想出任何東西。我一直在爲此工作好幾個小時,因爲我們的老師幾乎沒有完成功能,並且隨意地循環,所以我不得不自言自語。我在想這可能與sum_plus函數的語法有關,而不是for循環,但我不完全確定。 –
@MaxSilbert編輯 – 3ocene
@MaxSilbert認爲你也可以在那裏進行加票嗎? :) – 3ocene