2017-10-11 101 views
-2

JS中的第一個程序。我不明白爲什麼我不能在第二個腳本中看到條件輸出。謝謝,先發制人的道歉不知道我在尋求什麼幫助。輸入/輸出非常非常基本的JS

<!DOCTYPE html> 
 
    <head> 
 
    <script> 
 
    function init() { 
 
     var num = window.prompt("Enter a number"); 
 
     num_span = document.getElementById("entered_num"); 
 
     num_span.innerHTML = num; 
 
    } 
 
    </script> 
 
    </head> 
 
    
 
    <body onload="init()"> 
 
    
 
    <h1>We math!</h1> 
 
    
 
    Your number is 
 
    <p id ="here">MEMEME</p> 
 
    perfect! 
 
    
 
    <script> 
 
    function isPerfect(n){ 
 
     var snowball = 1; 
 
     var half_n = n >> 1; 
 
     for (var k = 2; k <= half_n; k++){ 
 
      if (n % k == 0) { 
 
       snowball += k; 
 
      } 
 
     } 
 
     return (snowball == n); 
 
    } 
 
    
 
    if (!isPerfect(num)) { 
 
     document.getElementById("here").innerHTML = "not"; 
 
    } else { 
 
     document.getElementById("here").innerHTML = "definitely"; 
 
    } 
 
    </script> 
 
    </body>

+0

錯誤消息應該是一個線索...'num'被包裹在一個函數。它不能從該功能以外訪問。這被稱爲[關閉](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) – Liam

+1

爲什麼這是downvoted? OP正在詢問與其代碼執行順序有關的有效問題。它是一個「noob」問題,但是一個有效的問題。 – robbymurphy

回答

1

因爲如果(!isPerfect(NUM))只執行一次的邏輯,那就是正在加載頁面時。您希望邏輯在用戶提供號碼後執行。所以,你需要更改init方法這樣:

<!DOCTYPE html> 
 
    <head> 
 
    <script> 
 
    function init() { 
 
     var num = window.prompt("Enter a number"); 
 
     if (!isPerfect(num)) { 
 
     document.getElementById("here").innerHTML = "not"; 
 
     } else { 
 
     document.getElementById("here").innerHTML = "definitely"; 
 
     } 
 
    } 
 
    </script> 
 
    </head> 
 
    
 
    <body onload="init()"> 
 
    
 
    <h1>We math!</h1> 
 
    
 
    Your number is 
 
    <p id ="here">MEMEME</p> 
 
    perfect! 
 
    
 
    <script> 
 
    function isPerfect(n){ 
 
     var snowball = 1; 
 
     var half_n = n >> 1; 
 
     for (var k = 2; k <= half_n; k++){ 
 
      if (n % k == 0) { 
 
       snowball += k; 
 
      } 
 
     } 
 
     return (snowball == n); 
 
    } 
 
    
 
    </script> 
 
    </body>

+1

@Liam公平。我沒有刪除我移入init()的片段。現在更新。 – robbymurphy

0

<!DOCTYPE html> 
 
<head> 
 
<script> 
 
function init() { 
 
    var num = window.prompt("Enter a number"); 
 
    if (!isPerfect(num)) { 
 
    document.getElementById("here").innerHTML = document.getElementById("here").innerHTML + " not"; 
 
} else { 
 
    document.getElementById("here").innerHTML = document.getElementById("here").innerHTML + " definitely"; 
 
} 
 
} 
 
function isPerfect(n){ 
 
    var snowball = 1; 
 
    var half_n = n >> 1; 
 
    for (var k = 2; k <= half_n; k++){ 
 
     if (n % k == 0) { 
 
      snowball += k; 
 
     } 
 
    } 
 
    return (snowball == n); 
 
} 
 

 
</script> 
 
</head> 
 

 
<body onload="init()"> 
 

 
<h1>We math!</h1> 
 

 
Your number is 
 
<p id ="here"></p> 
 
perfect!

你想這樣的事情?

+0

我仍然沒有看到條件的兩個結果。 (我已經打開了「隱身」,以便不會加載舊版本,如果這一點也很重要的話)。 –

0

試試這個代碼

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <script> 
 
     function init() { 
 
      var num = prompt("Enter a number"); 
 
      if (num != null) { 
 
       document.getElementById("here").innerHTML = num ; 
 
      } 
 
     } 
 
    </script> 
 
    </head> 
 
    <body onload="init()"> 
 
     <h1>We math!</h1> 
 
     Your number is 
 
     <p id ="here">MEMEME</p> 
 
     perfect! 
 
    </body> 
 
</html>