2014-07-26 54 views
0

我相信這是一個範圍的問題,因爲我嘗試設置裏面firstPartCook我的功能userPrmpt和它的作品。我把它放在外面,而不是。所以它正在閱讀,但沒有保留返回的內容。我認爲通過將它放在測試變量中它可以工作,但它不會。迅速恢復。 (範圍問題)

這裏是我的代碼

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <title> Race Makin' </title> 
    <!-- Link to the JS portion for this script --> 
    <script src="riceMakin.js"></script> 
</head> 
<body> 
    <!-- not going for anything fancy. Just focusing on mechanics --> 
    <h1>Lets cook some damn rice</h1> 
    <h2> To start cooking Rice, please hit start</h2> 
    <button onclick="firstPartCook()">Start</button> 
    <h3>Below explains the status on the Rice Making Machine:</h3> 
    <!-- with JS i have what is inbetween the spans, switching from on and off --> 
    <p id="print">Status: Turned <span id="print">Off</span> </p>  
</body> 
</html> 

JS

//Global Vars here 
//Promp for the User to continue throught the script. To use what is returned, set to a var 
function userPrmpt(userResponse) { 
    prompt(userResponse); 
} 

// This function is for adding type to the DOM. 
function insertCopy (copy) { 
    var paragraphCreate = document.createElement("p"); 
    var copyNode = document.createTextNode(copy); 
    paragraphCreate.appendChild(copyNode); 
    var idSelecter = document.getElementById("print"); 
    //print is the id tag of the span 
    idSelecter.appendChild(paragraphCreate); 
} 

//This is where we start working on the mechanics of the script  
function firstPartCook() { 
    //var userAnswer = prompt("Welcome to the Rice Maker, want to start making rice?"); 
    var test = userPrmpt("Hello"); 
    if (test == "yes") { 
     console.log(test); 
     insertCopy("It worked"); 
    } else { 
     console.log(test); 
     insertCopy("Nope"); 
    } 
} 

回答

0

你不得不返回從提示的值,否則該函數將只返回undefined,這是沒有其他價值的任何函數的默認返回值E從它

function userPrmpt(userResponse){ 
    return prompt(userResponse); 
} 
+0

返回哇哈哈。謝啦。編碼中最簡單的事情是最令人討厭的問題。再次感謝! – user3878784

+0

不客氣! – adeneo