Modify if statement (#36088)

* Modify if statement

Change the solution so an empty string is returned when num is less than 1 rather than less than 0 for consistency with instructions.

* Add test 

New test checks that if num is 0, an empty string is returned

* Fix test 

Fixing errors in newly proposed test that checks that empty string is returned when num is 0

* Modify intermediate solution

Changing if statement in intermediate solution to check whether num is less than 1 rather than less than 0. (Also adding curly brackets to that solutions if/else statements.)

* fix: removed unnecessary assert message arguments
This commit is contained in:
Natalie Cardot
2019-05-20 18:08:20 -07:00
committed by Manish Giri
parent 39c43dbe8a
commit 545b8a6f34
2 changed files with 16 additions and 13 deletions

View File

@ -22,19 +22,21 @@ Remember to use <a href="http://forum.freecodecamp.org/t/how-to-get-help-when-yo
```yml ```yml
tests: tests:
- text: <code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>. - text: <code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>.
testString: assert(repeatStringNumTimes("*", 3) === "***", '<code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>.'); testString: assert(repeatStringNumTimes("*", 3) === "***");
- text: <code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>. - text: <code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>.
testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc", '<code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>.'); testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc");
- text: <code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>. - text: <code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>.
testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc", '<code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>.'); testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc");
- text: <code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>. - text: <code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>.
testString: assert(repeatStringNumTimes("abc", 1) === "abc", '<code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>.'); testString: assert(repeatStringNumTimes("abc", 1) === "abc");
- text: <code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>. - text: <code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>.
testString: assert(repeatStringNumTimes("*", 8) === "********", '<code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>.'); testString: assert(repeatStringNumTimes("*", 8) === "********");
- text: <code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>. - text: <code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>.
testString: assert(repeatStringNumTimes("abc", -2) === "", '<code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>.'); testString: assert(repeatStringNumTimes("abc", -2) === "");
- text: The built-in <code>repeat()</code>-method should not be used - text: The built-in <code>repeat()</code> method should not be used.
testString: assert(!/\.repeat/g.test(code), 'The built-in <code>repeat()</code>-method should not be used'); testString: assert(!/\.repeat/g.test(code));
- text: <code>repeatStringNumTimes("abc", 0)</code> should return <code>""</code>.
testString: assert(repeatStringNumTimes("abc", 0) === "");
``` ```
@ -66,7 +68,7 @@ repeatStringNumTimes("abc", 3);
```js ```js
function repeatStringNumTimes(str, num) { function repeatStringNumTimes(str, num) {
if (num < 0) return ''; if (num < 1) return '';
return num === 1 ? str : str + repeatStringNumTimes(str, num-1); return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
} }

View File

@ -65,13 +65,14 @@ Make the variable created store the current value and append the word to it.
## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: ## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution:
function repeatStringNumTimes(str, num) { function repeatStringNumTimes(str, num) {
if(num < 0) if (num < 1) {
return ""; return "";
if(num === 1) } else if (num === 1) {
return str; return str;
else } else {
return str + repeatStringNumTimes(str, num - 1); return str + repeatStringNumTimes(str, num - 1);
} }
}
![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") <a href='https://repl.it/CLjU/21' target='_blank' rel='nofollow'>Run Code</a> ![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") <a href='https://repl.it/CLjU/21' target='_blank' rel='nofollow'>Run Code</a>