Pages

Tuesday, March 1, 2016

REPEAT A STRING REPEAT A STRING

Challenge here was to repeat a given string (first argument) num times (second argument). Return an empty string if num is a negative number.

The clue given on the challenge was:

Global String Object

While searching for clue I found a function called repeat the syntax for which is as follows:

str.repeat(count)
  
count
An integer between 0 and +∞: [0, +∞), indicating the number of times to repeat the string in the newly-created string that is to be returned.
The solution then got quite obvious as given below:

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

No comments:

Post a Comment