Did I cheat the system?

I’m on the Basic Algorithm challenges and have been having a rough time. Finally I was able to solve one of the challenges without looking anything up, but I think I cheated the system a bit.

The challenge is “Repeat a string repeat a string” and the code I ended up with was:

function repeatStringNumTimes(str, num) {
  if (num >= 0) {
  return str = str.repeat(num);
  else {
    return "";
repeatStringNumTimes("abc", 3);

I thought “return str = str.repeat(num);” would be too simple but it solved all of the test conditions except for one instance when the num was -2. So I turned it into an If statement and had the else return the answer for the -2 test.

The system passed me, but I feel like I found a loop hole and cheated a bit. Was this what other people came up with or did I cheat the system?

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

I also added spoiler tags just in case a person wasn’t expecting an algorithm solution.

1 Like

Oh sorry, I posted without thinking. It’s easy to think you’re the only person on “Basic” anything.

Thanks for cleaning it up and I’ll be mindful in the future.

I wouldn’t say it’s cheating. You used the methods available to you, and you accounted for erroneous input. I’d recommend challenging yourself to not use the repeat() method, though.

Check out this and this for hints on how you might accomplish this.

My initial thought was to use the first method you linked to, but then repeat() just struck me as simpler solution if it worked so I decided to take a shot in the dark. Once I saw all the green check marks, getting the last seemed too easy to be true.

I appreciate the recommendations. I’ll try using both forms to complete the challenge too.