javascript icon indicating copy to clipboard operation
javascript copied to clipboard

JS Loops

Open dree-max opened this issue 3 years ago • 10 comments

screenbud-cbd1390c-fb74-4cfd-9abf-4f5360820a58 while learning JS, i ran into a small issue and im stuck . Any help rendered would be appreciated

dree-max avatar Nov 10 '21 05:11 dree-max

screenbud-cbd1390c-fb74-4cfd-9abf-4f5360820a58 while learning JS, i ran into a small issue and im stuck . Any help rendered would be appreciated

Hi ,In the for loop ,both initial and final value of i is inclusive: for(let i=5;i<=10;i++) due to which **loop runs 6 times ** i think that might be the issue .Hope it helps.

sabhinav10 avatar Nov 12 '21 05:11 sabhinav10

Hello, it still doesn't give me the required answer Thanks for the help though

dree-max avatar Nov 13 '21 11:11 dree-max

try running **i from 0 to n inside loop ** . I think it might work.

karthik123karthik avatar Nov 15 '21 03:11 karthik123karthik

still didnt work for me

dree-max avatar Dec 24 '21 15:12 dree-max

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 :)

ghost avatar Jan 12 '22 09:01 ghost

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

dree-max avatar Feb 03 '22 19:02 dree-max

You can do it like this!

function scream(n)
{
    let str = '';
    while(n--)
    {
        str +='a';
    }
    console.log(str);
}

scream(10);

VaibhavArora19 avatar Feb 05 '22 13:02 VaibhavArora19

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

ghost avatar Feb 21 '22 18:02 ghost

@dree-max run loop as i=0 ; i < n; i++ this have to work

abhirajn avatar Jun 02 '22 19:06 abhirajn

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 :)

SiyonaL avatar Sep 05 '22 14:09 SiyonaL

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.)

Anshkaran7 avatar Mar 16 '23 17:03 Anshkaran7

// 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));`

vishal00923 avatar Apr 11 '23 12:04 vishal00923