I’m not sure, please correct me(and remove my reply) if I’m wrong.
As I checked the validation logic with the source code, seems the regex used to test the source code is not valid.
The expected logical answer could be as following(just like @akalo did)
function printManyTimes(str) {
"use strict";
// change code below this line
const SENTENCE = str + " is cool!";
for(let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
// change code above this line
}
printManyTimes("freeCodeCamp");
The last check fails, and this is the error(I’m on chrome 67 desktop)
// running test
Unexpected token ?
// tests completed
I just tried to dive into source codes and found the script for checking the user input, seems it’s the one as following
webpackJsonp([200374151356091], {
/***/
3940: /***/
(function(module, exports) {
module.exports = {
"data": {
"challengeNode": {
"title": "Declare a Read-Only Variable with the const Keyword",
"guideUrl": null,
"description": ["<code>let</code> is not the only new way to declare variables. In ES6, you can also declare variables using the <code>const</code> keyword.", "<code>const</code> has all the awesome features that <code>let</code> has, with the added bonus that variables declared using <code>const</code> are read-only. They are a constant value, which means that once a variable is assigned with <code>const</code>, it cannot be reassigned.", "<blockquote>\"use strict\"<br>const FAV_PET = \"Cats\";<br>FAV_PET = \"Dogs\"; // returns error</blockquote>", "As you can see, trying to reassign a variable declared with <code>const</code> will throw an error. You should always name variables you don't want to reassign using the <code>const</code> keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice when naming constants is to use all uppercase letters, with words separated by an underscore.", "<hr>", "Change the code so that all variables are declared using <code>let</code> or <code>const</code>. Use <code>let</code> when you want the variable to change, and <code>const</code> when you want the variable to remain constant. Also, rename variables declared with <code>const</code> to conform to common practices, meaning constants should be in all caps."],
"challengeType": 1,
"fields": {
"blockName": "ES6",
"tests": [{
"text": "<code>var</code> does not exist in your code.",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> does not exist in your code.');"
}, {
"text": "<code>SENTENCE</code> should be a constant variable declared with <code>const</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g), '<code>SENTENCE</code> should be a constant variable declared with <code>const</code>.');"
}, {
"text": "<code>i</code> should be declared with <code>let</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/(let i)/g), '<code>i</code> should be declared with <code>let</code>.');"
}, {
"text": "<code>console.log</code> should be changed to print the <code>SENTENCE</code> variable.",
"testString": "getUserInput => assert(getUserInput('index').match(/console.log/(/s*?SENTENCE/s*?/)/s*?;/g), '<code>console.log</code> should be adjusted to print the variable <code>SENTENCE</code>.');"
}]
},
"required": [],
"files": {
"indexhtml": null,
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": "function printManyTimes(str) {\n \"use strict\";\n\n // change code below this line\n\n var sentence = str + \" is cool!\";\n for(var i = 0; i < str.length; i+=2) {\n console.log(sentence);\n }\n\n // change code above this line\n\n}\nprintManyTimes(\"freeCodeCamp\");",
"head": "",
"tail": ""
},
"indexjsx": null
}
}
},
"pathContext": {
"challengeMeta": {
"introPath": "",
"template": null,
"required": [],
"nextChallengePath": "/javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const",
"id": "587d7b87367417b2b2512b41"
},
"slug": "/javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword"
}
}
/***/
}
)
});
// WEBPACK FOOTER //
// path---javascript-algorithms-and-data-structures-es-6-declare-a-read-only-variable-with-the-const-keyword-c332abf4782735eb34ea.js
If you check the last test(“console.log should be changed to print the SENTENCE variable.”) logic, it’s a typo. (lines 26,27) as following:
{
"text": "<code>console.log</code> should be changed to print the <code>SENTENCE</code> variable.",
"testString": "getUserInput => assert(getUserInput('index').match(/console.log/(/s*?SENTENCE/s*?/)/s*?;/g), '<code>console.log</code> should be adjusted to print the variable <code>SENTENCE</code>.');"
}
Note the regex pattern is not valid?! the regex is
/console.log/(/s*?SENTENCE/s*?/)/s*?;/g
shouldn’t be just like
/console.log\(\s*?SENTENCE\s*?\)\s*?;/g
(maybe typo /
instead of \
)
Hope I got it right.
Thanks.
EDIT:
With some help of @camperextraordinaire, here is the already opened issue in github issue tracking. Sorry for mentioning, I thought I was the first one get the issue.