Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: open

Message Too Old, No Replies

Syntax Error: JS wants a ; before my "else if"?

   
1:09 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let me start by saying that while writing and debugging xhtml, css, and php brings me bucket loads of joy, working with javascript makes me want to gouge my own eyes out with a spoon.

Case in point: there may be all kinds of things wrong with the script posted below, but I can't get it past the

}else if(questionType == 'hs') {


at line 10 because the JS Console keeps telling me that this line is
SyntaxError: missing ; before statement

at this line.
As far as I can tell, all my semi-colons are in place prior to this line.

The full function is below, but I'm really just hoping someone can help me see why I need a semi-colon on that line.

In the meantime, I'm going to go sharpen a spoon.


function checkAnswer(questionType,questionNumber,chosenAnswer) {
if(questionType == 'mc'){
var correct = eval('"correct' + questionNumber + '"');//Get the correct answer using the questionNumber variable.
var correctAnswer = document.getElementById(correct).value; //pull the correct answer value from the hidden input for this question
if (chosenAnswer == correctAnswer) {//Compare the checked value against the correct answer,
str = "<span class='yes'>&#10004;</span>"; // insert the 'Heavy Checkmark'
} else {
str = "<span class='no'>&#10008;</span>";//else insert 'Heavy Ballot X'
}
} else if(questionType == 'hs') {
var correct = eval('"hsacorrect' + questionNumber + '"');//Get the correct answer using the questionNumber variable.
var correctAnswersRaw = document.getElementById(correct.value); //pull the string of correct answers
//convert the string into an array by breaking it at the commas and pushing into an array
var correctAnswers = split.correctAnswersRaw(",");
//now have two arrays (correctAnswers and chosenAnswer). Run comparison.
for (var i=0; i < correctAnswers.length; i++){
while (i--) {
if (correctAnswers[i] === chosenAnswer) {
right.push(chosenAnswer);
}
}
}
//RIGHT array should now contain all the chosen answers that were correct.
//compare RIGHT, number of answers CHOSEN, and number of answers CORRECT
//if they match, the question is correct
if (length.right == length.chosenAnswer && length.chosenAnswer == length.correctAnswers){
str = "<span class='yes'>&#10004;</span>"; // insert the 'Heavy Checkmark'
} else {
str = "<span class='no'>&#10008;</span>";//else insert 'Heavy Ballot X'.
}
} else {
str = "<p>You screwed something up.</p>";
}
return str;
}



Thanks a million for any insight you can provide.

Best,
cEM
1:32 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the right bracket on the left side from here:

} else if(questionType == 'hs') {


That's one too many cause there's one before that.
3:23 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



First, lets format it nicely:

function checkAnswer(questionType, questionNumber, chosenAnswer) {
if (questionType == 'mc') {
var correct = eval('"correct' + questionNumber + '"'); //Get the correct answer using the questionNumber variable.
var correctAnswer = document.getElementById(correct).value; //pull the correct answer value from the hidden input for this question
if (chosenAnswer == correctAnswer) { //Compare the checked value against the correct answer,
str = "<span class='yes'>&#10004;</span>"; // insert the 'Heavy Checkmark'
} else {
str = "<span class='no'>&#10008;</span>"; //else insert 'Heavy Ballot X'
}
} else if (questionType == 'hs') {
var correct = eval('"hsacorrect' + questionNumber + '"'); //Get the correct answer using the questionNumber variable.
var correctAnswersRaw = document.getElementById(correct.value); //pull the string of correct answers
//convert the string into an array by breaking it at the commas and pushing into an array
var correctAnswers = split.correctAnswersRaw(",");
//now have two arrays (correctAnswers and chosenAnswer). Run comparison.
for (var i = 0; i < correctAnswers.length; i++) {
while (i--) {
if (correctAnswers[i] === chosenAnswer) {
right.push(chosenAnswer);
}
}
}
//RIGHT array should now contain all the chosen answers that were correct.
//compare RIGHT, number of answers CHOSEN, and number of answers CORRECT
//if they match, the question is correct
if (length.right == length.chosenAnswer && length.chosenAnswer == length.correctAnswers) {
str = "<span class='yes'>&#10004;</span>"; // insert the 'Heavy Checkmark'
} else {
str = "<span class='no'>&#10008;</span>"; //else insert 'Heavy Ballot X'.
}
} else {
str = "<p>You screwed something up.</p>";
}
return str;
}


When I tried this code, I did not get a syntax error. Is there other code within this code block that you didn't include? It's possible the error is elsewhere but makes it seem like this is where the error is.

Side note, you should not be using eval in the code above, as all you're doing is concatenating strings.
6:15 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



First, lets format it nicely:

When you look at someone else's code, do you always have to reformat it before you can bear to look at it? ;)
The control structures come out even for me (using nbsp because I can never get any of the "pre" modes to work)
function checkAnswer(questionType,questionNumber,chosenAnswer)
  if(questionType == 'mc')
    if (chosenAnswer == correctAnswer)
    else
  else if(questionType == 'hs')
    for (var i = 0; i < correctAnswers.length; i++)
      while (i--)
        if (correctAnswers[i] === chosenAnswer)
    if (length.right == length.chosenAnswer && length.chosenAnswer == length.correctAnswers)
    else
  else
  return str;
7:43 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month




When you look at someone else's code, do you always have to reformat it before you can bear to look at it?

Yeah, pretty much. I usually just copy it into [jsbeautifier.org...] and it's especially helpful for identifying errors relating to incorrect closing braces, etc.

I can never get any of the "pre" modes to work

I wrap code examples in [quote][pre][code][/code][/pre][/quote].
9:37 pm on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, all. It was something funky elsewhere in the js file. Still tinkering to get the thing to work, but at least that particular error message has fled the scene.

Greatly appreciate the input.