2

I have been working on a simple math game and am having problems getting the overall answer results to return after the end of the game.

Here is what my return function looks like

 function pShowResults() {
 var pNumResults = document.getElementById("results");
 for (var i = 0; i <= 10; i++) {
 pNumStore.push(pNumGuess[i]);
 var pNumTable = document.createElement("div");
 pNumTable.innerHTML = (pNumGuess[i]);
 pNumResults.appendChild(pNumTable);
 }
 }

Here is the full script

Pretty much need debugging help. I new to this so I'm guessing there is a ton that's off, but as long as I can get the results fed back I should be fine.

SomeShinyObject
7,8316 gold badges41 silver badges60 bronze badges
asked Mar 21, 2013 at 3:27
0

4 Answers 4

1

You are not passing the value of x in many placess

$(document).ready(function () {
 //declare arrays and variables for use below
 var pNum1 = [];
 var pNum2 = [];
 var pNumAns = [];
 var pNumGuess = [];
 var pNumStore = [];
 var pNumCarry = 0;
 var pNumTrack = 0;
 var pNumMessageRight = ['Awesome Job!', 'Correct!', 'Great Job!'];
 var pNumMessageWrong = ['Oh No! That Was Wrong!', 'Incorrect!', 'That\'s Wrong'];
 $(".Play").click(function () {
 $("#popup").attr("class", "on");
 pNumTrack = 0;
 pNumGen(pNumTrack);
 });
 $(".pNumSubmit").click(function () {
 pNumCalc(pNumTrack-1);
 });
 $(".pNumNext").click(function () {
 pNumGen(pNumTrack);
 });
 function pNumGen(x) {
 pNum1[x] = (Math.round(Math.random() * 51));
 pNum2[x] = (Math.round(Math.random() * 51));
 pNumAns[x] = pNum1[x] + pNum2[x];
 $(".pNum1").html(pNum1[x]);
 $(".pNum2").html(pNum2[x]);
 $(".pNumGuess").val("");
 $(".pNum1").html(pNumTrack[x]);
 if (pNumTrack == 2) {
 $(".pNumNext").html("");
 $(".pNumSubmit").html("Close");
 pShowResults();
 }
 pNumTrack++;
 }
 function pNumCalc(x) {
 pNumGuess[x] = $(".pNumGuess").val();
 if (pNumGuess[x] == pNumAns[x]) {
 $(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
 $(".pNumNext").html("Next Question >")
 } else {
 $(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
 $(".pNumNext").html("Maybe The Next Question >")
 }
 }
 function pShowResults() {
 var pNumResults = document.getElementById("results");
 for (var i = 0; i < pNumGuess.length; i++) {
 pNumStore.push(pNumGuess[i]);
 var pNumTable = document.createElement("div");
 pNumTable.innerHTML = (pNumGuess[i]);
 pNumResults.appendChild(pNumTable);
 }
 }
});

Demo: Fiddle

answered Mar 21, 2013 at 3:42
Sign up to request clarification or add additional context in comments.

Comments

1

There is a function called pNumCalc in your code which you have set to take in an argument, but you never pass one in. You use the argument to store the results in the pNumGuess array, but since the argument is never passed in, the guesses are never stored, and you end up with undefined as the answers the user gave.

Updated fiddle: http://jsfiddle.net/dwdX9/2/. Not sure how close this is to what you actually want though, but hopefully it gets you on the right track.

Because StackOverflow wants code to to be included when JSFiddle is...:

pNumCalc(pNumTrack)
answered Mar 21, 2013 at 3:35

Comments

0

You forget to define array before use it.

function pShowResults() {
 var pNumStore = new Array();
 var pNumResults = document.getElementById("results");
 for (var i = 0; i <= 10; i++) {
 pNumStore.push(pNumGuess[i]);
 var pNumTable = document.createElement("div");
 pNumTable.innerHTML = (pNumGuess[i]);
 pNumResults.appendChild(pNumTable);
 }
}
answered Mar 21, 2013 at 3:34

1 Comment

If you look at the source code in the given fiddle, you will see that pNumStore is defined already.
0

I must suggest you should use jquery instead. After visiting your Fiddle seems like there are many problems with the code. and also your question is unclear.

for e.g.

 $(".pNumSubmit").click(function () {
//why x value not passed?
 pNumCalc();
 });
 function pNumCalc(x) {
 pNumGuess[x] = $(".pNumGuess").val();
 if (pNumGuess[x] == pNumAns[x]) {
 $(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
 $(".pNumNext").html("Next Question >")
 } else {
 $(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
 $(".pNumNext").html("Maybe The Next Question >")
 }
 }

Please clear which array is returning undefined so that others can help you.

answered Mar 21, 2013 at 3:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.