Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find out below solution in JavaScript

I’m new for development area, pls help me.
**My code

    var totalNumberofRows = 5; 
    var output = '';
    for (var i = 1; i <= totalNumberofRows; i++) {
     
         for (var j = 1; j <= i; j++) {
            //  console.log('tes')
            output +=  i;
         }
        console.log(output+' ');
        output = '';
    }

Output:
1
22
333
4444
55555

Expected Output:

1
22
333
55555
88888888

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Pls some one suggest to me

>Solution :

Have you intended something similar to the Fibonacci sequence?

var totalNumberofRows = 5;
var output = '';

for (var i = 1; i <= totalNumberofRows; i++) {
    var next = fibo(i + 1);
    
    for (var j = 1; j <= next; j++) {
        output += next;
    }

    console.log(output + ' ');
    output = '';
}

function fibo(n) {
    if (n >= 2) {
        return fibo(n - 1) + fibo(n - 2)
    } else {
        return n; 
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading