homepage Welcome to WebmasterWorld Guest from 54.226.180.223
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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




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

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

 

drhowarddrfine




msg:4676561
 1:32 pm on Jun 1, 2014 (gmt 0)

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.

Fotiman




msg:4676583
 3:23 pm on Jun 1, 2014 (gmt 0)

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.

lucy24




msg:4676615
 6:15 pm on Jun 1, 2014 (gmt 0)

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;

Fotiman




msg:4676623
 7:43 pm on Jun 1, 2014 (gmt 0)


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].

createErrorMsg




msg:4676637
 9:37 pm on Jun 1, 2014 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved