Skip to content

Commit 6a24f8a

Browse files
authored
fix(curriculum): tests passing with seed code in Calorie counter (freeCodeCamp#55585)
1 parent 75f6ae2 commit 6a24f8a

File tree

5 files changed

+55
-34
lines changed

5 files changed

+55
-34
lines changed

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-form-validation-by-building-a-calorie-counter/63b60cfaca25bb27edd40f62.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ Finally, you need to link your JavaScript file to your HTML. Create a `script` e
1414
You should have a `script` element.
1515

1616
```js
17-
assert.isAtLeast(document.querySelectorAll('script')?.length, 1);
17+
const bodyEnding = code.split(/<\/main>/)?.[1]?.split(/<\/body>/)?.[0];
18+
assert.match(bodyEnding, /<script[^>]*>\s*<\/script>/);
1819
```
1920
2021
Your `script` element should have the `src` set to `./script.js`.
2122
2223
```js
23-
assert.match(code, /script\s*?src\s*?=\s*?('|")(\.\/)?script\.js\1/);
24+
const bodyEnding = code.split(/<\/main>/)?.[1]?.split(/<\/body>/)?.[0];
25+
assert.match(bodyEnding, /<script\s*src\s*=\s*('|")(\.\/)?script\.js\1\s*>\s*<\/script>/);
2426
```
2527
2628
# --seed--

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-form-validation-by-building-a-calorie-counter/63b61490e633a22b4593e62f.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ assert.match(code, /document\.getElementById\(\s*('|")add-entry\1\s*\)/g);
2626
You should assign the `#add-entry` element to `addEntryButton`.
2727

2828
```js
29-
assert.equal(addEntryButton, document.getElementById('add-entry'));
29+
assert.deepEqual(addEntryButton, document.getElementById('add-entry'));
3030
```
3131

3232
You should declare a variable called `clearButton`.
@@ -44,13 +44,14 @@ assert.match(code, /document\.getElementById\(\s*('|")clear\1\s*\)/g);
4444
You should assign the `#clear` element to `clearButton`.
4545

4646
```js
47-
assert.equal(clearButton, document.getElementById('clear'));
47+
assert.deepEqual(clearButton, document.getElementById('clear'));
4848
```
4949

5050
You should declare a variable called `output`.
5151

5252
```js
53-
assert.isDefined(output);
53+
// .isDefined cannot be used for `output`, as browser defines such variable on it own.
54+
assert.match(code, /const\s+output/);
5455
```
5556

5657
You should use `document.getElementById()` to get the `#output` element.
@@ -62,7 +63,9 @@ assert.match(code, /document\.getElementById\(\s*('|")output\1\s*\)/g);
6263
You should assign the `#output` element to `output`.
6364

6465
```js
65-
assert.equal(output, document.getElementById("output"));
66+
// assert.deepEqual(output, document.getElementById('output')); cannot be used,
67+
// as browser defines `output` variable on it own for `#output`.
68+
assert.match(code, /const\s+output\s*=\s*document.getElementById\(('|")output\1\)/);
6669
```
6770

6871
# --seed--

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-form-validation-by-building-a-calorie-counter/63bf461011fca327d3b60fa8.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,6 @@ Declare a `regex` variable and assign it the value from the example above. In fu
1919

2020
# --hints--
2121

22-
You should remove the `cleanStrArray` variable.
23-
24-
```js
25-
assert.notMatch(cleanInputString.toString(), /cleanStrArray/);
26-
```
27-
28-
You should remove the `strArray` variable.
29-
30-
```js
31-
assert.notMatch(cleanInputString.toString(), /strArray/);
32-
```
33-
34-
You should not have a `for` loop.
35-
36-
```js
37-
assert.notMatch(cleanInputString.toString(), /for/);
38-
```
39-
4022
You should declare a `regex` variable.
4123

4224
```js

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-form-validation-by-building-a-calorie-counter/63c8c15fd337ad24b9b68049.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Add an `if` statement that checks if `invalidInputMatch` is truthy.
2626
You should add an `if` statement to your `getCaloriesFromInputs` function.
2727

2828
```js
29-
assert.match(getCaloriesFromInputs.toString(), /if\s*\(/);
29+
const functionCode = code.split(/getCaloriesFromInputs/)?.[1]?.split(/\}\s*\}/)?.[0];
30+
assert.match(functionCode, /if\s*\(/);
3031
```
3132
3233
You should check the truthiness of `invalidInputMatch` in your `if` condition.
@@ -38,7 +39,8 @@ assert.match(getCaloriesFromInputs.toString(), /if\s*\(\s*invalidInputMatch\s*\)
3839
Your `if` statement should be inside your `for` loop.
3940
4041
```js
41-
assert.isBelow(getCaloriesFromInputs.toString().indexOf("for"), getCaloriesFromInputs.toString().indexOf("if"));
42+
const forCode = code.split(/getCaloriesFromInputs/)?.[1]?.split(/for\s*\(/)?.[1]?.split(/\}/)?.[0];
43+
assert.match(forCode, /if\s*\(\s*invalidInputMatch\s*\)/);
4244
```
4345
4446
# --seed--

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-form-validation-by-building-a-calorie-counter/63c9e8fe3a6f022a05a04675.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,65 @@ Using the same interpolation syntax, add a second `p` element with the text `con
1414
You should add a second `p` element to your template literal.
1515

1616
```js
17-
const htmlString = code.split(/output\s*\.\s*innerHTML\s*=\s*/)[1].split(/`/)[1];
18-
assert.isAtLeast(htmlString.match(/<p>/g).length, 2);
17+
assert.isAtLeast(getTemplateContents(code)?.match(/<p>[^<]*<\/p>/g)?.length, 2);
1918
```
2019
2120
Your second `p` element should be on a new line.
2221
2322
```js
24-
const htmlString = code.split(/output\s*\.\s*innerHTML\s*=\s*/)[1].split(/`/)[1];
25-
assert.match(htmlString, /\n\s*<p/)
23+
assert.match(getTemplateContents(code), /<hr>\s*<p>[^<]*<\/p>\n\s*<p>[^<]*<\/p>/);
2624
```
2725
2826
Your second `p` element should come after your existing `p` element.
2927
3028
```js
31-
const htmlString = code.split(/output\s*\.\s*innerHTML\s*=\s*/)[1].split(/`/)[1];
32-
assert(htmlString.match(/<p>\$\{budgetCalories\}\s*Calories\s*Budgeted<\/p>\n\s*<p/));
29+
assert.match(getTemplateContents(code), /<p>\$\{budgetCalories\}\s*Calories\s*Budgeted<\/p>\s*<p>/);
3330
```
3431
3532
Your second `p` element should have the text `${consumedCalories} Calories Consumed`.
3633
3734
```js
38-
const htmlString = code.split(/output\s*\.\s*innerHTML\s*=\s*/)[1].split(/`/)[1];
39-
assert(htmlString.match(/<p>\$\{consumedCalories\}\s*Calories\s*Consumed<\/p>/));
35+
const secondP = getTemplateContents(code)?.split(/<p>/)?.[2];
36+
assert.match(secondP, /\$\{consumedCalories\}\s*Calories\s*Consumed<\/p>/);
37+
```
38+
39+
You should add a third `p` element to your template literal.
40+
41+
```js
42+
assert.lengthOf(getTemplateContents(code)?.match(/<p>[^<]*<\/p>/g), 3);
43+
```
44+
45+
Your third `p` element should be on a new line.
46+
47+
```js
48+
assert.match(getTemplateContents(code), /<hr>\s*<p>[^<]*<\/p>\s*<p>[^<]*<\/p>\n\s*<p>[^<]*<\/p>/);
49+
```
50+
51+
Your third `p` element should come after your second existing `p` element.
52+
53+
```js
54+
assert.match(getTemplateContents(code), /<p>\$\{consumedCalories\}\s*Calories\s*Consumed<\/p>\s*<p>/);
55+
```
56+
57+
Your third `p` element should have the text `${exerciseCalories} Calories Burned`.
58+
59+
```js
60+
const thirdP = getTemplateContents(code)?.split(/<p>/)?.[3];
61+
assert.match(thirdP, /\$\{exerciseCalories\}\s*Calories\s*Burned<\/p>/);
4062
```
4163
4264
# --seed--
4365
66+
## --after-user-code--
67+
68+
```js
69+
function getTemplateContents(code) {
70+
return code
71+
.split(/output\s*\.\s*innerHTML\s*=\s*/)?.[1]
72+
?.split(/`/)?.[1];
73+
}
74+
```
75+
4476
## --seed-contents--
4577
4678
```html

0 commit comments

Comments
 (0)