0

I am building a website that teaches people how to code a website. I am trying to add a feature where they code the exercise into a text box and I then compare that to a string to see if they got it right or not yet. I am running into an issue though when there is quotes inside the strings answer because that then ends the string thus cutting off some of the answer. How can I get around this?

All feedback is greatly appreciated!

Here is an example of a strings answer that screws it up:

var answer = "var greeting="Hello World!"; ";

The second pair of quotes end the string's declaration early. Is there a way to include all of it including the second pair of quotes in the declaration?

AwesomeTN
  • 398
  • 2
  • 5
  • 16
  • Please post your code so far and a demo to reproduce the issue. Code speaks better that words. – elclanrs Jan 27 '14 at 03:52
  • 7
    How can you teach someone things you don't know yourself? o_O – zerkms Jan 27 '14 at 03:54
  • Why not use some websites that offer that capabilities ex: `jsfiddle.net`? – HTTP Jan 27 '14 at 03:55
  • It is an assignment so I can't use any templet/website. I am teaching a very basic site that just covers HTML and CSS. I know that. I don't know this question – AwesomeTN Jan 27 '14 at 03:57
  • @AwesomeTN: put "javascript string quote" request in http://google.com And next time google (now you know google exists) **before** you ask a question. – zerkms Jan 27 '14 at 03:58
  • What if I type `var greeting = "Hello World!";`? Am I wrong? – Niet the Dark Absol Jan 27 '14 at 04:02
  • Just a note: I think it's better to check what your visitor's code does instead of the code itself. There are multiple ways of doing the same thing, so maybe you think he is wrong but he isn't. – Oriol Jan 27 '14 at 04:09

1 Answers1

1

You can:

  • Escape the quotes with \:

    var answer = "var greeting=\"Hello World!\"; ";
    var answer = 'var greeting=\'Hello World!\'; ';
    
  • Use different quotes:

    var answer = "var greeting='Hello World!'; ";
    var answer = 'var greeting="Hello World!"; ';
    
Oriol
  • 274,082
  • 63
  • 437
  • 513