0

I'm just learning now. Can you please help me, why am I not getting the correct output. This is my code:

//ask questions
var quiz = [
  ["When is Bulgaria established?", 681],
  ["What year was it before 16 years?", 2000],
  ["When does WWII ends?", 1945]
];

//variables
var answer = [];
var correct = [];
var wrong = [];
var correctAns = 0;
var wrongAns = 0;
var oList = "<ol>";

//function to print the result in ordered list
function printResult(result){
  for(var j = 0; j < result.length; j++){
    oList += "<li>" + result[i] + "</li>";
  }
  oList += "</ol>";
  return oList;
}

function print(message) {
  document.getElementById('output').innerHTML = message;
}



//looping, adding correct and wrong answeres
for(var i = 0; i < 3; i++) {
  answer[i] = prompt(quiz[i][0]);
  if(parseInt(answer[i]) == quiz[i][1]){
    correct.push(quiz[i][0]);
    correctAns++;
  } else {
    wrong.push(quiz[i][0]);
    wrongAns++;
  }
}


//print logic
if(correct.length < 1 || correct == undefined){
  print("You did not guess any of the quiestions!");
} else if (correct.length >= 1){
  print("You have guessed " + correctAns + " questions.");
  print(printResult(correct));
  print("You have " + wrongAns + " wrong answeres.");
  if(wrongAns > 0){
    print(printResult(wrong));
  }
}

I have watched this code over and over again and I still can't understand why am I getting undefined as a result. In the debugger, after the loop I check my vars and everything seems ok.

10
  • what should happen ? Commented Aug 20, 2016 at 8:46
  • well, in this jsfiddle jsfiddle.net/owf1pgay it works. So please give a more specific info about what doesn't seem to work in your code :) Commented Aug 20, 2016 at 8:46
  • First error: Your definition of variable quiz is wrong (array of arrays instead of array of objects). Make it: var quiz = [{"When was Bulgaria Established?":"681"},{...}...];. After this fix, try and report if still not working. Commented Aug 20, 2016 at 8:47
  • How is this 'undefined' error manifesting itself? Is it in the console or printed to the page? Commented Aug 20, 2016 at 8:48
  • @levu in the same jsfiddle I get the same result: 1. undefined 2. undefined Unordered list bullet?! undefined> Commented Aug 20, 2016 at 8:51

3 Answers 3

3

In your printResult function you are using var i instead of j,

Also you better use innerHtml+=message;

//ask questions
var quiz = [
  ["When is Bulgaria established?", 681],
  ["What year was it before 16 years?", 2000],
  ["When does WWII ends?", 1945]
];

//variables
var answer = [];
var correct = [];
var wrong = [];
var correctAns = 0;
var wrongAns = 0;
   

//function to print the result in ordered list
function printResult(result){
//HERE:
 var oList = "<ol>";
  for(var j = 0; j < result.length; j++){
    oList += "<li>" + result[j] + "</li>";
  }
  oList += "</ol>";
  return oList;
}

function print(message) {
  document.getElementById('output').innerHTML += message;
}



//looping, adding correct and wrong answeres
for(var i = 0; i < 3; i++) {
  answer[i] = prompt(quiz[i][0]);
  if(parseInt(answer[i]) == quiz[i][1]){
    correct.push(quiz[i][0]);
    correctAns++;
  } else {
    wrong.push(quiz[i][0]);
    wrongAns++;
  }
}


//print logic
if(correct.length < 1 || correct == undefined){
  print("You did not guess any of the quiestions!");
} else if (correct.length >= 1){
  print("You have guessed " + correctAns + " questions.");
  print(printResult(correct));
  print("You have " + wrongAns + " wrong answeres.");
  if(wrongAns > 0){
    print(printResult(wrong));
  }
}
<div id="output">
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

Can you please tell me, why am I getting an Unordered list bullet for the wrong answers and not a numbered list? I can't seem to get that and I assume it will be the same stupid mistake as i - j.
Your var oList = "<ol>", put it inside the function, see edit.
I love you! Just changed that and everything works just great as it should!!!! Thank you! Why the heck is this so important, I mean if it is a global var it crashes?
It doesn't matter in this case if it was global or not, but you are initializing it with "ol" outside of the function and when you are in the function for the second time it does not start with "ol" so the final html is missing opening tag.
2

Basically you have three problems.

  • reuse of oList, the variable should be inside declared and used only in printResult.

  • Inside of printResult, use of i where j have been used and

  • At print, you replace the actual content with new content.

Just a small hint with variable names for counting. It is good practise to start always with i instead of j and go on with the letters in the alphabet.

var quiz = [["When is Bulgaria established?", 681], ["What year was it before 16 years?", 2000], ["When does WWII ends?", 1945]],
    answer = [],
    correct = [],
    wrong = [],
    correctAns = 0,
    wrongAns = 0;

//function to print the result in ordered list
function printResult(result) {
    var oList = "<ol>"; // !!! move variable inside of the function
    for (var j = 0; j < result.length; j++) {
        oList += "<li>" + result[j] + "</li>"; // !!! use j indstead if i
    }
    oList += "</ol>";
    return oList;
}

function print(message) {
    document.getElementById('output').innerHTML += message; // !!! append message
}

//looping, adding correct and wrong answeres
for (var i = 0; i < 3; i++) {
    answer[i] = prompt(quiz[i][0]);
    if (parseInt(answer[i]) == quiz[i][1]) {
        correct.push(quiz[i][0]);
        correctAns++;
    } else {
        wrong.push(quiz[i][0]);
        wrongAns++;
    }
}

//print logic
if (correct.length < 1 || correct == undefined) {
    print("You did not guess any of the quiestions!");
} else if (correct.length >= 1) {
    print("You have guessed " + correctAns + " questions.");
    print(printResult(correct));
    print("You have " + wrongAns + " wrong answeres.");
    if (wrongAns > 0) {
        print(printResult(wrong));
    }
}

3 Comments

Thanks so much, Nina! :) Can you please explain me, why the oList is so important to be a local and if it's global it crashes. In this example isn't it the same? / it seem's it's not but I can't get why? /
you declare the variable global and initialize it with an open ol tag. then with calling printResult, you append the string with more characters and return at the end of the function the string. that works. but for the next call of printResult you have still the content of the last action inside and not only the starting ol tag. and while you do not need the variable outside (remember, you return the content), you better use a local variable for it and prevent that the content of the last action is inside.
Ahaaaaaa! Thank you so much, Nina! :)
2

Your main mistake is using i intead of j:

for(var j = 0; j < result.length; j++){
   oList += "<li>" + result[j] + "</li>";// here was i before
}

1 Comment

HAHAHAH, newbie here! :@

Your Answer

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.