Hello all,
I am facing a javascript coding challenge, which requires me to find all occurences of letter ‘a’ in a repeated string.
So, the given string needs to be repeated a respective number of times in order to be found the occurences of letter ‘a’ inside it and need to look at the ‘n’ number of letters from the string.
function repeatedString(s, n) {
let count = 0;
let repeated = "";
console.log(s.length);
s.length <= n ? repeated = s.repeat(Math.ceil(n/(s.length))) : repeated = s.repeat(Math.floor(n/(s.length)));
//console.log(repeated, repeated.length);
for (let i = 0; i <= n; i++) {
if (repeated.split('')[i] == 'a') {
count++;
}
}
return count;
}
console.log(repeatedString('abcac',10)); //abcacabcac
console.log(repeatedString('aba',10)); //abaabaabaa
console.log(repeatedString('a',1000000000000));
Now, at the last console.log() test of ‘1000000000000’, i get the error of invalid string length.
I have checked MDN documentation for the repeat function, but I could find or understand why this high number isn’t a valid one for repetition.
How could I solve this problem? Is it a BigInt?
Appreciate every response in advance.