# Hacker Rank Repeated String Solution with JavaScript and comments

##### Posted on August 13th, 2020 in JavaScript by George

I have added the solution with the explanation in code comments for the Repeated String problem.

```
function repeatedString(s, n) {
let aInString;
//Spread the string into charachers and find out the number of "a"
//Store it in aInString
aInString = s.split("").filter((i) => { return i === "a" }).length;
// We check to make sure we are dealing with a string
//The string has content and n is a number
// the string length and number have to be between the given constraints
if (
(typeof s === "string" && s.length >= 1 && s.length <= 100) &&
(typeof n === "number" && n >= 0 && n <= 1000000000000)
) {
//Divide n to the length of the input string as integer
// Find out how many strings s inside n by length
let stringLengthInN = parseInt(n / s.length, 0);
//Multiply part1 to the number of a inside of our input string
// part 1 multiplied to the number of 'a' in the initial string
let part1 = stringLengthInN * aInString;
//Handle the reminder
let reminderString = s.slice(0, (n % s.length));
//Get the number of "a" from the reminder portion of string
let reminder = reminderString.split('').filter(i => i === "a").length;
//Add the integer part1 to the integer found in reminder
return (part1 + reminder);
}
return n;
}
console.log("result", repeatedString('zaxsda', 45345345345));
```

Thank you.