2017-02-07 24 views
0

プロンプトボックスがあり、条件が与えられたときに、プロンプト値が返されたときにcheckNumbers関数を呼び出したいが、値を渡す方法がわからない。プロンプトボックスから関数を呼び出す

function getNumbersMakeProblem() { 

    // a, b are declared up here usually 
    userAnswer = prompt("State the value of the unknown variable in the \ 
    following problem below. If your answer is not an integer, round to the \ 
    nearest hundredth. " + a + "x - " + b + " = " + 3*a); 

    return userAnswer() 
} 

function checkAnswer(a, b, userAnswer) { 

    var numAndDenom = userAnswer.split('/'), 
     result = parseInt(numAndDenom[0], 10)/parseInt(numAndDenom[1], 10), 
     resultToHundreth = Number(result.toFixed(2)), 
     correctAnswer = ((3 * Number(b))/Number(a)); 

    if (Number(userAnswer) === correctAnswer) { 
    alert("CORRECT!!!"); 
    } 
    // Further Conditions 
} 
+0

あなたは 'userAnswer'を返す直前に' 'checkAnswer(A、B、userAnswer)を呼び出します。 https://jsfiddle.net/77d6ngk6/ – haim770

+0

https://jsfiddle.net/h0ysz3a5/を参照してください。paramsで関数を呼び出す必要があります –

+0

'' if(condition)myFunction(a、b、userAnswer); ''を考慮してください。 – Crowes

答えて

1
<input type="text" id="input_a"> 
<input type="text" id="input_b"> 
<input type="button" value="Run" onclick="getNumbersMakeProblem();"> 

//script tag here 
function getNumbersMakeProblem(){ 
    var a = document.getElementById("input_a").value, 
    a = a.toLowerCase(); 

    var b = document.getElementById("input_b").value; 

//variables a and b are the users input for their birth month and day respectively 

if (a === "january") { 
    a = 1; 
} 

// leaving other months out to save space 

if (a === "december") { 
    a = 12; 
} 
var userAnswer = prompt("State the value of the unknown variable in the \ 
following problem below. If your answer is not an integer, round to the \ 
nearest hundredth. " + a + "x - " + b + " = " + 3*a); 


checkAnswer(a,b,userAnswer); // Single change here 
} 



// IDK how to incorporate this second piece after the first prompt 

function checkAnswer(a, b, userAnswer) { 

    var numAndDenom = userAnswer.split('/') 
    var result = parseInt(numAndDenom[0], 10)/parseInt(numAndDenom[1], 10); 
    var resultToHundreth = Number(result.toFixed(2)); 
    var correctAnswer = ((3*Number(a) + Number(b))/Number(a)); 

    if (Number(userAnswer) === correctAnswer) { 
    alert("CORRECT!!!"); 
    } 
    else if (userAnswer === correctAnswer.toFixed(2)) { 
    alert("AWESOME!!!"); 
    } 
    else if(userAnswer === correctAnswer.toFixed(1)) { 
    alert("You were supposed to round to the nearest HUNDRETH.") 
    } 
    else if (Number(result) === correctAnswer) { 
    alert("Now convert your answer to a decimal.") 
    } 
    else { 
    alert("TRY AGAIN..."); } 

    } 
// closing script tag here 
+0

return文の後にどのように関数が呼び出されると思いますか? –

+0

注目。編集されました。ありがとう –

+0

それはあなたの問題を解決した@ダビ・ロビ –

関連する問題