Hello,
Before I look at the suggested answers, I would like to to figure out why my code is wrong.
It passes all of the arguments except for the last one with the negative number.
The error code is “RangeError: Invalid count value”.
I’m stumped because I thought that this was straightforward and logical. But clearly I am missing something important.
Thanks.
function repeatStringNumTimes(str, num) {
// repeat after me
var resultString = str.repeat(num);
// store str.repeat() in variable resultString
if (num < 0 ) str = “”;
// if number is negative, empty string
else {
return resultString;
// else return the resultString that is repeated
}
}
repeatStringNumTimes(“abc”, 3);
repeatStringNumTimes(“abc”, 4);
repeatStringNumTimes(“abc”, -2);
Here are a couple thoughts you should consider:
-
look at your variables str and resultString and think about why when you return resultString you arent getting the value you are looking for. You are storing the if value in str and returning resultString
-
When you look at this function think about why you are storing the str.repeat(num) in a variable? do you need to do this? Try doing an str.repeat(num) with any string and just the negative number and see what happens. think about how you could return these results without storing in a variable. Maybe 2 return statements?
1 Like
String.prototype.repeat
throws an error when it’s given a negative integer. You should add a condition that checks if the number input is negative.
2 Likes
So I tried commenting out the old variable statement and bringing the repeat method down to the else return statement. I also added my own test statements.
Now I get a blank console instead of any values. I ran the code through my chrome dev tools’ console debugger and I get “undefined”. I’m not sure where I’m going wrong.
function repeatStringNumTimes(str, >num) {
// repeat after me
//var resultString = str.repeat(num);
// store str.repeat() in variable >resultString
if (num < 0 ) str = “”;
// if number is negative, empty string
else {
//return resultString;
return str.repeat(num);
// else return the result string
}
}
repeatStringNumTimes(“abc”, 3);
repeatStringNumTimes(“abc”, 4);
repeatStringNumTimes(“abc”, -2);
repeatStringNumTimes(“def”, 1);
repeatStringNumTimes(“def”, -1);
Never mind. I just solved it. From doing more reading about if/else syntax, I realized that my structure just wasn’t efficient enough. So this is what I went with.
function repeatStringNumTimes(str, >num) {
// repeat after me
//var resultString = str.repeat(num);
// store str.repeat() in variable >resultString
if (num > 0) {
return str.repeat(num);
}
// if number is negative, empty string
else {
//return resultString;
return “”;
// else return the result string
}
}
repeatStringNumTimes(“abc”, 3);
repeatStringNumTimes(“abc”, 4);
repeatStringNumTimes(“abc”, -2);
repeatStringNumTimes(“def”, 1);
repeatStringNumTimes(“def”, -1);
Nice job! That is a very nice simple solution. Sorry I didn’t get back to you earlier. That function is supposed to return an empty string right? I’m not quite there yet you are pretty far ahead of me.
When I run it through the FCC interpreter, it just gives a blank screen. So it was hard for me to tell at first. But when I run it through Chrome’s dev tools, it returns an empty string in quotes. So I guess it works.
Try returning a string with a stupid function like:
function return_string(str) {
return str
}
console.log(return_string(“string”))
And you will see that the return value is:
string
Without the quotes around it. So try the same thing but like this.
Console.log(return_string(""))
And then:
test_string = return_string("")
Then do:
console.log(typeof test_string)
String might be a bad word to use in the first example choose any word like “hamburger” or something
So I’m not sure if I am doing this right, but I tried your first bit of test code and it passed, but with a black box.
And with the console.log("") it passed.
However, when I try your code in the Chrome dev tools interpreter, it does not run or return a string. It just throws errors. Which tells me that my code prints an empty string in the dev tools console and it passes FCC’s console. But that your test code passes in the FCC console, but throws errors in the dev tools console.
Or maybe I’m not following?
Hmmm the point I was trying to show was that when you return a string it doesn’t return with quotes it just returns the word so when you return an empty string you should see nothing in the log. If you store an empty string in a variable and get the type of that variable it is still a string even though nothing is returned to the console.
Oh okay! Thanks. Sorry for the confusion.
Cool! I’m always looking for people to talk to about this stuff. If you get stuck on something and want to talk feel free to contact me.
1 Like
Absolutely. And I appreciate the help!
I’ve been skimming through Eloquent JavaScript and I just ordered Jon Duckett’s series on HTML and JS/jQuery. So I hope to improve my understanding.
My first language is python. I heard about this site and decided to jump in. If you haven’t done the intro to computer science and programming course from MIT I highly recommend it. Make sure to do the 2014 one I think it’s the best one.
1 Like
That is a good idea. My first introduction to programming was by completing the Python course on CodeCademy.com. But some things still take me a while to click.
I will have to check that course out.