2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
56533eb9ac21ba0edf2244b5 | Escaping Literal Quotes in Strings | 1 | https://scrimba.com/c/c2QvgSr | 17568 |
Description
"
or '
inside of your string?
In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash (\
) in front of the quote.
var sampleStr = "Alan said, \"Peter is learning JavaScript\".";
This signals to JavaScript that the following quote is not the end of the string, but should instead appear inside the string. So if you were to print this to the console, you would get:
Alan said, "Peter is learning JavaScript".
Instructions
myStr
variable so that if you were to print it to the console, you would see:
I am a "double quoted" string inside "double quotes".
Tests
tests:
- text: You should use two double quotes (<code>"</code>) and four escaped double quotes (<code>\"</code>).
testString: assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
- text: 'Variable myStr should contain the string: <code>I am a "double quoted" string inside "double quotes".</code>'
testString: 'assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".");'
Challenge Seed
var myStr = ""; // Change this line
After Test
(function(){
if(typeof myStr === 'string') {
console.log("myStr = \"" + myStr + "\"");
} else {
console.log("myStr is undefined");
}
})();
Solution
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";