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

Two "While Do" Conditional Inside of a Main Function

I want to ask about my code below, that doesn’t run as I expected when I use for loop, that show the result of all five data in total is 26, since I want to know what happens here.

function main() {
    var levels = parseInt(5,10);
    var points = new Array();
    
    var count = 0;
    while(count<levels){
        var elem = [4,5,6,7,4]; // in total must be 26
        points[count] = elem[count];
        count++;
    }
    
    var sum = 0;
    while(count<levels){
        var sum = sum + points[count];
        count++;
    } // I can make it worked using for loop

    console.log(sum)
} 

main()

>Solution :

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

var sum = 0;
count = 0; // Reset count to 0
while(count<levels){

Will return 26

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