JS Loops
dree-max opened this issue · 12 comments
Hello, it still doesn't give me the required answer
Thanks for the help though
try running **i from 0 to n inside loop ** . I think it might work.
still didnt work for me
Hi @dree-max, there's a super easy approach without loops if you want to... i.e. using .repeat()
.
You can simply solve this in 4 lines of code:
function scream(n) {
let str = 'a'.repeat(n)
console.log(str)
}
scream(10)
Lemme know if you have any questions. Cheers :)
Unfortunately this one has to have loops because it's what's been examined in this particular exercise!
Thank you for the other option though I tried it out and it works just fine
You can do it like this!
function scream(n)
{
let str = '';
while(n--)
{
str +='a';
}
console.log(str);
}
scream(10);
Hello @ dree-max, you have already figured that out but still useful to give my two cents.
In Pseudocode, that problem would be solved like this:
n = 5
i = 0
from i to n:
// do
In JavaScript, that would be solved like this:
function scream(n) {
let str = "";
for (let i = 0; i<n; i++)
str = str + 'a';
return str;
}
console.log(scream(2)); // aa
console.log(scream(5)); // aaaaa
function scream(n) {
let str = "";
for (let i = 0; i<n; i++)
str = str + 'a';
return str;
}
console.log(scream(3)); // aaa
console.log(scream(5)); // aaaaa
//This will solve the issue.
Happy to help :)
function scream(n) {
return Array(n+1).join('a');
}
console.log(scream(3)); // aaa
console.log(scream(5)); // aaaaa
Hey @dree-max , Easy approach without loops if you want to..(using an Array and then join it with 'a' as the separator.)
// simple solution with for loop
`function scream(n) {
let str = '';
for (let i = 0; i < n; ++i) {
str += 'a';
}
return str;
}
console.log(scream(5));
console.log(scream(10));`