2015-12-21 51 views
-1

對於我的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循環內的函數?

任何幫助將不勝感激。

回答

0

重要的是你知道你的代碼在做什麼。要回答你的問題,我們真的需要看的唯一代碼是sum_plus功能

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

眼下這段代碼,每次迭代,通話sum_plus()sum_plus()將:

1)詢問用戶的數,並將其存儲在變量result

var result = input(); 

2)詢問用戶另一個號碼,通過1相乘,將其添加到變量result和把它返還。

return result += 1*prompt("Enter a number:"); 

所以,每次for循環運行的,你的代碼將採取兩個數字,添加它們,並將它們存儲在final_result,然後在最後會提醒進入了最後兩個數字的總和。

這聽起來像你想要你的代碼做什麼,是拿input()存儲在result然後運行一個循環,需要一個新的輸入值,並將其添加到result

sum_plus功能應該是這個樣子:

var sum_plus = function(iteration_count) { 
    // Take an input and store it in the variable `result` 
    var result = input(); 

    // Run a loop `iteration_count` times 
    for (var i = 0; i < iteration_count; i++) { 
    // Every time the loop runs, take an input and add it to result 
    result += input(); // This is the same as `result = result + input(); 
    } 
    // Return the value 
    return result; 
} 

現在你可以調用與功能:

alert(sum_plus(iterations("add"))); 

或者更長的版本,這是幾乎完全一樣的東西:

var iterations = iterations("add"); 
var sum = sum_plus(iterations); 
alert(sum); 

M確信你真的明白這是如何工作的,不要只是複製我的代碼。函數和循環是現代代碼的基石。沒有他們,你不會得到任何地方。如果您對發生的事情有任何疑問,請隨時詢問。

幫助功能:http://www.w3schools.com/js/js_functions.asp

幫助for循環:http://www.w3schools.com/js/js_loop_for.asp

+0

這正是我期待的事情。我試圖根據你告訴我的想法來想出一個解決方案,但不幸的是我無法想出任何東西。我一直在爲此工作好幾個小時,因爲我們的老師幾乎沒有完成功能,並且隨意地循環,所以我不得不自言自語。我在想這可能與sum_plus函數的語法有關,而不是for循環,但我不完全確定。 –

+0

@MaxSilbert編輯 – 3ocene

+0

@MaxSilbert認爲你也可以在那裏進行加票嗎? :) – 3ocene