Repeat a string repeat a string - Undefined

Tell us what’s happening:
Describe your issue in detail here.
I pass all the cases except for the 2 where the num is negative. I pass in my code and got undefined. May I know why?

  **Your code so far**

function repeatStringNumTimes(str, num) {
let arr = []
for(let i = 0; i < num; i++){
   if(num > 0){
    arr.push(str)
   
    var array = arr.join("")
    console.log(array)
   
  
  }
   else if (num < 1){
     return ""
   }
}


return array

}

console.log(repeatStringNumTimes("abc", 3));
console.log(repeatStringNumTimes("abc", -2)) 
console.log(repeatStringNumTimes("abc", 0)) 
  **Your browser information:**

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

Challenge: Repeat a String Repeat a String

Link to the challenge:

There is no variable “array”.

var arr = [];
// number is < 0 so loop doesn't run
return array; 

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.