简体   繁体   中英

How do I append/update a message to a single alert message in javascript?

I'm trying to print the entire results at once onto a single alert message. I tried to put it in with the same variable answerText where everything prints but it doesn't work. I tried adding alert to all of them and it pops up separately. Anyone knows how to put all results into a single alert message, not all over the place? Right now, the code doesn't show any alert boxes. Thanks!

JavaScript:

var ans = new Array;
var done = new Array;
var yourAns = new Array;
//var explainAnswer = new Array;

var score = 0;
ans[1] = "D";
ans[2] = "A";
ans[3] = "D";
ans[4] = "A";
ans[5] = "B";

function Engine(question, answer) 
{
yourAns[question] = answer;
}

function Score()
{
alert("TESTING!");
var answerText = "How did you do?\n------------------------------------\n";
for(i = 1; i <= 5; i++)
{
    answerText = answerText + "\nQuestion :" + i + "\n";
    if(ans[i] != yourAns[i])
    {
        answerText = answerText + "\nThe correct answer was " + ans[i];
    }
    else
    {
        answerText = answerText + " \nCorrect! \n";
        score++;
    }
}
answerText = answerText + "\n\nYour total score is : " + score + "\n";
 }

try

 function Score()
    {
    alert("TESTING!");
    var answerText = "How did you do?\n------------------------------------\n";
    for(i = 1; i <= 5; i++)
    {
        answerText = answerText + "\nQuestion :" + i + "\n";
        if(ans[i] != yourAns[i])
        {
            answerText = answerText + "\nThe correct answer was " + ans[i];
        }
        else
        {
            answerText = answerText + " \nCorrect! \n";
            score++;
        }
    }
    answerText = answerText + "\n\nYour total score is : " + score + "\n";
    alert(answerText );

 }

There are two issues with your code

  • You declared a function Score but never called it.
  • answerText is created but you never passed its value to alert.


var ans = new Array;
var done = new Array;
var yourAns = new Array;
//var explainAnswer = new Array;

var score = 0;
ans[1] = "D";
ans[2] = "A";
ans[3] = "D";
ans[4] = "A";
ans[5] = "B";

function Engine(question, answer) 
{
yourAns[question] = answer;
}

function Score()
{
alert("TESTING!");
var answerText = "How did you do?\n------------------------------------\n";
for(i = 1; i <= 5; i++)
{
    answerText = answerText + "\nQuestion :" + i + "\n";
    if(ans[i] != yourAns[i])
    {
        answerText = answerText + "\nThe correct answer was " + ans[i];
    }
    else
    {
        answerText = answerText + " \nCorrect! \n";
        score++;
    }
}
answerText = answerText + "\n\nYour total score is : " + score + "\n";
    // shows an alert
    alert(answerText);
 }
// call a Score function
Score();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM