Steps check for 'Repeat a string'

Tell us what’s happening:
I want to solve this challenge in a systematic way defining steps first as advised by ieelleen in the previous challenge.
link to the challenge:https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/repeat-a-string-repeat-a-string

Steps I believe I need to perform:

  1. Define a for loop or iteration to check each num
  2. Check if num is a positive number - this implies a comparison between num -num
  3. Return an empty string if num is not a positive number - if statement

please could you check them and provide feedback?
Thanks in advance!

Your code so far


function repeatStringNumTimes(str, num) {

return str;
}

repeatStringNumTimes("abc", 3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Repeat a String Repeat a String

Link to the challenge:

Hello there,

Those steps are correct, but you might want to rethink the order you perform them. You are not “check[ing] each num”, num is simply one integer.

Hope this helps

1 Like

Thank you!
I get it, so I am comparing different values of num and then returning an outcome based on its type, correct?
So if the integer is negative or positive, I return different results.

Yes. Make one check with the parameter num, then, depending on the outcome of that check, perform the appropriate steps.

When doing these challenges, looking at the tests is often a good place to start, and clarify what needs to be done.

One check with num inside an if statememt with a ===
i am confused with the comparison

I have worked out this on my own following your recomm and steps:


function repeatStringNumTimes(str, num) {
  let myArray = ['']
  for(let i= 0; i < str.length; i++) {
    if(num === 0 || num === -2) {
      return myArray
    } 
    else{
      myArray++

    }  

}

repeatStringNumTimes("abc", 3);
}

Right. Start from here:

Return an empty string if num is not a positive number

Then, why are you looping as many times as the str is long?

Also, what do you expect myArray++ to do?

1 Like

Indeed, it does not make sense.
I am doing that because that is normally the syntax but I guess in this case is different.
Let me try to work it out this time with i === +num inside the if statement
Thanks!

Hi there,

You don’t really need an array here. Again you just need to check if num is not positive (num < 0) in which case you return an empty string. You have to loop over num not str. Pay attention, you’re not returning anything in your else block. You’re on the way, try to refactor and we’re there to help you.

Congratulations for your effort to master.

1 Like

About myArray++
I did that out of guess because in HINT 4 says to make the variable to store the current variable and append the word to it
what does that mean in code?

Thank you!
I have appliend (num < 0)

function repeatStringNumTimes(str, num) {
  let myArray = ['']
  for(let i= 0; i < num.length; i++) {
    if(num < 0) {
      return myArray
    } 
    else{
      
   
    }  

}

repeatStringNumTimes("abc", 3);
}

You’re almost there. Try to refactor your code like this

let newStr = '' // <=== Initialize an empty str here
  for(let i= 0; i < num; i++) { // num is already a num, no num.length
    if(num < 0) {
      return newStr // return newStr that is empty for now or return ''
    } 
    else{
      // add str to newStr
     newStr += str
   
    }  
1 Like

I have refactor the code but it does not work

function repeatStringNumTimes(str, num) {
  let myString = ''
  for(let i= 0; i < num.length; i++) {
    if(num < 0) {
      return myString
    } 
    else{
      myString += str

    }  

}

}

repeatStringNumTimes("abc", 3);

I dont understand what is missing

Delete .length next num in your loop.

1 Like

Thanks! I did and now it works :slight_smile:

Happy to hear that. You can mark this as solved.

1 Like

You were right! I am learning to think! :slight_smile: